• activemq生产者消费者


    1.导入依赖
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-all</artifactId>
      <version>5.15.9</version>
    </dependency>
    <dependency>
      <groupId>org.apache.xbean</groupId>
      <artifactId>xbean-spring</artifactId>
      <version>3.16</version>
    </dependency>

    2.生产者

    package com.hourui.activemq;
    
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    import javax.jms.*;
    
    public class JmsProduce {
    
        public static final String ACTIVEMQ_URL = "tcp://192.168.61.100:61616";
        public static final String QUEUE_NAME = "queue01";
    
    
        public static void main(String[ ] args) throws JMSException {
            //1 创建连接工厂,按照给定的url地址,采用默认用户名和密码
            ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
            //2 通过连接工厂,获取连接connection 并启动访问
            Connection connection = activeMQConnectionFactory.createConnection();
            connection.start();
    
            //3 创建会话session
            //两个参数,第一个叫事务/第二个叫签收
            Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
            //4 创建目的地(具体是队列还是主题topic)
            Queue queue = session.createQueue(QUEUE_NAME);
            //5 创建消息的生产者
            MessageProducer messageProducer = session.createProducer(queue);
            //6 通过使用messageProducer生产3条信息发送到MQ的队列里面
            for(int i = 1; i <= 6; i++) {
                //7 创建消息,好比学生按照老师要求写好的面试题消息
                TextMessage textMessage = session.createTextMessage("msg---" + i);
                //8 通过messageProducer发送给mq
                messageProducer.send(textMessage);
            }
            //9 关闭资源
            messageProducer.close();
            session.close();
            connection.close();
            System.out.println("*********消息发布到MQ完成");
    
        }
    }

    3.消费者

    package com.hourui.activemq;
    
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    import javax.jms.*;
    
    public class JmsConsumer {
    
        public static final String ACTIVEMQ_URL = "tcp://192.168.61.100:61616";
        public static final String QUEUE_NAME = "queue01";
    
        public static void main(String[ ] args) throws JMSException {
    
            System.out.println("我是1号消费者");
            //1 创建连接工厂,按照给定的url地址,采用默认用户名和密码
            ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
            //2 通过连接工厂,获取连接connection 并启动访问
            Connection connection = activeMQConnectionFactory.createConnection();
            connection.start();
    
            //3 创建会话session
            //两个参数,第一个叫事务/第二个叫签收
            Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
            //4 创建目的地(具体是队列还是主题topic)
            Queue queue = session.createQueue(QUEUE_NAME);
            //5 创建消费者
            MessageConsumer messageConsumer = session.createConsumer(queue);
            while (true) {
                TextMessage textMessage = (TextMessage)messageConsumer.receive();
                //TextMessage textMessage = (TextMessage)messageConsumer.receive(4000L);
                if(null != textMessage) {
                    System.out.println("消费者接收到消息"+textMessage.getText());
                } else {
                    break;
                }
            }
            messageConsumer.close();
            session.close();
            connection.close();
            System.out.println("*********消息发布到MQ完成");
    
        }
    }
    package com.hourui.activemq;
    
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    import javax.jms.*;
    import java.io.IOException;
    
    public class JmsConsumer1 {
    
        public static final String ACTIVEMQ_URL = "tcp://192.168.61.100:61616";
        public static final String QUEUE_NAME = "queue01";
    
        public static void main(String[ ] args) throws JMSException, IOException {
            System.out.println("我是2号消费者");
            //1 创建连接工厂,按照给定的url地址,采用默认用户名和密码
            ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
            //2 通过连接工厂,获取连接connection 并启动访问
            Connection connection = activeMQConnectionFactory.createConnection();
            connection.start();
    
            //3 创建会话session
            //两个参数,第一个叫事务/第二个叫签收
            Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
            //4 创建目的地(具体是队列还是主题topic)
            Queue queue = session.createQueue(QUEUE_NAME);
            //5 创建消费者
            MessageConsumer messageConsumer = session.createConsumer(queue);
    
            //通过监听的方式来消费消息
            messageConsumer.setMessageListener(new MessageListener() {
                @Override
                public void onMessage(Message message) {
                    TextMessage textMessage =(TextMessage)message;
                    if(null != message && message instanceof TextMessage) {
                        try {
                            System.out.println("**********消费者接收到消息:" + textMessage.getText());
                        } catch (JMSException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            System.in.read();
            messageConsumer.close();
            session.close();
            connection.close();
            System.out.println("*********消息发布到MQ完成");
    
        }
    }



  • 相关阅读:
    Docker--简介&&安装
    Mycat
    Mysql--主从复制
    Nginx--平滑升级
    Nginx--rewrite
    Nginx--缓存
    Mysql--SQL语句
    Nginx--虚拟主机
    Nginx--反向代理&&负载均衡
    Nginx--用户认证&&访问控制&&限速&&状态访问
  • 原文地址:https://www.cnblogs.com/liuyi13535496566/p/12500672.html
Copyright © 2020-2023  润新知