概述:
下面将介绍如何在Spring下集成ActiveMQ。
消费者:同步接收;
目的地:Queue
环境:
共5个文件
- Receiver.java
- ReceiverTest.java
- Sender.java
- SenderTest.java
- applicationContext.xml
使用的jar包如下:
Receiver.java
ReceiverTest.java
Sender.java
SenderTest.java
源文件:
Receiver.java
package com.ll.springActiveMQ2;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.JmsUtils;
public class Receiver {
private JmsTemplate jmsTemplate;
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("lastname");
} catch (JMSException e) {
throw JmsUtils.convertJmsAccessException(e);
}
return my;
}
}
ReceiverTest.java
package com.ll.springActiveMQ2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ReceiverTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Receiver receiver = (Receiver) context.getBean("receiver");
System.out.print("接收到消息:" + receiver.receiveMessage());
}
}
Sender.java
package com.ll.springActiveMQ2;
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;
public class Sender {
private JmsTemplate jmsTemplate;
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("lastname", "科比");
return message;
}
});
}
}
SenderTest.java
package com.ll.springActiveMQ2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SenderTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Sender sender = (Sender) context.getBean("sender");
sender.sendInfo();
System.out.println("消息已发送...");
}
}
配置文件:
applicationContext.xml
<?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="com.ll.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.ll.springActiveMQ2.Sender">
<property name="jmsTemplate" ref="jmsTemplate"></property>
</bean>
<!-- 接收者/消费者 -->
<bean id="receiver" class="com.ll.springActiveMQ2.Receiver">
<property name="jmsTemplate" ref="jmsTemplate"></property>
</bean>
</beans>
运行结果:
转载请标明出处:http://www.cnblogs.com/ssslinppp/