ActiveMQ与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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><!--创建连接工厂 --><bean id="connectionFactory" value="tcp://localhost:61616"></property></bean><!-- 声明ActiveMQ消息目标,目标可以是一个队列,也可以是一个主题ActiveMQTopic --><bean id="destination" ref="connectionFactory"></property><property name="defaultDestination" ref="destination"></property><property name="receiveTimeout" value="600"></property></bean><bean id="sender" ref="jmsTemplate"></property></bean><bean id="receiver" ref="jmsTemplate"></property></bean></beans>?
/* * Copyright (c) 2012-2032 Accounting Center of China Aviation(ACCA). * All Rights Reserved. */package com.acca.activemq;import javax.jms.JMSException;import javax.jms.MapMessage;import javax.jms.Message;import javax.jms.Session;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;/** * * * * @author zhouhua, 2012-12-25 */public class Sender { private JmsTemplate jmsTemplate; // getter and setter public JmsTemplate getJmsTemplate() { return jmsTemplate; } public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } public void sendInfo() { jmsTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException { MapMessage message = session.createMapMessage(); message.setString("activemq", "Hello ActiveMQ"); return message; } }); }}?
/* * Copyright (c) 2012-2032 Accounting Center of China Aviation(ACCA). * All Rights Reserved. */package com.acca.activemq;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * * * * @author zhouhua, 2012-12-25 */public class SenderTest { public static void main(String[] args) { // TODO 自动生成方法存根 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Sender sender = (Sender) context.getBean("sender"); sender.sendInfo(); }}?
/* * Copyright (c) 2012-2032 Accounting Center of China Aviation(ACCA). * All Rights Reserved. */package com.acca.activemq;import javax.jms.JMSException;import javax.jms.MapMessage;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.support.JmsUtils;/** * * * * @author zhouhua, 2012-12-25 */public class Receiver { private JmsTemplate jmsTemplate; // getter and setter public JmsTemplate getJmsTemplate() { return jmsTemplate; } public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } /** * 构造函数 */ public Receiver() { } public String receiveMessage() { String my = ""; MapMessage message = (MapMessage) jmsTemplate.receive(); try { my = message.getString("activemq"); } catch (JMSException e) { throw JmsUtils.convertJmsAccessException(e); } return my; }}?
/* * Copyright (c) 2012-2032 Accounting Center of China Aviation(ACCA). * All Rights Reserved. */package com.acca.activemq;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * * * * @author zhouhua, 2012-12-25 */public class ReceiverTest { public static void main(String[] args) { // TODO 自动生成方法存根 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Receiver receiver = (Receiver) context.getBean("receiver"); System.out.print(receiver.receiveMessage()); }}?
首先需要启动ActiveMQ,然后运行SenderTest,运行之后再运行ReceiverTest,这样就可以看到控制台输出:Hello ActiveMQ;ActiveMQ版本:apache-activemq-5.7.0所需jar包:activemq-camel-5.7.0.jaractivemq-console-5.7.0.jaractivemq-core-5.7.0.jaractivemq-jaas-5.7.0.jaractivemq-pool-5.7.0.jaractivemq-protobuf-1.1.jaractivemq-web-5.7.0.jarcamel-spring-2.10.1.jarcommons-logging-1.0.4.jarcommons-pool-1.6.jargeronimo-j2ee-management_1.1_spec-1.0.1.jargeronimo-jms_1.1_spec-1.1.1.jarlog4j-1.2.17.jarslf4j-api-1.6.6.jarslf4j-log4j12-1.6.6.jarspring-asm-3.0.7.RELEASE.jarspring-beans-3.0.7.RELEASE.jarspring-context-3.0.7.RELEASE.jarspring-core-3.0.7.RELEASE.jarspring-expression-3.0.7.RELEASE.jarspring-jms-3.0.7.RELEASE.jarspring-tx-3.0.7.RELEASE.jarapache-activemq-5.7.0下载地址:http://activemq.apache.org/activemq-570-release.html以上所需jar包在apache-activemq-5.7.0\lib\optional中可以找到? 盼望楼主上传1、2、3、4