2019獨角獸企業重金招聘Python工程師標準>>>
Maven dependency
maven的dependency用著 隱形的依賴傳遞性,如果只是用到 JMS這部分功能 引用一下Maven坐標即可
<dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>${project.dependency.spring.core}</version>
</dependency>
依賴傳遞關系,可見SpringJMS 會隱形的導入其他的依賴包
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 最基本的需要構建2個類, 一個是JMS Template 一個是JMS 客戶端
構建JMS 客戶端類
使用@Service 注解將 類注冊到spring 容器中, 使用Autowire注解 自動裝填 JMS Template。
定義 JmsTemplate的setter方法 主要是為了解耦,脫離spring 容器的時候 需要自行set 一個 JMS template 實例
@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 是可選項,后面后續的系列會提到。
這里使用了 spring的 cacheConnectionFactory去池化connection。
最終 我們需要向spring 提供 2個實現類 分別是? 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 供應, 使用 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"?/>
如果需要到 自己定義實現類 需要額外定義 connectFactory 的實際類(各供應商可能各不相同),這里以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>
測試
簡單測試 發送消息到隊列
準備工作需要 引入依賴包 ActiveMq ,Spring-test 和 開啟 JMS服務器 。
參考: 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();}
}
轉載于:https://my.oschina.net/u/1041012/blog/475202
總結
以上是生活随笔為你收集整理的SpringFramework4系列之SpringJMS:(一)搭建JMS-注解加XML版的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。