1 pom依赖
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.0.9.RELEASE</version>
</dependency>
注意:spring-jms不要使用高版本(会依赖spring 4.1以上)
2 applicationContext-mq.xml
applicationContext-mq.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.xsd">
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<bean id="targetConnectionFactoryMq" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="${mq.url}"/>
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactoryMq" class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactoryMq"/>
</bean>
<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<property name="connectionFactory" ref="connectionFactoryMq"/>
<!-- deliveryMode, priority, timeToLive 的开关,要生效,必须配置为true,默认false-->
<property name="explicitQosEnabled" value="true"/>
<!-- 发送模式 DeliveryMode.NON_PERSISTENT=1:非持久 ; DeliveryMode.PERSISTENT=2:持久-->
<property name="deliveryMode" value="${mq.deliveryMode}"/>
<!-- 消息应答方式
Session.AUTO_ACKNOWLEDGE 消息自动签收
Session.CLIENT_ACKNOWLEDGE 客户端调用acknowledge方法手动签收
Session.DUPS_OK_ACKNOWLEDGE 不必必须签收,消息可能会重复发送
-->
<property name="sessionAcknowledgeMode" value="1"/>
</bean>
<!--这个是队列目的地,点对点的-->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>${mq.prefix}${mq.queueDestination}</value>
</constructor-arg>
</bean>
<!-- 消息监听器 -->
<bean id="messageListenerAdapter" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<property name="delegate">
<bean class="com.user.bussiness.utils.mq.consumer.MqLister"></bean>
</property>
</bean>
<!-- 消息监听容器 -->
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactoryMq"/>
<property name="destination" ref="queueDestination"/>
<property name="messageListener" ref="messageListenerAdapter"/>
</bean>
activeMq.properties
#队列名
mq.queueDestination=l_test
#mq地址 测试环境
mq.url= tcp://192.168.1.55:61616
#队列前缀
mq.prefix=big_user_
#持久化类型
mq.deliveryMode=2
3消息消费者
MqLister.java
public class MqLister {
public void handleMessage(String message) {
System.out.println(message);
}
public void handleMessage(Map message) {
System.out.println(message);
}
public void handleMessage(OutputDTO message) {
System.out.println(message);
}
}
自定义类:
默认消息接收方法: handleMessage
MessageListenerAdapter对象实现Lister接口 ,并具有消息类型转换功能,根据接收到的消息类型转换。
handleMessage重载方法会处理不同消息请求,对于对象,可直接转换。
4消息发送及测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/spring/applicationContext.xml")
public class MqTest {
@Autowired
private JmsTemplate jmsTemplate;
@Resource(name = "queueDestination")
private Destination destination;
@Test
public void test() {
jmsTemplate.convertAndSend(destination, "hello");
Map map = new HashMap<>();
map.put("2", "hello2");
jmsTemplate.convertAndSend(destination, map);
OutputDTO outputDTO = new OutputDTO();
outputDTO.setCode("hello3");
jmsTemplate.convertAndSend(destination, outputDTO);
}
}
说明: 注入jmstemplate 和 xml中定义的队列
convertAndSend方法 第一个参数 队列 第二个参数 data