zoukankan      html  css  js  c++  java
  • ActiveMQ简单例子

    1,下载ActiveMQ,

    http://activemq.apache.org/

    2,运行ActiveMQ,

    解压缩apache-activemq-5.5.1-bin.zip,然后双击apache-activemq-5.5.1\bin\activemq.bat运行ActiveMQ程序。

    启动ActiveMQ以后,登陆:http://localhost:8161/admin/,创建一个Queue,命名为FirstQueue(Send→ Destination =FirstQueue)。

    3,创建项目,

     

    3.1.Sender.java

    package com.testmq;
    
    import javax.jms.Connection;  
    import javax.jms.ConnectionFactory;  
    import javax.jms.DeliveryMode;  
    import javax.jms.Destination;  
    import javax.jms.MessageProducer;  
    import javax.jms.Session;  
    import javax.jms.TextMessage;  
      
    import org.apache.activemq.ActiveMQConnection;  
    import org.apache.activemq.ActiveMQConnectionFactory;  
      
      
    public class Sender {  
        private static final int SEND_NUMBER = 5;  
      
        public static void main(String[] args) {  
            // ConnectionFactory :连接工厂,JMS 用它创建连接  
            ConnectionFactory connectionFactory;  
            // Connection :JMS 客户端到JMS Provider 的连接  
            Connection connection = null;  
            // Session: 一个发送或接收消息的线程  
            Session session;  
            // Destination :消息的目的地;消息发送给谁.  
            Destination destination;  
            // MessageProducer:消息发送者  
            MessageProducer producer;  
            // TextMessage message;  
            // 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar  
            connectionFactory = new ActiveMQConnectionFactory(  
                    ActiveMQConnection.DEFAULT_USER,  
                    ActiveMQConnection.DEFAULT_PASSWORD,  
                    "tcp://localhost:61616");  
            try {  
                // 构造从工厂得到连接对象  
                connection = connectionFactory.createConnection();  
                // 启动  
                connection.start();  
                // 获取操作连接  
                session = connection.createSession(Boolean.TRUE,  
                        Session.AUTO_ACKNOWLEDGE);  
                // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置  
                destination = session.createQueue("FirstQueue");  
                // 得到消息生成者【发送者】  
                producer = session.createProducer(destination);  
                // 设置不持久化,此处学习,实际根据项目决定  
                producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);  
                // 构造消息,此处写死,项目就是参数,或者方法获取  
                sendMessage(session, producer);  
                session.commit();  
            } catch (Exception e) {  
                e.printStackTrace();  
            } finally {  
                try {  
                    if (null != connection)  
                        connection.close();  
                } catch (Throwable ignore) {  
                }  
            }  
        }  
      
        public static void sendMessage(Session session, MessageProducer producer)  
                throws Exception {  
            for (int i = 1; i <= SEND_NUMBER; i++) {  
                TextMessage message = session  
                        .createTextMessage("ActiveMq 发送的消息" + i);  
                // 发送消息到目的地方  
                System.out.println("发送消息:" + "ActiveMq 发送的消息" + i);  
                producer.send(message);  
            }  
        }  
    }

    3.2.Receiver.java

    package com.testmq;
    
    import javax.jms.Connection;  
    import javax.jms.ConnectionFactory;  
    import javax.jms.Destination;  
    import javax.jms.MessageConsumer;  
    import javax.jms.Session;  
    import javax.jms.TextMessage;  
      
    import org.apache.activemq.ActiveMQConnection;  
    import org.apache.activemq.ActiveMQConnectionFactory;  
      
      
    public class Receiver {  
        public static void main(String[] args) {  
            // ConnectionFactory :连接工厂,JMS 用它创建连接  
            ConnectionFactory connectionFactory;  
            // Connection :JMS 客户端到JMS Provider 的连接  
            Connection connection = null;  
            // Session: 一个发送或接收消息的线程  
            Session session;  
            // Destination :消息的目的地;消息发送给谁.  
            Destination destination;  
            // 消费者,消息接收者  
            MessageConsumer consumer;  
            connectionFactory = new ActiveMQConnectionFactory(  
                    ActiveMQConnection.DEFAULT_USER,  
                    ActiveMQConnection.DEFAULT_PASSWORD,  
                    "tcp://localhost:61616");  
            try {  
                // 构造从工厂得到连接对象  
                connection = connectionFactory.createConnection();  
                // 启动  
                connection.start();  
                // 获取操作连接  
                session = connection.createSession(Boolean.FALSE,  
                        Session.AUTO_ACKNOWLEDGE);  
                // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置  
                destination = session.createQueue("FirstQueue");  
                consumer = session.createConsumer(destination);  
                while (true) {  
                    //设置接收者接收消息的时间,为了便于测试,这里谁定为100s  
                    TextMessage message = (TextMessage) consumer.receive(100000);  
                    if (null != message) {  
                        System.out.println("收到消息" + message.getText());  
                    } else {  
                        break;  
                    }  
                }  
            } catch (Exception e) {  
                e.printStackTrace();  
            } finally {  
                try {  
                    if (null != connection)  
                        connection.close();  
                } catch (Throwable ignore) {  
                }  
            }  
        }  
    }

    运行sender后:

    4.注意事项

    1. 最后接收者跟发送者在不同的机器上测试
    2. 项目所引用的jar最后在ActiveMQ下的lib中找,这样不会出现版本冲突。

    5.测试过程

    Sender的console显示如下信息:

    发送消息:ActiveMq 发送的消息1
    发送消息:ActiveMq 发送的消息2
    发送消息:ActiveMq 发送的消息3
    发送消息:ActiveMq 发送的消息4
    发送消息:ActiveMq 发送的消息5

    Receiver的console界面出现如下信息:

    收到消息ActiveMq 发送的消息1
    收到消息ActiveMq 发送的消息2
    收到消息ActiveMq 发送的消息3
    收到消息ActiveMq 发送的消息4
    收到消息ActiveMq 发送的消息5

  • 相关阅读:
    java数组转list
    【转载】tomcat端口被占用问题完美解决方案!
    基于apicloud的英语课堂app设计与实现
    springboot整合mybatis(SSM开发环境搭建)
    POI Excel读取图片对应位置和顺序生成图片方法
    E: Sub-process /usr/bin/dpkg returned an error code (1) 出错解决方案
    Ubuntu 虚拟机无法关机的解决方案
    Celery 提示[ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 61] Connection refused.
    Python @classmethod&@staticmethod 区别
    SyntaxError: non-default argument follows default argument
  • 原文地址:https://www.cnblogs.com/qq2083587182/p/6595828.html
Copyright © 2011-2022 走看看