apache的强劲的消息总线服务

最近学习了一段时间的ActiveMQ,apache的强劲的消息总线服务。
学习过程参考了ActiveMQ in Action和whitesock的javaeye博客。
使用消息中间件来进行消息传递的原理

1.生产者
public class XuSendMessage
{
 private Connection mqConnection;
 private Session mqSession;
 private static final String url ="tcp://10.131.98.127:61616";  
 private static final String QUEUE_NAME ="xujianhui.queue";  
 protected String expectedBody = "<hello>world!ererererer201100</hello>";
 public XuSendMessage() throws JMSException
 {
  createMQConnection();
  createMQSession();
 }
 protected Connection createMQConnection() throws JMSException
 {
  ActiveMQConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory(url);
  mqConnection = mqConnectionFactory.createQueueConnection();
  mqConnection.start();
  return mqConnection;
 }
 
 protected Session createMQSession() throws JMSException
 {
  mqSession = mqConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  return mqSession;
 }
 
 public void sendMessage(String msg) throws JMSException
 {
  Destination destination = mqSession.createQueue(QUEUE_NAME);
  MessageProducer producer = mqSession.createProducer(destination);
  TextMessage message = mqSession.createTextMessage(msg);
  message.setStringProperty("headname", "remoteB");  
  producer.send(message);
 }
 
 public void sendMessageObj(XuUser msg) throws JMSException
 {
  Destination destination = mqSession.createQueue(QUEUE_NAME);
  MessageProducer producer = mqSession.createProducer(destination);
  ObjectMessage message = mqSession.createObjectMessage();
  message.setStringProperty("headname", "remoteB");
  message.setObject(msg);
  producer.send(message);
 }
 
 protected void close()throws JMSException{
  mqConnection.stop();
  mqConnection.close();
  
 }
 
 
 public static void main(String str[])
 {
  XuSendMessage xSend = null;
  try{
   xSend = new XuSendMessage();
   System.out.println("发送消息");
   String msg ="客户群创建成功";
   for(int i = 0 ; i < 1; i++)
   {
    XuUser xuUser = new XuUser();
    xuUser.setMobile("13939065991_"+i);
    xuUser.setAge(i);
    xuUser.setName("测试消息_"+i);
    xSend.sendMessageObj(xuUser);
    //xSend.sendMessage(msg+i);
    
   }
   
   xSend.close();
  }catch(Exception e)
  {
   e.printStackTrace();
  }
  
 }

}


2.消费者
public class XuReceiveMessage implements MessageListener,ExceptionListener{

 private static final String url = "tcp://10.131.98.127:61616";  
 private static final String QUEUE_NAME = "xujianhui.queue";
 private String user = ActiveMQConnection.DEFAULT_USER;  
 private String password = ActiveMQConnection.DEFAULT_PASSWORD;
 private Connection mqConnection = null;
 private Session mqSession = null;
 private Destination mqDestination = null;
 private String name = null;
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  XuReceiveMessage xrm = null;
  XuReceiveMessage xrm1 = null;
  xrm = new XuReceiveMessage();
  xrm.setName("xujianhui");
  xrm1 = new XuReceiveMessage();
  xrm1.setName("zhouyongzheng");
  try{
   xrm.run();
   xrm1.run();
  }catch(Exception e){
   e.printStackTrace();
  }
  
 }
 @Override
 public void onMessage(Message msg){
  // TODO Auto-generated method stub
  try
  {
   if(msg instanceof TextMessage)
   {
    TextMessage txtMsg = (TextMessage)msg;
    String msgStr = txtMsg.getText();
    System.out.println(this.getName()+" Received:"+msgStr);
   }else if(msg instanceof ObjectMessage)
   {
    ObjectMessage objMsg = (ObjectMessage)msg;
    XuUser xuUser = (XuUser)objMsg.getObject();
    System.out.println("name="+xuUser.getName());
    System.out.println("mobile="+xuUser.getMobile());
    System.out.println("age="+xuUser.getAge());
   }
  }catch(Exception e){
   e.printStackTrace();
  }
  
 }
 @Override
 public void onException(JMSException arg0) {
  // TODO Auto-generated method stub
  
 }
 
 public void run() throws JMSException
 {
  ActiveMQConnectionFactory amcf = new ActiveMQConnectionFactory(user,password,url);
  mqConnection = amcf.createConnection();
  mqConnection.setExceptionListener(this);
  
  mqConnection.start();
  mqSession = mqConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  
  mqDestination = mqSession.createQueue(QUEUE_NAME);
  
  MessageConsumer mqMessageConsumer = mqSession.createConsumer(mqDestination);
  
  mqMessageConsumer.setMessageListener(this);
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 
 
 
 

}

 

 

