zoukankan      html  css  js  c++  java
  • ActiveMQ 安装和使用

    一、Window下 ActiveMQ的安装 

    1、下载ActiveMQ, 这里使用的版本为5.5.1

    https://cwiki.apache.org/confluence/display/ACTIVEMQ/ActiveMQ+5.5.1+Release

    2、启动ActiveMQ

    cd D:softwaremqapache-activemq-5.5.1-binapache-activemq-5.5.1in

    输入activemq.bat (我的电脑是64位)

     启动后如下图所示:说明启动成功

    3、打开ActiveMQ

    localhost:8161/admin/admin

    二、ActiveMQ的使用(点对点方式)

    1、创建生产者

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

      

    2、创建消费者

    /**
     * 消息消费者
     */
    public class ActiveMQConsumer {
    
        private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
        private static final String PASSWORD  = ActiveMQConnection.DEFAULT_PASSWORD;//默认密码
        private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL; //默认的连接地址
    
        public static void main(String[] args) throws JMSException {
            ConnectionFactory connectionFactory; //连接工厂
            Connection connection = null; //连接
            Session session; //会话 接受或者发送消息的线程
            Destination destination; // 消息的目的地
            MessageConsumer messageConsumer; //消息消费者
            //实例化连接工厂
            connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);
            try{
                //通过连接工厂获取连接
                connection = connectionFactory.createConnection();
                //启动连接
                connection.start();
                //创建session
                session = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);
                //创建消息队列
                destination = session.createQueue("FirstQueue1");
                //创建消息消费者
                messageConsumer= session.createConsumer(destination);
                while (true){
                  TextMessage textMessage  = (TextMessage)  messageConsumer.receive(100000); // 100秒接收1次
                    if(textMessage != null){
                        System.out.println("接收的消息:" + textMessage.getText());
                    }else {
                        break;
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if(connection != null){
                    connection.close();
                }
            }
    
        }
    
    
    
    } 

    运行后,查看ActiveMQ

    缺点: while(true) 一直监听,使用Listener消费

    3、使用Listerer消费

    /**
     * 消息消费者
     */
    public class ActiveMQConsumer2 {
    
        private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
        private static final String PASSWORD  = ActiveMQConnection.DEFAULT_PASSWORD;//默认密码
        private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL; //默认的连接地址
    
        public static void main(String[] args) throws JMSException, InterruptedException {
            ConnectionFactory connectionFactory; //连接工厂
            Connection connection = null; //连接
            Session session; //会话 接受或者发送消息的线程
            Destination destination; // 消息的目的地
            MessageConsumer messageConsumer; //消息消费者
            //实例化连接工厂
            connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);
            try{
                //通过连接工厂获取连接
                connection = connectionFactory.createConnection();
                //启动连接
                connection.start();
                //创建session
                session = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);
                //创建消息队列
                destination = session.createQueue("FirstQueue1");
                //创建消息消费者
                messageConsumer= session.createConsumer(destination);
                //注册消息监听
                messageConsumer.setMessageListener(new Listener());
            }catch (Exception e){
                e.printStackTrace();
            }
    
        }
    
    }
    

      

    三、ActiveMQ发布-订阅模式

    一个人发布,多个人订阅

    1、生产者

    /**
     * 消息生产者-消息发布者
     */
    public class ActiveMQProducer {
    
        private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
        private static final String PASSWORD  = ActiveMQConnection.DEFAULT_PASSWORD;//默认密码
        private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL; //默认的连接地址
        private static final int SENDNUM = 10; //发送的消息数量
    
        public static void main(String[] args) throws JMSException {
            ConnectionFactory connectionFactory; //连接工厂
            Connection connection = null; //连接
            Session session; //会话 接受或者发送消息的线程
            Destination destination; // 消息的目的地
            MessageProducer messageProducer; //消息生产者
            //实例化连接工厂
            connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);
            try{
                //通过连接工厂获取连接
                connection = connectionFactory.createConnection();
                //启动连接
                connection.start();
                //创建session
                session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
                destination = session.createTopic("FirstTopic1");
                //创建消息生产者
                messageProducer = session.createProducer(destination);
                //发送消息
                sendMessage(session, messageProducer);
                session.commit();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if(connection != null){
                    connection.close();
                }
            }
    
        }
    
        /**
         * 发送消息
         * @param session
         * @param messageProducer
         */
        public static  void sendMessage(Session session, MessageProducer messageProducer) throws JMSException {
            for(int i = 0; i < ActiveMQProducer.SENDNUM; i++){
                String msg = "ActiveMQ 发布的消息"+ i;
                TextMessage message = session.createTextMessage(msg);
                System.out.println("发布消息: " + msg );
                messageProducer.send(message);
            }
        }
    
    
    }
    

      

    2、消费者-订阅者1

    /**
     * 消息消费者-消息订阅者1
     */
    public class ActiveMQConsumer {
    
        private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
        private static final String PASSWORD  = ActiveMQConnection.DEFAULT_PASSWORD;//默认密码
        private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL; //默认的连接地址
    
        public static void main(String[] args) throws JMSException {
            ConnectionFactory connectionFactory; //连接工厂
            Connection connection = null; //连接
            Session session; //会话 接受或者发送消息的线程
            Destination destination; // 消息的目的地
            MessageConsumer messageConsumer; //消息消费者
            //实例化连接工厂
            connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);
            try{
                //通过连接工厂获取连接
                connection = connectionFactory.createConnection();
                //启动连接
                connection.start();
                //创建session
                session = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);
    
                destination = session.createTopic("FirstTopic1");
                //创建消息消费者
                messageConsumer= session.createConsumer(destination);
                //注册消息监听
                messageConsumer.setMessageListener(new Listener());
            }catch (Exception e){
                e.printStackTrace();
            }
    
        }
    
    
    }
    

      

    Listener

    /**
     * 消息监听-订阅1
     */
    public class Listener  implements MessageListener {
        @Override
        public void onMessage(Message message) {
            try {
                System.out.println("订阅者1 收到的消息:" + ((TextMessage)message).getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
    

      

    3、消费者-订阅者2

    /**
     * 消息消费者-消息订阅者2
     */
    public class ActiveMQConsumer2 {
    
        private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
        private static final String PASSWORD  = ActiveMQConnection.DEFAULT_PASSWORD;//默认密码
        private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL; //默认的连接地址
    
        public static void main(String[] args) throws JMSException {
            ConnectionFactory connectionFactory; //连接工厂
            Connection connection = null; //连接
            Session session; //会话 接受或者发送消息的线程
            Destination destination; // 消息的目的地
            MessageConsumer messageConsumer; //消息消费者
            //实例化连接工厂
            connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);
            try{
                //通过连接工厂获取连接
                connection = connectionFactory.createConnection();
                //启动连接
                connection.start();
                //创建session
                session = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);
    
                destination = session.createTopic("FirstTopic1");
                //创建消息消费者
                messageConsumer= session.createConsumer(destination);
                //注册消息监听
                messageConsumer.setMessageListener(new Listener2());
            }catch (Exception e){
                e.printStackTrace();
            }
    
        }
    
    
    
    }
    

      

    Listener2

    /**
     * 消息监听-订阅2
     */
    public class Listener2 implements MessageListener {
        @Override
        public void onMessage(Message message) {
            try {
                System.out.println("订阅者2 收到的消息:" + ((TextMessage)message).getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
    

      

    先运行生产者,再运行订阅者1和订阅者2,可以看到两个订阅者都收到了消息。

    三、Linux环境下

    1、下载activeMQ的linux

    2、启动activemq

    cd /home/mc/apache-activemq/bin/linux-x86-64

    chmod 755 activemq

    ./activemq start

    3、访问activemq

    http://118.xx.xx.101:18161/admin/index.jsp

    输入用户名和密码

    作者:Work Hard Work Smart
    出处:http://www.cnblogs.com/linlf03/
    欢迎任何形式的转载,未经作者同意,请保留此段声明!

  • 相关阅读:
    winform 单选框, 图像控件,图像列表,状态栏,定时器,绘图
    学习资料链接,csdn博客
    c# 基本控件,窗口程序
    Gmap.net 怎么导入离线地图
    修改MFC主窗口界面标题和图标的方法
    vc6.0缓冲区
    vc6.0编译出错,删除多余的文件,清空重新编译
    Tomcat插件与Jetty插件在MyEclipse中的配置
    div里面的内容超出自身高度时,显示省略号
    aptana studio 3 自动换行(无需插件)
  • 原文地址:https://www.cnblogs.com/linlf03/p/14642959.html
Copyright © 2011-2022 走看看