先导入spring相关的jar包:

再在src目录下添加spring配置文件applicaion.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 连接池 -->
<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"></property>
</bean>
</property>
</bean>
<!-- 连接工厂 -->
<bean id="activeMQConnectionFatory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"></property>
</bean>
<!-- 配置消息目标 destination-->
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- 目标队列,在ActiveMQ管理员控制台创建 http://localhost:8161/admin/queues.jsp创建 -->
<constructor-arg index="0" value="FouthQueue" /> <!-- 设置第一个参数为队列名 -->
</bean>
<!-- 配置消息模板 jmsTemplate -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="activeMQConnectionFatory"></property>
<property name="defaultDestination" ref="destination"></property>
<property name="messageConverter">
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter" />
</property>
</bean>
</beans>
发送方代码:
package com.mycom.activemq;
import java.util.HashMap;
import java.util.Map;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
/**
* ActiviteMQ方式4:整合Spring
* 消息发送方(生产者)
*
* @author guweiqiang
*/
public class SpringSender {
/**
* 发送消息
*/
public static void sendMessage(final Map<String, Object> map) {
// 读取Spring配置文件
ApplicationContext context = new FileSystemXmlApplicationContext(
"classpath:applicaion.xml");
// 获取JmsTemplate对象
JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
// 利用 JmsTemplate 发送消息
jmsTemplate.send(new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
// 发送消息
MapMessage mapMsg = session.createMapMessage();
mapMsg.setInt("ID", (Integer) map.get("id"));
mapMsg.setString("NAME", (String) map.get("name"));
mapMsg.setInt("AGE", (Integer) map.get("age"));
System.out.println(mapMsg);
return mapMsg;
}
});
}
/**
* 测试方法
*/
public static void main(String[] args) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 10043);
map.put("name", "guweiqiang43");
map.put("age", 143);
SpringSender.sendMessage(map);
}
}
接收方代码:
package com.mycom.activemq;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
/**
* ActiviteMQ方式4:整合Spring
* 消息接收方(消费者)
*
* @author guweiqiang
*/
public class SpringReceiver {
/**
* 接收消息
*/
public static void receiveMessage() {
// 读取Spring配置文件
ApplicationContext context = new FileSystemXmlApplicationContext(
"classpath:applicaion.xml");
// 获取JmsTemplate对象
JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
// 利用 JmsTemplate 接收消息
while (true) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) jmsTemplate
.receiveAndConvert();
System.out.println("ID:" + map.get("ID") + "\t NAME:"
+ map.get("NAME") + "\t AGE:" + map.get("AGE"));
}
}
/**
* 测试方法
*/
public static void main(String[] args) {
SpringReceiver.receiveMessage();
}
}
启动ActiveMQ,再在本地执行上述发送方和接收方代码,运行结果如下:
发送方console:
ActiveMQMapMessage {commandId = 0, responseRequired = false, messageId = null, originalDestination = null, originalTransactionId = null, producerId = null, destination = null, transactionId = null, expiration = 0, timestamp = 0, arrival = 0, brokerInTime = 0, brokerOutTime = 0, correlationId = null, replyTo = null, persistent = false, type = null, priority = 0, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = false, readOnlyBody = false, droppable = false} ActiveMQMapMessage{ theTable = {NAME=guweiqiang43, AGE=143, ID=10043} }
接收方console:
ID:10043 NAME:guweiqiang43 AGE:143
本文详细介绍了如何使用Spring框架与ActiveMQ集成,实现消息的发送和接收过程,包括配置连接池、创建消息目标及消息模板,并通过发送方和接收方代码示例进行演示。
495

被折叠的 条评论
为什么被折叠?



