论坛首页 Java企业应用论坛

在Spring中使用JMS

浏览 22372 次
精华帖 (1) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-07-05   最后修改:2010-08-18

什么是JMS?

JMS即java消息服务,JMS通过消息的形式来降低组件之间的耦合度。

JMS由两部分组成消息发送者消息监听者

 

JMS的运用场景?

   用户系统负责维护用户信息,文档系统负责维护文档信息,但是当用户删除的时候,需要将他所撰写的文档信息也删除的时候,在用户管理模块调用文档管理模块的接口,会造成用户模块和业务模块紧耦合。

   这个时候可以使用JMS技术来将紧耦合转化为松耦合,具体做法是用户系统在删除,修改用户的时候往JMS服务器发送更新消息,又业务系统监听这些消息,然后按照自己的业务逻辑来进行相应的处理。

   即组件A做了一件事情往消息服务器上发送了一个通知,组件B监听到了消息,处理自己的业务逻辑。

 

详细步骤:

1:配置消息服务器:配置JMS需要两个对象connectionFactory和 destination。

connectionFactory使用jboss自带的TopicConnectionFactory。

destination可以使用自定义的。

kiral-jms-service.xml   注意:文件名称一定要是-service.xml结尾。这个文件放在部署目录下。

 

xml 代码

 

  1. <!---->< xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < server >   
  3.    < mbean   code = "org.jboss.mq.server.jmx.Topic"   
  4.       name = "jboss.mq.destination:service=Topic,name=kiralJms" >   
  5.      < depends   optional-attribute-name = "DestinationManager" > jboss.mq:service = DestinationManager depends >   
  6.      < depends   optional-attribute-name = "SecurityManager" > jboss.mq:service = SecurityManager depends >   
  7.      < attribute   name = "SecurityConf" >   
  8.        < security >   
  9.          < role   name = "guest"   read = "true"   write = "true" />   
  10.          < role   name = "publisher"   read = "true"   write = "true"   create = "false" />   
  11.          < role   name = "durpublisher"   read = "true"   write = "true"   create = "true" />   
  12.        security >   
  13.      attribute >   
  14.    mbean >   
  15.   server >    

2:配置发送消息端

bean-jms.xml

xml 代码
  1. <!---->xml   version = "1.0"   encoding = "GB2312" ?>  
  2. < beans >   
  3.      < bean   id = "jmsConnectionFactory"   
  4.          class = "org.springframework.jndi.JndiObjectFactoryBean" >   
  5.          < property   name = "jndiName" >   
  6.              < value > TopicConnectionFactory value >   
  7.          property >   
  8.      bean >   
  9.        
  10.      < bean   id = "destination"   
  11.          class = "org.springframework.jndi.JndiObjectFactoryBean" >   
  12.          < property   name = "jndiName" >   
  13.              < value > topic/kiralJms value >   
  14.          property >   
  15.      bean >   
  16.   
  17.      <!---->  
  18.      < bean   id = "jmsTemplate"   
  19.          class = "org.springframework.jms.core.JmsTemplate" >   
  20.          < property   name = "connectionFactory" >   
  21.              < bean   
  22.                  class = "org.springframework.jms.connection.SingleConnectionFactory" >   
  23.                  < property   name = "targetConnectionFactory"   
  24.                      ref = "jmsConnectionFactory"   />   
  25.              bean >   
  26.          property >   
  27.      bean >   
  28.   
  29.      <!----> <!----> 
  30.      < bean   id = "messageProducer"   
  31.          class = "jms.MessageProducer" >   
  32.          < property   name = "template"   ref = "jmsTemplate"   />   
  33.          < property   name = "destination"   ref = "destination"   />   
  34.      bean >   
  35. beans >   

 

