ActiveMQ消息队列基本实现

1、启动ActiveMQ。如何安装启动ActiveMQ请看上一篇文章。

ActiveMQ 安装及启动(Windows)

2、Queue实现

①:pom文件依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>

②:消息发布

import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
@Slf4j
public class Producer {
    // ActiveMQ默认用户名
    private static final String USERNAME = "admin";
    // ActiveMQ默认登陆密码
    private static final String PASSWORD = "admin";
    // ActiveMQ链接地址
    private static final String BROKEN_URL = "tcp://localhost:61616";
    // 定义发送消息的主题名称
    private static final String QUEUE_NAME = "QueueMessage";

    public Boolean sendMessage() throws JMSException {
        // 创建连接工厂
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEN_URL);
        // 从工厂中创建一个链接
        Connection connection = activeMQConnectionFactory.createConnection();
        // 打开连接
        connection.start();
        // 创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //创建队列目标
        Destination destination = session.createQueue(QUEUE_NAME);
        // 创建一个生产者
        MessageProducer producer = session.createProducer(destination);
        // 创建消息
        TextMessage message = session.createTextMessage("么么哒~~~");
        // 发送消息
        producer.send(message);
        // 在本地打印消息
        System.out.println("发送消息:" + message.getText());
        // 关闭连接
        connection.close();
        return true;
    }
}

③:消息消费

import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
@Slf4j
public class Consumer {
    // ActiveMQ默认用户名
    private static final String USERNAME = "admin";
    // ActiveMQ默认登陆密码
    private static final String PASSWORD = "admin";
    // ActiveMQ链接地址
    private static final String BROKEN_URL = "tcp://localhost:61616";
    // 定义发送消息的主题名称
    private static final String QUEUE_NAME = "QueueMessage";

    Boolean result;

    public Boolean getMessage() throws JMSException {
        // 创建连接工厂
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEN_URL);
        // 从工厂中创建一个链接
        Connection connection = activeMQConnectionFactory.createConnection();
        // 打开连接
        connection.start();
        // 创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // 创建队列目标
        Destination destination = session.createQueue(QUEUE_NAME);
        // 创建消费者
        MessageConsumer consumer = session.createConsumer(destination);
        // 创建消费的监听
        consumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                TextMessage textMessage = (TextMessage) message;
                try {
                    System.out.println("获取消息:" + textMessage.getText());
                    if (textMessage.getText() != null || textMessage.getText() != "") {
                        result = true;
                    } else {
                        result = false;
                    }
                } catch (JMSException e) {
                    e.printStackTrace();
                    result = false;
                }
            }
        });

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        connection.close();
        return result;
    }
}

3、运行消息发布者和消费者。
在这里插入图片描述
在这里插入图片描述

  • Queue队列是一种点对点的消费模式, 即服务端发布一条消息,消费端根据发布的消息名称进行消息队列消费。消费后,该消息状态为已消费并回收,不再支持消费端进行消费。点对点消费不过时,服务端发布消息后,如果消费端未进行及时消费,则消息一直处于挂起状态,直到消费端启动并消费,整个消息队列流程才算完成。如果在等待消费过程中,服务端系统宕机,此时如果服务端发送消息状态为持久化消息(默认),则重新启动后会出现消息回流,消费端可继续进行消费;若消息状态为非持久化状态消息,消息丢失。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值