日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

在Spring中使用JMS

發布時間:2024/4/17 javascript 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Spring中使用JMS 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?http://xiangtui.iteye.com/blog/978674

?

什么是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 代碼

?

  • <!---->< xml ? version = "1.0" ? encoding = "UTF-8" ?> ??
  • < server > ??
  • ?? < mbean ? code = "org.jboss.mq.server.jmx.Topic" ??
  • ????? name = "jboss.mq.destination:service=Topic,name=kiralJms" > ??
  • ???? < depends ? optional-attribute-name = "DestinationManager" > jboss.mq:service = DestinationManager depends > ??
  • ???? < depends ? optional-attribute-name = "SecurityManager" > jboss.mq:service = SecurityManager depends > ??
  • ???? < attribute ? name = "SecurityConf" > ??
  • ?????? < security > ??
  • ???????? < role ? name = "guest" ? read = "true" ? write = "true" /> ??
  • ???????? < role ? name = "publisher" ? read = "true" ? write = "true" ? create = "false" /> ??
  • ???????? < role ? name = "durpublisher" ? read = "true" ? write = "true" ? create = "true" /> ??
  • ?????? security > ??
  • ???? attribute > ??
  • ?? mbean > ??
  • ? server > ???
  • 2:配置發送消息端

    bean-jms.xml

    xml 代碼
  • <!---->xml ? version = "1.0" ? encoding = "GB2312" ?> ?
  • < beans > ??
  • ???? < bean ? id = "jmsConnectionFactory" ??
  • ???????? class = "org.springframework.jndi.JndiObjectFactoryBean" > ??
  • ???????? < property ? name = "jndiName" > ??
  • ???????????? < value > TopicConnectionFactory value > ??
  • ???????? property > ??
  • ???? bean > ??
  • ???? ??
  • ???? < bean ? id = "destination" ??
  • ???????? class = "org.springframework.jndi.JndiObjectFactoryBean" > ??
  • ???????? < property ? name = "jndiName" > ??
  • ???????????? < value > topic/kiralJms value > ??
  • ???????? property > ??
  • ???? bean > ??
  • ??
  • ???? <!----> ??
  • ???? < bean ? id = "jmsTemplate" ??
  • ???????? class = "org.springframework.jms.core.JmsTemplate" > ??
  • ???????? < property ? name = "connectionFactory" > ??
  • ???????????? < bean ??
  • ???????????????? class = "org.springframework.jms.connection.SingleConnectionFactory" > ??
  • ???????????????? < property ? name = "targetConnectionFactory" ??
  • ???????????????????? ref = "jmsConnectionFactory" ? /> ??
  • ???????????? bean > ??
  • ???????? property > ??
  • ???? bean > ??
  • ??
  • ???? <!----> ?<!---->?
  • ???? < bean ? id = "messageProducer" ??
  • ???????? class = "jms.MessageProducer" > ??
  • ???????? < property ? name = "template" ? ref = "jmsTemplate" ? /> ??
  • ???????? < property ? name = "destination" ? ref = "destination" ? /> ??
  • ???? bean > ??
  • beans > ??
  • ?

    java 代碼
  • import ?javax.jms.Destination; ??
  • import ?javax.jms.JMSException; ??
  • import ?javax.jms.Message; ??
  • import ?javax.jms.Session; ??
  • ??
  • import ?org.springframework.jms.core.JmsTemplate; ??
  • import ?org.springframework.jms.core.MessageCreator; ??
  • ??
  • /*********************************************************** ?
  • ?*?消息發送者 ?
  • ?*? ?
  • ?*?@作者:kiral ?
  • ?*?@日期:2007-7-3 ?
  • ?**********************************************************/ ??
  • public ? class ?MessageProducer?{ ??
  • ??
  • ???? public ? void ?send( final ?String?message)?{ ??
  • ????????template.send(destination,? new ?MessageCreator()?{ ??
  • ???????????? public ?Message?createMessage(Session?session)? throws ?JMSException?{ ??
  • ????????????????Message?m?=?session.createTextMessage(message); ??
  • ???????????????? return ?m; ??
  • ????????????} ??
  • ????????}); ??
  • ????} ??
  • ??
  • ???? private ?JmsTemplate?template; ??
  • ??
  • ???? private ?Destination?destination; ??
  • ??
  • ???? public ? void ?setDestination(Destination?destination)?{ ??
  • ???????? this .destination?=?destination; ??
  • ????} ??
  • ??
  • ???? public ? void ?setTemplate(JmsTemplate?template)?{ ??
  • ???????? this .template?=?template; ??
  • ????} ??
  • ??
  • }??
  • 發送方調用send方法發送消息。

    ?

    3:配置消息接收者

    xml 代碼
  • <!----> < xml ? version = "1.0" ? encoding = "UTF-8" ?> ??
  • <!----> ??
  • < beans > ??
  • ???? < bean ? id = "jmsConnectionFactory" ??
  • ???????? class = "org.springframework.jndi.JndiObjectFactoryBean" > ??
  • ???????? < property ? name = "jndiName" > ??
  • ???????????? < value > TopicConnectionFactory value > ??
  • ???????? property > ??
  • ????< bean > ??
  • ???? < bean ? id = "destination" ??
  • ???????? class = "org.springframework.jndi.JndiObjectFactoryBean" > ??
  • ???????? < property ? name = "jndiName" > ??
  • ???????????? < value > topic/kiralJms value > ??
  • ???????? property > ??
  • ????< bean > ??
  • ??
  • ???? <!----> ??
  • ???? < bean ? id = "messageListener" ??
  • ???????? class = "jms.MessageConsumer" > ??
  • ???????? < property ? name = "worksheetService" ? ref = "worksheetService" > property > ??
  • ????< bean > ??
  • ??
  • ???? <!----> ??
  • ???? < bean ? id = "listenerContainer" ??
  • ???????? class = "org.springframework.jms.listener.DefaultMessageListenerContainer" > ??
  • ???????? < property ? name = "connectionFactory" ? ref = "jmsConnectionFactory" ? /> ??
  • ???????? < property ? name = "destination" ? ref = "destination" ? /> ??
  • ???????? < property ? name = "messageListener" ? ref = "messageListener" ? /> ??
  • ????< bean > ??
  • < beans > ??
  • ?

    java 代碼
  • import ?javax.jms.Message; ??
  • import ?javax.jms.MessageListener; ??
  • ??
  • import org.kiral.flow.service.WorksheetService; ??
  • ??
  • /******************************************************************************* ?
  • ?*?消息接收者 ?
  • ?*? ?
  • ?*?@作者:kiral ?
  • ?*?@日期:2007-7-3 ?
  • ?******************************************************************************/ ??
  • public ? class ?MessageConsumer? implements ?MessageListener?{ ??
  • ??
  • ???? private ?WorksheetService?worksheetService; ??
  • ??
  • ???? public ?WorksheetService?getWorksheetService()?{ ??
  • ???????? return ?worksheetService; ??
  • ????} ??
  • ??
  • ???? public ? void ?setWorksheetService(WorksheetService?worksheetService)?{ ??
  • ???????? this .worksheetService?=?worksheetService; ??
  • ????} ??
  • ??
  • ???? public ? void ?onMessage(Message?message)?{ ??
  • ????????System.out.println(message); ??
  • ????????worksheetService.updateRole(); ??
  • ????} ??
  • ??
  • }??
  • 接受方一旦接收到消息,就會打印在控制臺。

    總結

    以上是生活随笔為你收集整理的在Spring中使用JMS的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。