<?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" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616"></property> </bean> <!-- 声明ActiveMQ消息目标,目标可以是一个队列,也可以是一个主题ActiveMQTopic --> <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg index="0" value="edu.sjtu.erplab.springactivemq2"></constructor-arg> </bean> <!----> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory"></property> <property name="defaultDestination" ref="destination"></property> <property name="receiveTimeout" value="600"></property> </bean> <bean id="sender" class="com.acca.activemq.Sender"> <property name="jmsTemplate" ref="jmsTemplate"></property> </bean> <bean id="receiver" class="com.acca.activemq.Receiver"> <property name="jmsTemplate" 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.jar activemq-console-5.7.0.jar activemq-core-5.7.0.jar activemq-jaas-5.7.0.jar activemq-pool-5.7.0.jar activemq-protobuf-1.1.jar activemq-web-5.7.0.jar camel-spring-2.10.1.jar commons-logging-1.0.4.jar commons-pool-1.6.jar geronimo-j2ee-management_1.1_spec-1.0.1.jar geronimo-jms_1.1_spec-1.1.1.jar log4j-1.2.17.jar slf4j-api-1.6.6.jar slf4j-log4j12-1.6.6.jar spring-asm-3.0.7.RELEASE.jar spring-beans-3.0.7.RELEASE.jar spring-context-3.0.7.RELEASE.jar spring-core-3.0.7.RELEASE.jar spring-expression-3.0.7.RELEASE.jar spring-jms-3.0.7.RELEASE.jar spring-tx-3.0.7.RELEASE.jar apache-activemq-5.7.0下载地址:http://activemq.apache.org/activemq-570-release.html 以上所需jar包在apache-activemq-5.7.0\lib\optional中可以找到?
1 楼
dplqg
23 小时前
盼望楼主上传1、2、3、4