zoukankan      html  css  js  c++  java
  • ActiveMq入门实例

    pom.xml配置

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

    消息生产者

    public class JMSProducer {
    
        //默认连接用户名
        private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
        //默认连接密码
        private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
        //默认连接地址
        private static final String BROKEURL = "tcp://xxx.xx.xx.xx:61616";
        //发送的消息数量
        private static final int SENDNUM = 10;
    
        public static void main(String[] args) {
            //连接工厂
            ConnectionFactory connectionFactory;
            //连接
            Connection connection = null;
            //会话 接受或者发送消息的线程
            Session session;
            //消息的目的地
            Destination destination;
            //消息生产者
            MessageProducer messageProducer;
            //实例化连接工厂
            connectionFactory = new ActiveMQConnectionFactory(JMSProducer.USERNAME, JMSProducer.PASSWORD, JMSProducer.BROKEURL);
    
            try {
                //通过连接工厂获取连接
                connection = connectionFactory.createConnection();
                //启动连接
                connection.start();
                //创建session
                session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
                //创建一个名称为HelloWorld的消息队列
                destination = session.createQueue("HelloWorld");
                //创建消息生产者
                messageProducer = session.createProducer(destination);
                //发送消息
                sendMessage(session, messageProducer);
    
                session.commit();
    
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                if(connection != null){
                    try {
                        connection.close();
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }
    
        }
        /**
         * 发送消息
         * @param session
         * @param messageProducer  消息生产者
         * @throws Exception
         */
        public static void sendMessage(Session session,MessageProducer messageProducer) throws Exception{
            for (int i = 0; i < JMSProducer.SENDNUM; i++) {
                //创建一条文本消息
                TextMessage message = session.createTextMessage("ActiveMQ 发送消息" +i);
                System.out.println("发送消息boothsun:Activemq 发送消息" + i);
                //通过消息生产者发出消息
                messageProducer.send(message);
            }
    
        }
    }
    
    

    运行截图:

    消息管理后台页面:

    消息消费者

    public class JMSConsumer {
        private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;//默认连接用户名
        private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;//默认连接密码
        private static final String BROKEURL = "tcp://xxx.xxx.xxx.xxx:61616";//连接地址
    
        public static void main(String[] args) {
            ConnectionFactory connectionFactory;//连接工厂
            Connection connection = null;//连接
    
            Session session;//会话 接受或者发送消息的线程
            Destination destination;//消息的目的地
    
            MessageConsumer messageConsumer;//消息的消费者
    
            //实例化连接工厂
            connectionFactory = new ActiveMQConnectionFactory(JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKEURL);
    
            try {
                //通过连接工厂获取连接
                connection = connectionFactory.createConnection();
                //启动连接
                connection.start();
                //创建session
                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                //创建一个连接HelloWorld的消息队列
                destination = session.createQueue("HelloWorld");
                //创建消息消费者
                messageConsumer = session.createConsumer(destination);
    
                while (true) {
                    TextMessage textMessage = (TextMessage) messageConsumer.receive(100000);
                    if(textMessage != null){
                        System.out.println("收到的消息:" + textMessage.getText());
                    }else {
                        break;
                    }
                }
    
            } catch (JMSException e) {
                e.printStackTrace();
            }
    
        }
    }
    

    运行截图:

    消息管理后台页面:

  • 相关阅读:
    python中的split、rsplit、splitlines
    docker 启动,端口映射,挂载本地目录
    pycharm18.2.4 + Python3.7.1 安装salt报错python pip install salt: Command "python setup.py egg_info" failed with error code 10 及解决方法
    Dockerfile 下安装Python3.7.4 环境命令
    linux项目部署学习(5)
    linux环境下安装selenium(python3)
    linux项目部署学习(4)
    linux项目部署学习(3) -nginx/发布crm
    linux项目部署学习(2)
    nginx语法之location详解
  • 原文地址:https://www.cnblogs.com/boothsun/p/7482792.html
Copyright © 2011-2022 走看看