java 代码
  1. import  javax.jms.Destination;   
  2. import  javax.jms.JMSException;   
  3. import  javax.jms.Message;   
  4. import  javax.jms.Session;   
  5.   
  6. import  org.springframework.jms.core.JmsTemplate;   
  7. import  org.springframework.jms.core.MessageCreator;   
  8.   
  9. /***********************************************************  
  10.  * 消息发送者  
  11.  *   
  12.  * @作者:kiral  
  13.  * @日期:2007-7-3  
  14.  **********************************************************/   
  15. public   class  MessageProducer {   
  16.   
  17.      public   void  send( final  String message) {   
  18.         template.send(destination,  new  MessageCreator() {   
  19.              public  Message createMessage(Session session)  throws  JMSException {   
  20.                 Message m = session.createTextMessage(message);   
  21.                  return  m;   
  22.             }   
  23.         });   
  24.     }   
  25.   
  26.      private  JmsTemplate template;   
  27.   
  28.      private  Destination destination;   
  29.   
  30.      public   void  setDestination(Destination destination) {   
  31.          this .destination = destination;   
  32.     }   
  33.   
  34.      public   void  setTemplate(JmsTemplate template) {   
  35.          this .template = template;   
  36.     }   
  37.   
  38. }  

发送方调用send方法发送消息。

 

3:配置消息接收者

xml 代码
  1. <!---->< xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. <!---->  
  3. < beans >   
  4.      < bean   id = "jmsConnectionFactory"   
  5.          class = "org.springframework.jndi.JndiObjectFactoryBean" >   
  6.          < property   name = "jndiName" >   
  7.              < value > TopicConnectionFactory value >   
  8.          property >   
  9.     < bean >   
  10.      < bean   id = "destination"   
  11.          class = "org.springframework.jndi.JndiObjectFactoryBean" >   
  12.          < property   name = "jndiName" >   
  13.              < value > topic/kiralJms value >   
  14.          property >   
  15.     < bean >   
  16.   
  17.      <!---->  
  18.      < bean   id = "messageListener"   
  19.          class = "jms.MessageConsumer" >   
  20.          < property   name = "worksheetService"   ref = "worksheetService" > property >   
  21.     < bean >   
  22.   
  23.      <!---->  
  24.      < bean   id = "listenerContainer"   
  25.          class = "org.springframework.jms.listener.DefaultMessageListenerContainer" >   
  26.          < property   name = "connectionFactory"   ref = "jmsConnectionFactory"   />   
  27.          < property   name = "destination"   ref = "destination"   />   
  28.          < property   name = "messageListener"   ref = "messageListener"   />   
  29.     < bean >   
  30. < beans >   

 

java 代码
  1. import  javax.jms.Message;   
  2. import  javax.jms.MessageListener;   
  3.   
  4. import org.kiral.flow.service.WorksheetService;   
  5.   
  6. /*******************************************************************************  
  7.  * 消息接收者  
  8.  *   
  9.  * @作者:kiral  
  10.  * @日期:2007-7-3  
  11.  ******************************************************************************/   
  12. public   class  MessageConsumer  implements  MessageListener {   
  13.   
  14.      private  WorksheetService worksheetService;   
  15.   
  16.      public  WorksheetService getWorksheetService() {   
  17.          return  worksheetService;   
  18.     }   
  19.   
  20.      public   void  setWorksheetService(WorksheetService worksheetService) {   
  21.          this .worksheetService = worksheetService;   
  22.     }   
  23.   
  24.      public   void  onMessage(Message message) {   
  25.         System.out.println(message);   
  26.         worksheetService.updateRole();   
  27.     }   
  28.   
  29. }  

接受方一旦接收到消息,就会打印在控制台。

   发表时间:2007-09-03  
能否提供有消息发送时,及时打印到页面上的例子?
0 请登录后投票
   发表时间:2007-10-19  
我也用SPRING JMS + ACTIVEMQ做了异步收发的例子,但测试的时候,发现数据好不理想,发送端每秒也只能发50条的消息,接收服务器倒是没有性能上的问题,不知道是什么原因引起了.JMS的性能不会如此差吧.不知道谁有测试过的性能报告.....
0 请登录后投票
   发表时间:2007-10-22  
建议搞个打印到页面的例子
0 请登录后投票
   发表时间:2007-10-23  
对不起,现在工作忙,没有时间做这个例子
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics