ActiveMQ学习笔记二-----Queue的生产和消费篇

本文详细介绍了ActiveMQ在JMS、Spring及SpringBoot中的应用,涵盖Queue的生产和消费,强调了监听模式下连接不能关闭的重要性,以及Topic的订阅顺序。还提供了Spring整合ActiveMQ的依赖注入、生产者与消费者的实现,以及SpringBoot中Queue和Topic的配置示例。
摘要由CSDN通过智能技术生成

本文将由三方面阐述JMS的应用:

  1. JMS
  2. Sprint整合ActiveMQ
  3. SpringBoot整合ActiveMQ

JMS

一、依赖

PTP(queue)模式生产者

        <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-core -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.11.2</version>
        </dependency>

二、Queue

1. 生产消息Producer

	//点对点(Queue)- 消息生产者demo
    public static void main(String[] args) throws JMSException {
   
        //1、创建连接工厂
        ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
        //2、创建连接
        Connection connection = factory.createConnection();
        //3、打开连接
        connection.start();
        //4、创建session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5、创建目标地址(Queue/Topic)
        Destination destination = session.createQueue("testQueue1");
        //6、创建消息生产者
        MessageProducer producer = session.createProducer(destination);
        //7、创建消息
        TextMessage textMessage = session.createTextMessage("test queue1 message");
        //8、发送消息
        producer.send(textMessage);
        System.out.println("message send success to testQueue1");
        //9、释放资源
        session.close();
        connection.close();
    }

2. 消费消息Consumer

注意:在监听器模式下不能关闭连接,一旦关闭,消息无法接收
方式一:

    public static void main(String[] args) throws JMSException {
   
        //1、创建连接工厂
        ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
        //2、创建连接
        Connection connection = factory.createConnection();
        //3、打开连接
        connection.start();
        //4、创建session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5、指定目标地址
        Destination destination = session.createQueue("testQueue1");
        //6、创建消息的消费者
        MessageConsumer consumer = session.createConsumer(destination);
        //7、设置消息监听器来接收消息
        consumer.setMessageListener(new MessageListener() {
   
            public void onMessage(Message message) {
   
                if (message instanceof TextMessage){
   
                    TextMessage message1 = (TextMessage)message;
                    try {
   
                        System.out.println(message1.getText());
                    } catch (JMSException e) {
   
                        e.printStackTrace();
                    }
                }
            }
        });
        //注意:在监听器模式下不能关闭连接,一旦关闭,消息无法接收
    }

方式二:

    public static void main(String[] args) throws JMSException {
   
        //1、创建连接工厂
        ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
        //2、创建连接
        Connection connection = factory.createConnection();
        //3、打开连接
        connection.start();
        //4、创建session
        Session session &
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值