作者:L的日记727248401 | 来源:互联网 | 2023-09-08 17:31
篇首语:本文由编程笔记#小编为大家整理,主要介绍了ActiveMQ之与Spring集成相关的知识,希望对你有一定的参考价值。
增加maven依赖
spring版本此处是4.3.5
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jmsartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.apache.activemqgroupId>
<artifactId>activemq-allartifactId>
<version>5.10.0version>
dependency>
<dependency>
<groupId>org.apache.xbeangroupId>
<artifactId>xbean-springartifactId>
<version>4.5version>
dependency>
<dependency>
<groupId>org.apache.activemqgroupId>
<artifactId>activemq-poolartifactId>
<version>5.10.0version>
dependency>
增加spring-activemq.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"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd">
<amq:connectionFactory id="amqConnectionFactory" brokerURL="tcp://localhost:61616" userName="admin" password="admin" />
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="amqConnectionFactory">property>
<property name="sessionCacheSize" value="100" />
bean>
<bean id="demoQueueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg><value>myqueuevalue>constructor-arg>
bean>
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultDestination" ref="demoQueueDestination" />
<property name="pubSubDomain" value="false" />
bean>
<bean id="queueListenerContainer1" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="pubSubDomain" value="false"/>
<property name="destination" ref="demoQueueDestination" />
<property name="messageListener" ref="queueListenter1" />
bean>
<bean id="queueListenter1" class="com.zns.listenter.QueueListenter1">bean>
<bean id="demoTopicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg><value>myqueuevalue>constructor-arg>
bean>
<bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultDestination" ref="demoTopicDestination" />
<property name="pubSubDomain" value="true" />
bean>
<bean id="topicListenerContainer1" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="pubSubDomain" value="true"/>
<property name="destination" ref="demoTopicDestination" />
<property name="messageListener" ref="topicListenter1" />
bean>
<bean id="topicListenerContainer2" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="pubSubDomain" value="true"/>
<property name="destination" ref="demoTopicDestination" />
<property name="messageListener" ref="topicListenter2" />
bean>
<bean id="topicListenter1" class="com.zns.listenter.TopicListenter1">bean>
<bean id="topicListenter2" class="com.zns.listenter.TopicListenter2">bean>
beans>
引入spring-activemq.xml文件
消息监听器
package com.zns.listenter;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class QueueListenter1 implements MessageListener {
public void onMessage(Message message) {
TextMessage textMsg = (TextMessage) message;
try {
System.out.println("消息内容是:" + textMsg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
package com.zns.listenter;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class TopicListenter1 implements MessageListener {
public void onMessage(Message message) {
TextMessage textMsg = (TextMessage) message;
try {
System.out.println("监听者1 收到消息内容是:" + textMsg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
package com.zns.listenter;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class TopicListenter2 implements MessageListener {
public void onMessage(Message message) {
TextMessage textMsg = (TextMessage) message;
try {
System.out.println("监听者2 收到消息内容是:" + textMsg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
控制器Controller核心代码
@Resource(name = "jmsQueueTemplate")
private JmsTemplate jmsQueueTemplate;
@Resource(name = "demoQueueDestination")
@Autowired
private Destination demoQueueDestination;
@Resource(name = "jmsTopicTemplate")
private JmsTemplate jmsTopicTemplate;
@Resource(name = "demoTopicDestination")
@Autowired
private Destination demoTopicDestination;
final String msg="hello,queue...";
System.out.println("发送消息...");
jmsQueueTemplate.send(demoQueueDestination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(msg);
}
});
final String msg="hello,topic...";
System.out.println("发送消息...");
jmsTopicTemplate.send(demoTopicDestination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(msg);
}
});
发送消息后,可以看到监听器接收到消息