ActiveMQ--HelloWorld

来源:互联网 发布:spss19.0 for mac下载 编辑:程序博客网 时间:2024/06/04 19:50

ActiveMQ

Apache ActiveMQ ™ is the most popular and powerful open source messaging and Integration Patterns server.Apache ActiveMQ is fast, supports many Cross Language Clients and Protocols, comes with easy to use Enterprise Integration Patterns and many advanced features while fully supporting JMS 1.1 and J2EE 1.4. 

ActiveMQ是由Apache出品的,一款最流行的,能力强劲的开源消息总线。ActiveMQ是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,它非常快速,支持多种语言的客户端和协议,而且可以非常容易的嵌入到企业的应用环境中,并有许多高级功能。

1.下载ActiveMQ

官方网站:http://archive.apache.org/dist/activemq/5.11.1/

2.解压缩 
大家现在好之后,将apache-activemq-5.11.1-bin.zip解压缩,我们可以看到它的整体目录结构: 
 
从它的目录来说,还是很简单的: 
  • bin存放的是脚本文件
  • conf存放的是基本配置文件
  • data存放的是日志文件
  • docs存放的是说明文档
  • examples存放的是简单的实例
  • lib存放的是activemq所需jar包
  • webapps用于存放项目的目录
3.启动ActiveMQ 

双击bin目录下运行自己电脑版本(win32或者win64)下的activemq.bat,就可以看下图的效果。


从上图我们可以看到activemq的存放地址,以及浏览器要访问的地址. 

4.测试

ActiveMQ默认使用的TCP连接端口是61616, 通过查看该端口的信息可以测试ActiveMQ是否成功启动 netstat -aon | findstr "61616"

C:\Users\lenovo>netstat -aon | findstr "61616"  TCP    0.0.0.0:61616          0.0.0.0:0                LISTENING       3608  TCP    [::]:61616             [::]:0                 LISTENING       3608

5. 监控 
ActiveMQ默认启动时,启动了内置的jetty服务器,提供一个用于监控ActiveMQ的admin应用。 
admin:http://127.0.0.1:8161/admin/

用户名和密码都是admin


6.搭建开发环境

  • 建立项目 
    我们只需要建立一个java项目就可以了,导入jar包,项目截图: 
    这里写图片描述

点对点的消息模型,只需要一个消息生成者和消息消费者,下面我们编写代码。

  • 编写生产者
package com.tgb.activemq;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.MessageProducer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;/** * 消息的生产者(发送者)  * @author liang * */public class JMSProducer {    //默认连接用户名    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;    //默认连接密码    private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;    //默认连接地址    private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL;    //发送的消息数量    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("发送消息:Activemq 发送消息" + i);            //通过消息生产者发出消息             messageProducer.send(message);        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 编写消费者
package com.tgb.activemq;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.MessageConsumer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;/** * 消息的消费者(接受者) * @author liang * */public class JMSConsumer {    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;//默认连接用户名    private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;//默认连接密码    private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL;//默认连接地址    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();        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

7.运行

  1. 首先,启动ActiveMQ
  2. 运行发送者,eclipse控制台输出,如下图: 
    这里写图片描述 
    此时,我们先看一下ActiveMQ服务器,Queues内容如下: 
    这里写图片描述
    我们可以看到创建了一个名称为HelloWorld的消息队列,队列中有10条消息未被消费,我们也可以通过Browse查看是哪些消息,如下图: 
    这里写图片描述
    如果这些队列中的消息,被删除,消费者则无法消费。

  3. 我们继续运行一下消费者,eclipse控制台打印消息,如下: 
    这里写图片描述 
    此时,我们先看一下ActiveMQ服务器,Queues内容如下: 
    这里写图片描述
    我们可以看到HelloWorld的消息队列发生变化,多一个消息者,队列中的10条消息被消费了,点击Browse查看,已经为空了。 
    点击Active Consumers,我们可以看到这个消费者的详细信息: 
    这里写图片描述

8.demo结束


1 0
原创粉丝点击