`
flyer2010
  • 浏览: 355594 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

JMS消息服务AcitveMQ的配置和测试小例子

    博客分类:
  • Java
阅读更多
一、ActiveMQ安装与配置
1、安装文件:apache-activemq-5.2.0-bin.zip

2、安装过程:解压缩到apache-activemq-5.2.0-bin.zip到一个目录,比如C:\apache-activemq-5.2.0

3、ActiveMQ配置文件在C:\apache-activemq-5.2.0\conf\activemq.xml,采用默认的配置就可以运行了,多台做集群时可以修改该文件。

4、启动ActiveMQ:运行C:\apache-activemq-5.2.0\bin\activemq.bat

5、测试
ActiveMQ默认使用的TCP连接端口是61616, 通过查看该端口的信息可以测试ActiveMQ是否进入DOS:netstat -an|find "61616"
    TCP        0.0.0.0:61616                    0.0.0.0:0                            LISTENING

6、监控
ActiveMQ5.0版本默认启动时,启动了内置的jetty服务器,提供一个demo应用和用于监控ActiveMQ的admin应用。
admin:http://127.0.0.1:8161/admin/
demo:http://127.0.0.1:8161/demo/

二、测试ActiveMQ的一个简单例子

1.新建一个工程,将apache-activemq-5.2.0-bin.zip解压缩后里面的activemq-all-5.2.0.jar包加入到classpath下面,这个包包含了所有jms接口api的实现。

JMS消息的发送类JmsSender.java,代码如下:

package com.jms;
import org.apache.activemq.ActiveMQConnection; 
import org.apache.activemq.ActiveMQConnectionFactory; 

import javax.jms.*; 

/**
 * 
 * @author Wind
 *
 */
public class JmsSender { 
        public static void main(String[] args) throws JMSException { 
                // ConnectionFactory :连接工厂,JMS 用它创建连接 
                ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( 
                                ActiveMQConnection.DEFAULT_USER, 
                                ActiveMQConnection.DEFAULT_PASSWORD, 
                                "tcp://192.168.1.27:61616"); 
                //JMS 客户端到JMS Provider 的连接 
                Connection connection = connectionFactory.createConnection(); 
                connection.start(); 
                // Session: 一个发送或接收消息的线程 
                Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); 
                // Destination :消息的目的地;消息发送给谁. 
                // 获取session注意参数值my-queue是Query的名字 
                Destination destination = session.createQueue("my-queue"); 
                // MessageProducer:消息生产者 
                MessageProducer producer = session.createProducer(destination); 
                //设置不持久化 
                producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 
                //发送一条消息 
                sendMsg(session, producer); 
                session.commit(); 
                connection.close(); 
        } 

        /** 
         * 在指定的会话上,通过指定的消息生产者发出一条消息 
         * 
         * @param session    消息会话 
         * @param producer 消息生产者 
         */ 
        public static void sendMsg(Session session, MessageProducer producer) throws JMSException { 
                //创建一条文本消息 
                TextMessage message = session.createTextMessage("Hello ActiveMQ!"); 
                //通过消息生产者发出消息 
                producer.send(message); 
                System.out.println(""); 
        } 
}



JMS的消息接收类JmsReceiver.java

package com.jms;

import org.apache.activemq.ActiveMQConnection; 
import org.apache.activemq.ActiveMQConnectionFactory; 

import javax.jms.*; 

/**
 * 
 * @author Wind
 *
 */
public class JmsReceiver { 
        public static void main(String[] args) throws JMSException { 
                // ConnectionFactory :连接工厂,JMS 用它创建连接 
                ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( 
                                ActiveMQConnection.DEFAULT_USER, 
                                ActiveMQConnection.DEFAULT_PASSWORD, 
                                "tcp://192.168.1.27:61616"); 
                //JMS 客户端到JMS Provider 的连接 
                Connection connection = connectionFactory.createConnection(); 
                connection.start(); 
                // Session: 一个发送或接收消息的线程 
                Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); 
                // Destination :消息的目的地;消息发送给谁. 
                // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置 
                Destination destination = session.createQueue("my-queue"); 
                // 消费者,消息接收者 
                MessageConsumer consumer = session.createConsumer(destination); 
                while (true) { 
                        TextMessage message = (TextMessage) consumer.receive(1000); 
                        if (null != message) 
                                System.out.println("收到消息:" + message.getText()); 
                        else 
                                break; 
                } 
                session.close(); 
                connection.close(); 
        } 
}


3.启动ActiveMQ,先运行发送者JmsSender,然后运行JmsReceiver,控制台输出如下:
收到消息Hello ActiveMQ!


4.成功了,没什么问题,晚上再去整合下Spring+JMS的例子。

附:
1.activemq官方也有个HelloWorld例子,http://activemq.apache.org/hello-world.html
2.附件导入activemq-all-5.2.0.jar后才可以直接运行。
分享到:
评论
1 楼 urey_pp 2015-07-30  
感谢博主的分享,借此我写了一篇Linux的《Linux下Apache ActiveMQ5.9的安装配置与测试》http://dgd2010.blog.51cto.com/1539422/1680244。

相关推荐

Global site tag (gtag.js) - Google Analytics