[转帖]在Spring中使用JMS_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3388 | 回复: 0   主题: [转帖]在Spring中使用JMS        下一篇 
dwx8023
注册用户
等级:上尉
经验:612
发帖:114
精华:0
注册:2011-11-8
状态:离线
发送短消息息给dwx8023 加好友    发送短消息息给dwx8023 发消息
发表于: IP:您无权察看 2011-11-14 16:10:41 | [全部帖] [楼主帖] 楼主

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.      < bean   id = "jmsTemplate"    
  18.          class = "org.springframework.jms.core.JmsTemplate" >    
  19.          < property   name = "connectionFactory" >    
  20.              < bean    
  21.                  class = "org.springframework.jms.connection.SingleConnectionFactory" >    
  22.                  < property   name = "targetConnectionFactory"    
  23.                      ref = "jmsConnectionFactory"   />    
  24.              bean >    
  25.          property >    
  26.      bean >    
  27.      <!----> <!---->  
  28.      < bean   id = "messageProducer"    
  29.          class = "jms.MessageProducer" >    
  30.          < property   name = "template"   ref = "jmsTemplate"   />    
  31.          < property   name = "destination"   ref = "destination"   />    
  32.      bean >    
  33. beans >    



java 代码

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


发送方调用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.      < bean   id = "messageListener"    
  18.          class = "jms.MessageConsumer" >    
  19.          < property   name = "worksheetService"   ref = "worksheetService" > property >    
  20.     < bean >    
  21.      <!---->   
  22.      < bean   id = "listenerContainer"    
  23.          class = "org.springframework.jms.listener.DefaultMessageListenerContainer" >    
  24.          < property   name = "connectionFactory"   ref = "jmsConnectionFactory"   />    
  25.          < property   name = "destination"   ref = "destination"   />    
  26.          < property   name = "messageListener"   ref = "messageListener"   />    
  27.     < bean >    
  28. < beans >    



java 代码

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


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




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论