3.发布者
public class XuTopicPublisher {

 private static String USER = ActiveMQConnectionFactory.DEFAULT_USER;
 
 private static String PASSWORD = ActiveMQConnectionFactory.DEFAULT_PASSWORD;
 
 private static String URL = "tcp://localhost:61616";
 
 private static String TOPIC_NAME = "my_topic";
 
 private Connection mqConnection = null;
 
 private Session mqSession = null;
 
 private Topic mqTopic = null;
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  XuTopicPublisher xp = null;
  xp = new XuTopicPublisher();
  try{
   xp.run();
  }catch(Exception e){
   e.printStackTrace();
  };
  
 }
 
 public void run() throws JMSException
 {
  ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(USER,PASSWORD,URL);
  mqConnection = factory.createConnection();
  mqConnection.start();
  mqSession = mqConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  
  mqTopic = mqSession.createTopic(TOPIC_NAME);
  
  MessageProducer producer = mqSession.createProducer(mqTopic);
  
  TextMessage textMessage = mqSession.createTextMessage("eeeeeeeeeeee");
  
  producer.send(textMessage);
  System.out.println("eweerwerewew");
  mqConnection.close();
 }

}

 

 

 

4.订阅者
public class XuTopicSubsribeA implements MessageListener{

 private String url = "tcp://localhost:61616";  
 private String user = ActiveMQConnection.DEFAULT_USER;  
 private String password = ActiveMQConnection.DEFAULT_PASSWORD;  
 private String topicName = "my_topic";

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  XuTopicSubsribeA xa = null;
  xa = new XuTopicSubsribeA();
  try{
   xa.run();
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 
 private void run() throws JMSException{  
  ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(user,password,url);  
  Connection connection = factory.createConnection();  
  connection.start();  
        
  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
  Destination destination = session.createTopic(topicName);  
    
  MessageConsumer consumer = session.createConsumer(destination);  
           
  consumer.setMessageListener(this);  
  }


 @Override
 public void onMessage(Message message) {
  // TODO Auto-generated method stub
  if (message instanceof TextMessage) {  
          
   TextMessage txtMsg = (TextMessage) message;  
   String msg = "";  
   try {  
      msg = txtMsg.getText();  
      System.out.println("Received: " + msg);  
   } catch (JMSException e) {  
      e.printStackTrace();  
   }   
                 
  }

 }
 

}

 

二 、ActiveMQ5.0实战二 基本配置
简介
上一篇http://www.iteye.com/topic/15317介绍了ActiveMQ5.0的安装,这一篇将介绍的配置。ActiveMQ包含了很多features(详见

http://activemq.apache.org/features.html ),  
不同的需求,不同的环境,需要不同的features,当然需要不同的配置。在这里我只写了最基本的配置,算是抛砖了,希望引出更多关于ActiveMQ的

高级配置。
假设已经正确安装ActiveMQ5.0,同时及其IP地址为192.168.1.148,具体使用时可以改为自己的IP。下面讲解的配置实现的features如下: 客

户端可以通过tcp://192.168.1.148连接ActiveMQ。 消息持久化保存,重启服务器不会丢失消息。 可以通过

http://192.168.1.148:8161/admin监控ActiveMQ服务器
配置
ActiveMQ默认使用的是XML格式配置,从4.0版本开始用MBean的方式实现XML配置,配置文件在${activemq.home}/conf目录下,文件名为activemq.xml

。最新的默认配置见
http://svn.apache.org/repos/asf/activemq/trunk/assembly/src/release/conf/activemq.xml 。下面为本篇文章使用的配置,及重要部分的解释


 
Xml代码    
 <beans 
   xmlns="http://www.springframework.org/schema/beans
   xmlns:amq="http://activemq.org/config/1.0
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.springframework.org/schema/beans  

  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
   http://activemq.org/config/1.0 http://activemq.apache.org/schema/activemq-core.xsd  
   http://activemq.apache.org/camel/schema/spring
    
   <!-- persistent="true"表示要持久化存储消息,和子元素persistenceAdapter结合使用 --> 
   <!-- dataDirectory默认的存储持久化数据的目录 --> 
   <!-- brokerName 设置broker的name,在注意在网络上必须是唯一的--> 
   <!-- 更多参考http://activemq.apache.org/xbean-xml-reference-50.html#XBeanXMLReference5.0-brokerelement --> 
   <broker xmlns="http://activemq.org/config/1.0" brokerName="192.168.1.148" persistent ="true"   

dataDirectory="${activemq.base}/data" useShutdownHook="false"> 
    
     <!-- Destination specific policies using destination names or wildcards --> 
     <!-- wildcards意义见http://activemq.apache.org/wildcards.html --> 
     <destinationPolicy> 
       <policyMap> 
         <policyEntries> 
        <!-- 这里使用了wildcards,表示所有以EUCITA开头的topic --> 
           <policyEntry topic="EUCITA.>" producerFlowControl="false" memoryLimit="10mb"> 
             <!-- 分发策略 --> 
         <dispatchPolicy> 
           <!-- 按顺序分发 --> 
               <strictOrderDispatchPolicy/> 
             </dispatchPolicy> 
         <!--  恢复策略--> 
             <subscriptionRecoveryPolicy> 
           <!-- 只恢复最后一个message --> 
               <lastImageSubscriptionRecoveryPolicy/> 
             </subscriptionRecoveryPolicy> 
           </policyEntry> 
         </policyEntries> 
       </policyMap> 
     </destinationPolicy> 
  
     <!-- The transport connectors ActiveMQ will listen to --> 
     <transportConnectors> 
        <transportConnector name="openwire" uri="tcp://192.168.1.148:61616" discoveryUri="multicast://default"/> 
        <transportConnector name="ssl"     uri="ssl://192.168.1.148:61617"/> 
        <transportConnector name="stomp"   uri="stomp://192.168.1.148:61613"/> 
        <transportConnector name="xmpp"    uri="xmpp://192.168.1.148:61222"/> 
     </transportConnectors> 
      
     <!-- 消息持久化方式 --> 
     <persistenceAdapter> 
       <amqPersistenceAdapter directory="${activemq.base}/data"/> 
     </persistenceAdapter> 
 </broker> 
  
   <!-- lets create a command agent to respond to message based admin commands on the ActiveMQ.Agent topic --> 
     <commandAgent xmlns="
     
   <!-- An embedded servlet engine for serving up the Admin console --> 
   <jetty xmlns="
http://mortbay.com/schemas/jetty/1.0"> 
     <connectors> 
       <nioConnector port="8161" /> 
     </connectors> 
  
