大鸟的学习乐园
路漫漫其修远兮,吾将上下而求索
posts - 26,comments - 27,trackbacks - 0
1、环境:
Windows XP
apache-activemq-5.2.0-bin.zip
 
2、安装
解压缩到apache-activemq-5.2.0-bin.zip到一个目录,比如C:\apache-activemq-5.2.0
 
3、配置
配置就在C:\apache-activemq-5.2.0\conf目录下三个文件
activemq.xml
credentials.properties
log4j.properties
 
4、启动ActiveMQ
运行C:\apache-activemq-5.2.0\bin\activemq.bat
5、测试
ActiveMQ默认使用的TCP连接端口是61616, 通过查看该端口的信息可以测试ActiveMQ是否成功启动 netstat -an|find "61616"

C:\Documents and Settings\Administrator>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/

下面是ActiveMQ5.2的一个最简单例子!
环境还是apache-activemq-5.2.0-bin.zip,需要注意的是,开发时候,要将apache-activemq- 5.2.0-bin.zip解压缩后里面的activemq-all-5.2.0.jar包加入到classpath下面,这个包包含了所有jms接口 api的实现。

Java代码 复制代码
  1. import org.apache.activemq.ActiveMQConnection;   
  2. import org.apache.activemq.ActiveMQConnectionFactory;   
  3.   
  4. import javax.jms.*;   
  5.   
  6. /**  
  7. * 消息的生产者(发送者)  
  8. *  
  9. */  
  10. public class JmsSender {   
  11.         public static void main(String[] args) throws JMSException {   
  12.                 // ConnectionFactory :连接工厂,JMS 用它创建连接   
  13.                 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(   
  14.                                 ActiveMQConnection.DEFAULT_USER,   
  15.                                 ActiveMQConnection.DEFAULT_PASSWORD,   
  16.                                 "tcp://192.168.14.117:61616");   
  17.                 //JMS 客户端到JMS Provider 的连接   
  18.                 Connection connection = connectionFactory.createConnection();   
  19.                 connection.start();   
  20.                 // Session: 一个发送或接收消息的线程   
  21.                 Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);   
  22.                 // Destination :消息的目的地;消息发送给谁.   
  23.                 // 获取session注意参数值my-queue是Query的名字   
  24.                 Destination destination = session.createQueue("my-queue");   
  25.                 // MessageProducer:消息生产者   
  26.                 MessageProducer producer = session.createProducer(destination);   
  27.                 //设置不持久化   
  28.                 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);   
  29.                 //发送一条消息   
  30.                 sendMsg(session, producer);   
  31.                 session.commit();   
  32.                 connection.close();   
  33.         }   
  34.   
  35.         /**  
  36.          * 在指定的会话上,通过指定的消息生产者发出一条消息  
  37.          *  
  38.          * @param session    消息会话  
  39.          * @param producer 消息生产者  
  40.          */  
  41.         public static void sendMsg(Session session, MessageProducer producer) throws JMSException {   
  42.                 //创建一条文本消息   
  43.                 TextMessage message = session.createTextMessage("Hello ActiveMQ!");   
  44.                 //通过消息生产者发出消息   
  45.                 producer.send(message);   
  46.                 System.out.println("");   
  47.         }   
  48. }  
 
Java代码 复制代码
  1. import org.apache.activemq.ActiveMQConnection;   
  2. import org.apache.activemq.ActiveMQConnectionFactory;   
  3.   
  4. import javax.jms.*;   
  5.   
  6. /**  
  7. * 消息的消费者(接受者)  
  8. *  
  9. */  
  10. public class JmsReceiver {   
  11.         public static void main(String[] args) throws JMSException {   
  12.                 // ConnectionFactory :连接工厂,JMS 用它创建连接   
  13.                 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(   
  14.                                 ActiveMQConnection.DEFAULT_USER,   
  15.                                 ActiveMQConnection.DEFAULT_PASSWORD,   
  16.                                 "tcp://192.168.14.117:61616");   
  17.                 //JMS 客户端到JMS Provider 的连接   
  18.                 Connection connection = connectionFactory.createConnection();   
  19.                 connection.start();   
  20.                 // Session: 一个发送或接收消息的线程   
  21.                 Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);   
  22.                 // Destination :消息的目的地;消息发送给谁.   
  23.                 // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置   
  24.                 Destination destination = session.createQueue("my-queue");   
  25.                 // 消费者,消息接收者   
  26.                 MessageConsumer consumer = session.createConsumer(destination);   
  27.                 while (true) {   
  28.                         TextMessage message = (TextMessage) consumer.receive(1000);   
  29.                         if (null != message)   
  30.                                 System.out.println("收到消息:" + message.getText());   
  31.                         else  
  32.                                 break;   
  33.                 }   
  34.                 session.close();   
  35.                 connection.close();   
  36.         }   
  37. }  
 
启动ActiveMQ,然后开始执行:
先运行发送者,连续运行了三次,最后一次控制台输出:


Process finished with exit code 0
 
后运行接受者,输出结果:
收到消息Hello ActiveMQ!
收到消息Hello ActiveMQ!
收到消息Hello ActiveMQ!

Process finished with exit code 0
 
注意:
其中的端口61616是ActiveMQ默认的配置,在activemq.xml中,

Xml代码 复制代码
  1. <!-- The transport connectors ActiveMQ will listen to -->  
  2.              <transportConnectors>  
  3.                      <transportConnector name="openwire" uri="tcp://localhost:61616" discoveryUri="multicast://default"/>  
  4.                      <transportConnector name="ssl" uri="ssl://localhost:61617"/>  
  5.                      <transportConnector name="stomp" uri="stomp://localhost:61613"/>  
  6.                      <transportConnector name="xmpp" uri="xmpp://localhost:61222"/>  
  7.              </transportConnectors>   
 
,建议不要改动,都用这个端口多好,就像ftp都用21端口,也没错。
 
 
这是官方的HelloWorld例子,不过看着不顺眼:
http://activemq.apache.org/hello-world.html

posted on 2009-09-16 13:00 大鸟 阅读(1286) 评论(0)  编辑  收藏 所属分类: JAVA

只有注册用户登录后才能发表评论。


网站导航: