生活随笔
收集整理的這篇文章主要介紹了
activeMQ,spring的jmstemplate简单例子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.使用的是maven的結構,導入activeMQ的包
[html]?view plain
?copy<dependency>????????????<groupId>org.apache.activemq</groupId>????????????<artifactId>activemq-core</artifactId>????????????<version>5.5.0</version>??</dependency>???
2.創建“生產者”
①xml配置文件
[html]?view plain
?copy??????<bean?id="connectionFactory"?class="org.apache.activemq.ActiveMQConnectionFactory">??????????<property?name="brokerURL"?value="tcp://localhost:61616"?/>??????</bean>????????????????????????<bean?id="rantzDestination"?class="org.apache.activemq.command.ActiveMQQueue">??????????<constructor-arg?index="0"?value="rantz.marketing.queue"></constructor-arg>??????</bean>????????????????????????????????????????????????<bean?id="jmsTemplate"?class="org.springframework.jms.core.JmsTemplate">??????????<property?name="connectionFactory"?ref="connectionFactory"?/>??????</bean>????????????<bean?id="marketingGateway"?class="com.jms.service.RantzMarketingGatewayImpl">??????????<property?name="jmsTemplate"?ref="jmsTemplate"?/>??????????<property?name="destination"?ref="rantzDestination"?/>??????</bean>??
②java代碼
主類SpringMain
[java]?view plain
?copypublic?static?void?main(String[]?args)?{????????????????????????ApplicationContext?context?=?new?ClassPathXmlApplicationContext("classpath:applicationContext.xml");??????????????IRantzMarketingGateway?rantzMarketingGateway=?(RantzMarketingGatewayImpl)?context.getBean("marketingGateway");??????????????rantzMarketingGateway.sendMotoristInfo();??????????????System.out.println("Start?...");??????????}??
消息發送類RantzMarketingGatewayImpl
[java]?view plain
?copy??????????????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;??public?class?RantzMarketingGatewayImpl?implements?IRantzMarketingGateway?{????????????private?JmsTemplate?jmsTemplate;??????private?Destination?destination;????????????public?JmsTemplate?getJmsTemplate()?{??????????return?jmsTemplate;??????}??????public?void?setJmsTemplate(JmsTemplate?jmsTemplate)?{??????????this.jmsTemplate?=?jmsTemplate;??????}??????public?Destination?getDestination()?{??????????return?destination;??????}??????public?void?setDestination(Destination?destination)?{??????????this.destination?=?destination;??????}????????????public?void?sendMotoristInfo(){??????????jmsTemplate.send(??????????????destination,??????????????new?MessageCreator(){??????????????????public?Message?createMessage(Session?session)?throws?JMSException?{??????????????????????return?session.createTextMessage("這是一個測試");??????????????????}??????????????}??????????);??????}??}??
?
3.創建“消費者”
①xml配置文件
[html]?view plain
?copy??<bean?id="connectionFactory"?class="org.apache.activemq.ActiveMQConnectionFactory">??????<property?name="brokerURL"?value="tcp://localhost:61616"?/>??</bean>??????<bean?id="rantzDestination"?class="org.apache.activemq.command.ActiveMQQueue">??????<constructor-arg?index="0"?value="rantz.marketing.queue"></constructor-arg>??</bean>????????????<bean?id="jmsTemplate"?class="org.springframework.jms.core.JmsTemplate">??????<property?name="connectionFactory"?ref="connectionFactory"?/>??????<property?name="defaultDestination"?ref="rantzDestination"?/>??</bean>????<bean?id="marketingGateway"?class="com.huateng.jms.service.MarketingReceiverGatewayImpl">??????<property?name="jmsTemplate"?ref="jmsTemplate"?/>??</bean>??
②java代碼
主類SpringMain
[java]?view plain
?copypublic?static?void?main(String[]?args)?{????????????????ApplicationContext?context?=?new?ClassPathXmlApplicationContext("classpath:applicationContext.xml");??????????MarketingReceiverGatewayImpl?rantzMarketingGateway=?(MarketingReceiverGatewayImpl)?context.getBean("marketingGateway");??????????System.out.println("Receive?Start?...");??????????try?{??????????????rantzMarketingGateway.receiveMotorist();??????????}?catch?(Exception?e)?{??????????????e.printStackTrace();??????????}??????}??
消息發送類MarketingReceiverGatewayImpl
[java]?view plain
?copyimport?javax.jms.TextMessage;??import?org.springframework.jms.core.JmsTemplate;??public?class?MarketingReceiverGatewayImpl?{????????????private?JmsTemplate?jmsTemplate;????????????public?JmsTemplate?getJmsTemplate()?{??????????return?jmsTemplate;??????}??????public?void?setJmsTemplate(JmsTemplate?jmsTemplate)?{??????????this.jmsTemplate?=?jmsTemplate;??????}????????????public?MarketingReceiverGatewayImpl()?{??????}????????????public?void?receiveMotorist()?throws?Exception{??????????TextMessage?message??=?(TextMessage)jmsTemplate.receive();??????????System.out.println("reviced?msg?is:"?+?message.getText());??????}????????}??
?
4.啟動activeQM
在下載的activeQM包中的bin目錄中,可以找到一個用于啟動activeMQ的腳本。unix用戶使用activemq;windows用戶使用activemq.bat
5.先運行"生產者",再運行"消費者"
6.結果
?
Start ...
Receive Start ...
?
reviced msg is:這是一個測試
?
總結
以上是生活随笔為你收集整理的activeMQ,spring的jmstemplate简单例子的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。