     <handlers> 
       <webAppContext contextPath="/admin" resourceBase="${activemq.base}/webapps/admin" logUrlOnStart="true" />       
       <webAppContext contextPath="/demo" resourceBase="${activemq.base}/webapps/demo" logUrlOnStart="true" />         
     </handlers> 
   </jetty>   
 </beans> 
注释
关于XML配置中元素的具体信息可以参考http://activemq.apache.org/xbean-xml-reference-50.html 下面介绍本篇配置使用的一些重要元素。
DispathPolicy
ActiveMQ支持3中不同的分发策略(避免翻译了以后误解,这里用原文):
 <roundRobinDispatchPolicy>:Simple dispatch policy that sends a message to every subscription that matches the message.
 <simpleDispatchPolicy>:Simple dispatch policy that sends a message to every subscription that matches the message.
 <strictOrderDispatchPolicy>:Dispatch policy that causes every subscription to see messages in the same order.
SubscriptionRecoveryPolicy
ActiveMQ支持6种恢复策略,可以自行选择使用不同的策略
 <fixedCountSubscriptionRecoveryPolicy>: keep a fixed count of last messages.
 <fixedSizedSubscriptionRecoveryPolicy>: keep a fixed amount of memory available in RAM for message history which is

evicted in time order.
 <lastImageSubscriptionRecoveryPolicy>:only keep the last message.
 <noSubscriptionRecoveryPolicy>:disable recovery of messages.
 <queryBasedSubscriptionRecoveryPolicy>:perform a user specific query mechanism to load any messages they may have missed.
 <timedSubscriptionRecoveryPolicy>:keep a timed buffer of messages around in memory and use that to recover new

subscriptions.
PersistenceAdapter
http://activemq.apache.org/persistence 讲解了关于persistence的信息。ActiveMQ5.0使用AMQ Message Store 持久化消息,这种方式提供了很好

的性能(The AMQ Message Store is an embeddable transactional message storage solution that is extremely fast and reliable.) 默认使

用该存储方式即可,如果想使用JDBC来存储,可以查找文档配置。
Summary
本篇文章只提供了基本配置信息。如果需要更多的文章,可以查看ActiveMQ的文档。
讲了安装和简单的配置,下一篇将介绍和Sping的整合,以及多个queue,多个topic,多个producer,多个consumer的配置,使用。

 

三、ActiveMQ5.0实战三使用Spring发送,消费topic和queue消息


实战一 , 实战二介绍了ActiveMQ的基本概念和配置方式.
本篇将通过一个实例介绍使用spring发送,消费topic, queue类型消息的方法. 不懂topic和queue的google 之.
 
 
如图示, TOPIC和QUEUE分别代表一个topic和一个queue消息通道.
 TopicMessageProducer向topic发送消息, TopicConsumerA和TopicConsumerB则从topic消费消息.
 QueueMessageProducer向Queue发送消息, QueueConsumer从Queue中消费消息

Spring整合JMS

就像对orm, web的支持一样, spring同样支持jms, 为整合jms到已有的项目提供了很多便利的方法. 本篇主要讲实战, 是所以先从配置开始, spring

配置jms基本上需要8个部分.
 1 ConnectionFactory. 和jms服务器的连接, 可以是外部的jms server, 也可以使用embedded ActiveMQ Broker.
 2 Destination. 有topic和queue两种方式.
 3 JmsTemplate. spring提供的jms模板.
 4 MessageConverter. 消息转换器.
 5 MessageProducer. 消息生产者.
 6 MessageConsumer. 消息消费者.
 7 MessageListener. 消息监听器
 8 MessageListenerContainer. 消息监听容器
下面以实例的方式介绍上面8个部分.
1. ConnectionFactory
Xml代码    
<amq:connectionFactory id="jmsConnectionFactory" brokerURL="vm://localhost" /> 
 brokerURL是指要连接的activeMQ server的地址, activeMQ提供了多种brokerURL, 集体可参见文档.一般我们使用嵌套的ActiveMQ server. 配置如

下, 这个配置使用消息的存储机制, 服务器重启也不会丢失消息.
Xml代码    
 <!--  embedded ActiveMQ Broker --> 
     <amq:broker useJmx="false" persistent="true"> 
         <amq:persistenceAdapter> 
             <amq:amqPersistenceAdapter directory="d:/amq"/> 
         </amq:persistenceAdapter> 
         <amq:transportConnectors> 
             <amq:transportConnector uri="tcp://localhost:61616" /> 
                        <amq:transportConnector uri="vm://localhost:0" /> 
         </amq:transportConnectors> 
     </amq:broker> 
2. Destination
 在实例中我们使用了两种destination
Xml代码    
 <!--  ActiveMQ destinations  --> 
 <!--  使用topic方式--> 
 <amq:topic name="TOPIC" physicalName="JMS-TEST-TOPIC" /> 
 <!--  使用Queue方式--> 
 <amq:queue name="QUEUE" physicalName="JMS-TEST-QUEUE" /> 
3. JmsTemplate
Xml代码    
 <!--  Spring JmsTemplate config --> 
     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> 
         <property name="connectionFactory"> 
             <!--  lets wrap in a pool to avoid creating a connection per send --> 
             <bean class="org.springframework.jms.connection.SingleConnectionFactory"> 
                 <property name="targetConnectionFactory" ref="jmsConnectionFactory" /> 
             </bean> 
         </property> 
         <!-- custom MessageConverter --> 
         <property name="messageConverter" ref="defaultMessageConverter" /> 
     </bean> 
4. MessageConverter
   MessageConverter实现的是org.springframework.jms.support.converter.MessageConverter接口, 提供消息的转换功能.

DefaultMessageConverter的实现见附件.
Xml代码    
<bean id="defaultMessageConverter" class="com.andyao.activemq.DefaultMessageConverter" /> 
 
5. MessageProducer
   实例拥有两个消息生产者, 消息生产者都是POJO, 实现见附件.
Xml代码    
 <!-- POJO which send Message uses  Spring JmsTemplate --> 
     <bean id="topicMessageProducer" class="com.andyao.activemq.TopicMessageProducer"> 
         <property name="template" ref="jmsTemplate" /> 
         <property name="destination" ref="TOPIC" /> 
     </bean> 
     <bean id="queueMessageProducer" class="com.andyao.activemq.QueuMessageProducer"> 
         <property name="template" ref="jmsTemplate" /> 
         <property name="destination" ref="QUEUE" /> 
     </bean> 
6. MessageConsumer
 TOPIC通道有两个消息消费者, QUEUE有一个消息消费者
Xml代码    
 <!--  Message Driven POJO (MDP) --> 
     <!-- consumer1 for topic a --> 
     <bean id="topicConsumerA" class="com.andyao.activemq.TopicConsumerA" /> 
     <!-- consumer2 for topic a --> 
     <bean id="topicConsumerB" class="com.andyao.activemq.TopicConsumerB" /> 
     <!-- consumer for queue --> 
     <bean id="queueConsumer" class="com.andyao.activemq.QueueConsumer" /> 
7. MessageListener
每一个消息消费者都对应一个MessageListener
Xml代码    
 <bean id="topicListenerA" class="org.springframework.jms.listener.adapter.MessageListenerAdapter"> 
         <constructor-arg ref="topicConsumerA" /> 
         <!--  may be other method --> 
         <property name="defaultListenerMethod" value="receive" /> 
         <!-- custom MessageConverter define --> 
         <property name="messageConverter" ref="defaultMessageConverter" /> 
     </bean> 
  
