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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

SpringFramework4系列之SpringJMS:(一)搭建JMS-注解加XML版

發(fā)布時(shí)間:2024/1/17 asp.net 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringFramework4系列之SpringJMS:(一)搭建JMS-注解加XML版 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

Maven dependency

maven的dependency用著 隱形的依賴傳遞性,如果只是用到 JMS這部分功能 引用一下Maven坐標(biāo)即可

<dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>${project.dependency.spring.core}</version> </dependency>

依賴傳遞關(guān)系,可見SpringJMS 會隱形的導(dǎo)入其他的依賴包

Spring Namespace

Spring-config.xml 支持 Spring-jms的命名空間,使用namespace 可以簡化spring的配置

<?xml?version="1.0"?encoding="UTF-8"?> <beans?xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:jms="http://www.springframework.org/schema/jms"xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans.xsd???http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd?????????http://www.springframework.org/schema/jms?http://www.springframework.org/schema/jms/spring-jms.xsd">????<!--?bean?definitions?here?--> </beans>

Spring beans

使用 Spring JMS 最基本的需要構(gòu)建2個(gè)類, 一個(gè)是JMS Template 一個(gè)是JMS 客戶端

  • JMS Template: 是Spring 自身提供,只需向Spring 容器內(nèi) 注冊這個(gè)類即可

  • JMS 客戶端 : 這個(gè)是需要自己編寫的, 會使用到JMS Template類,如果需要 spring 托管 也需要向容器內(nèi) 注冊

構(gòu)建JMS 客戶端類

使用@Service 注解將 類注冊到spring 容器中, 使用Autowire注解 自動裝填 JMS Template。

定義 JmsTemplate的setter方法 主要是為了解耦,脫離spring 容器的時(shí)候 需要自行set 一個(gè) JMS template 實(shí)例

@Service("JMSDemo") public?class?JMSDemo{????private?JmsTemplate?jmsTemplate;@Autowiredpublic?void?setJmsTempalte(JmsTemplate?jmsTemplate){this.jmsTemplate?=?jmsTemplate;}public?void?send(final?String?argQueueName,?final?String?argObject)?throws?JMSException?{jmsTemplate.send(argQueueName,?new?MessageCreator()?{public?Message?createMessage(Session?session)?throws?JMSException?{return?session.createObjectMessage(argObject);}});}public?Message?consumee(String?queueName)?throws?JMSException?{Message?message?=?jmsTemplate.receive(queueName);return?message;} }

Spring Config

spring的配置文件中 首先需要 componet-scan 去掃描package 將帶有@component @Service 等注解的類 注冊到spring的容器中。

<!--?===============================================?--> <!--?????????????component?Scanning??????????????????--> <!--?===============================================?--> <context:component-scan?base-package="com.*"/>

其次需要需要定義 Jms template Bean

Jms Template 需要額外配置 connectionFactory 和defaultDestination 屬性?? messageConverter 是可選項(xiàng),后面后續(xù)的系列會提到。

這里使用了 spring的 cacheConnectionFactory去池化connection。

最終 我們需要向spring 提供 2個(gè)實(shí)現(xiàn)類 分別是? connectionFacotry和defaultDestination

<!--?===============================================?--> <!--?????????????JMS?Template????????????????????????--> <!--?===============================================?--> <bean?id="jmsTemplate"?class="org.springframework.jms.core.JmsTemplate"><property?name="connectionFactory"?ref="cachingConnectionFactory"/><property?name="defaultDestination"?ref="jmsDestination"/><property?name="messageConverter"><bean?class="org.springframework.jms.support.converter.SimpleMessageConverter"/></property> </bean> <!--?Pooled?Spring?connection?factory?--> <bean?id="cachingConnectionFactory"?class="org.springframework.jms.connection.CachingConnectionFactory"><property?name="targetConnectionFactory"?ref="jmsConnectionFactory"?/> </bean>


如果需要到 JNID 里面去尋找 JMS 供應(yīng), 使用 jee:jndi-lookup 去尋找即可

前提是 需要加上 jee 的name space

xmlns:jee="? xsi:schemaLocation=" http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd" <jee:jndi-lookup?id="jmsConnectionFactory"?jndi-name="amqConnectionFactory"?/> <jee:jndi-lookup?id="jmsDestination"?jndi-name="amqDestination"?/>

如果需要到 自己定義實(shí)現(xiàn)類 需要額外定義 connectFactory 的實(shí)際類(各供應(yīng)商可能各不相同),這里以ActiveMQ為例

<bean?id="amqConnectionFactory"?class="org.apache.activemq.ActiveMQConnectionFactory"><!--?brokerURL,?You?may?have?different?IP?or?port?--><property?name="brokerURL"?value="tcp://localhost:61616"?/> </bean> <bean?id="defaultDestination"?class="org.apache.activemq.command.ActiveMQQueue"><!--<property?name="compositeDestinations"?value="testQueue"/>--><constructor-arg?index="0"?value="testQueue"?/> </bean>

測試

簡單測試 發(fā)送消息到隊(duì)列

準(zhǔn)備工作需要 引入依賴包 ActiveMq ,Spring-test 和 開啟 JMS服務(wù)器 。

參考: ActiveMQ get Started

<dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId><version>${project.dependency.apache.activemq}</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${project.dependency.spring.core}</version><scope>test</scope> </dependency> public?static?void?main(String[]?arg){ConnectionFactory?connectionFactory?=?new?ActiveMQConnectionFactory("tcp://localhost:61616");//?creates?an?JNDI?Context?and?combine?resourcesSimpleNamingContextBuilder?builder?=?null;try?{builder?=?SimpleNamingContextBuilder.emptyActivatedContextBuilder();}?catch?(NamingException?e)?{e.printStackTrace();}builder.bind("amqConnectionFactory",?connectionFactory);builder.bind("amqDestination",?new?ActiveMQQueue("testQueue")?);//?Initialize?Spring?ContextApplicationContext?context?=?new?ClassPathXmlApplicationContext("spring-config.xml");JMSDemo?jmsDemo?=?context.getBean(JMSDemo.class);try?{jmsDemo.send("testQueue","Test");}?catch?(JMSException?e)?{e.printStackTrace();} }


轉(zhuǎn)載于:https://my.oschina.net/u/1041012/blog/475202

總結(jié)

以上是生活随笔為你收集整理的SpringFramework4系列之SpringJMS:(一)搭建JMS-注解加XML版的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。