码迷,mamicode.com
首页 > 编程语言 > 详细

【ActiveMQ入门-9】ActiveMQ学习-与Spring集成2

时间:2015-04-30 12:13:44      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

概述:

下面将介绍如何在Spring下集成ActiveMQ
消费者:同步接收;
目的地:Queue

技术分享

环境:

共5个文件
  1. Receiver.java
  2. ReceiverTest.java
  3. Sender.java
  4. SenderTest.java
  5. applicationContext.xml
技术分享
使用的jar包如下:
技术分享


源文件:

Receiver.java
  1. package com.ll.springActiveMQ2;
  2. import javax.jms.JMSException;
  3. import javax.jms.MapMessage;
  4. import org.springframework.jms.core.JmsTemplate;
  5. import org.springframework.jms.support.JmsUtils;
  6. public class Receiver {
  7. private JmsTemplate jmsTemplate;
  8. public JmsTemplate getJmsTemplate() {
  9. return jmsTemplate;
  10. }
  11. public void setJmsTemplate(JmsTemplate jmsTemplate) {
  12. this.jmsTemplate = jmsTemplate;
  13. }
  14. /**
  15. * 构造函数
  16. */
  17. public Receiver() {
  18. }
  19. public String receiveMessage() {
  20. String my = "";
  21. MapMessage message = (MapMessage) jmsTemplate.receive();
  22. try {
  23. my = message.getString("lastname");
  24. } catch (JMSException e) {
  25. throw JmsUtils.convertJmsAccessException(e);
  26. }
  27. return my;
  28. }
  29. }

ReceiverTest.java

  1. package com.ll.springActiveMQ2;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class ReceiverTest {
  5. public static void main(String[] args) {
  6. ApplicationContext context = new ClassPathXmlApplicationContext(
  7. "applicationContext.xml");
  8. Receiver receiver = (Receiver) context.getBean("receiver");
  9. System.out.print("接收到消息:" + receiver.receiveMessage());
  10. }
  11. }

Sender.java

  1. package com.ll.springActiveMQ2;
  2. import javax.jms.JMSException;
  3. import javax.jms.MapMessage;
  4. import javax.jms.Message;
  5. import javax.jms.Session;
  6. import org.springframework.jms.core.JmsTemplate;
  7. import org.springframework.jms.core.MessageCreator;
  8. public class Sender {
  9. private JmsTemplate jmsTemplate;
  10. public JmsTemplate getJmsTemplate() {
  11. return jmsTemplate;
  12. }
  13. public void setJmsTemplate(JmsTemplate jmsTemplate) {
  14. this.jmsTemplate = jmsTemplate;
  15. }
  16. public void sendInfo() {
  17. jmsTemplate.send(new MessageCreator() {
  18. public Message createMessage(Session session) throws JMSException {
  19. MapMessage message = session.createMapMessage();
  20. message.setString("lastname", "科比");
  21. return message;
  22. }
  23. });
  24. }
  25. }

SenderTest.java

  1. package com.ll.springActiveMQ2;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class SenderTest {
  5. public static void main(String[] args) {
  6. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  7. Sender sender = (Sender) context.getBean("sender");
  8. sender.sendInfo();
  9. System.out.println("消息已发送...");
  10. }
  11. }


配置文件:

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
  6. <!--创建连接工厂 -->
  7. <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
  8. <property name="brokerURL" value="tcp://localhost:61616"></property>
  9. </bean>
  10. <!-- 声明ActiveMQ消息目标,目标可以是一个队列,也可以是一个主题ActiveMQTopic -->
  11. <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
  12. <constructor-arg index="0" value="com.ll.springActiveMQ2"></constructor-arg>
  13. </bean>
  14. <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
  15. <property name="connectionFactory" ref="connectionFactory"></property>
  16. <property name="defaultDestination" ref="destination"></property>
  17. <property name="receiveTimeout" value="600"></property>
  18. </bean>
  19. <!-- 发送者/生产者 -->
  20. <bean id="sender" class="com.ll.springActiveMQ2.Sender">
  21. <property name="jmsTemplate" ref="jmsTemplate"></property>
  22. </bean>
  23. <!-- 接收者/消费者 -->
  24. <bean id="receiver" class="com.ll.springActiveMQ2.Receiver">
  25. <property name="jmsTemplate" ref="jmsTemplate"></property>
  26. </bean>
  27. </beans>



运行结果:

技术分享
技术分享












【ActiveMQ入门-9】ActiveMQ学习-与Spring集成2

标签:

原文地址:http://www.cnblogs.com/ssslinppp/p/4468327.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!