     <bean id="topicListenerB" class="org.springframework.jms.listener.adapter.MessageListenerAdapter"> 
         <constructor-arg ref="topicConsumerB" /> 
         <!--  may be other method --> 
         <property name="defaultListenerMethod" value="receive" /> 
         <!-- custom MessageConverter define --> 
         <property name="messageConverter" ref="defaultMessageConverter" /> 
     </bean> 
  
     <bean id="queueListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter"> 
         <constructor-arg ref="queueConsumer" /> 
         <!--  may be other method --> 
         <property name="defaultListenerMethod" value="receive" /> 
         <!-- custom MessageConverter define --> 
         <property name="messageConverter" ref="defaultMessageConverter" /> 
     </bean> 
8. MessageListenerContainer
 有几个MessageListener既有几个MessageListenerContainer
Xml代码    
 <bean id="topicListenerContainerA" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 
         <property name="connectionFactory" ref="jmsConnectionFactory" /> 
         <property name="destination" ref="TOPIC" /> 
         <property name="messageListener" ref="topicListenerA" /> 
     </bean> 
  
     <bean id="topicListenerContainerB" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 
         <property name="connectionFactory" ref="jmsConnectionFactory" /> 
         <property name="destination" ref="TOPIC" /> 
         <property name="messageListener" ref="topicListenerB" /> 
     </bean> 
       
     <bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 
         <property name="connectionFactory" ref="jmsConnectionFactory" /> 
         <property name="destination" ref="QUEUE" /> 
         <property name="messageListener" ref="queueListener" /> 
     </bean> 
Summary
写spring配置文件的时候, 要把MessageProducer, MessageConsumer,MessageListener,MessageListenerContainer几个地方弄清楚:
1. 可以有一个或者多个消息生产者向同一个destination发送消息.
2. queue类型的只能有一个消息消费者.
3. topic类型的可以有多个消息消费者.
4. 每个消费者对应一个MessageListener和一个MessageListenerContainer.

三、activemq5.5的入门包括安装启动数据库持久化

ActiveMQ的配置文件在/activemq/conf目录下面。主要配置文件为activemq.xml。
 
如何在数据库持久化
1.屏蔽硬盘读取方式 修改成jdbc数据库
Xml代码    
1. <!--  
2.         <persistenceAdapter> 
3.             <kahaDB directory="${activemq.base}/data/kahadb"/> 
4.         </persistenceAdapter> 
5.         --> 
6.     <persistenceAdapter> 
7.          <jdbcPersistenceAdapter dataSource="#oracle-ds"/> 
8.     </persistenceAdapter> 
 2.在</beans>的上面建立数据源信息
Java代码    
1. <bean id="oracle-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
2. <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>  
3. <property name="url" value="jdbc:oracle:thin:@127.0.0.1:oracle"/>  
4. <property name="username" value="admin"/>  
5. <property name="password" value="admin"/>  
6. <property name="maxActive" value="200"/>  
7. <property name="poolPreparedStatements" value="true"/>  
8. ;/bean> 
 3.然后启动activemq.bat即可在数据库中看到几张active开头的表,一共3张。
 
明天抽空再写下和spring中的结合以及消息的发送和消费。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值