导入依赖
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.13.4</version>
</dependency>
1.消息生产者
配置文件
<?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">
<!-- 1、真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
<bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.25.132:61616"></property>
</bean>
<!-- 2、Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 2-1目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="activeMQConnectionFactory"></property>
</bean>
<!--3-1这个是队列目的地,点对点的 文本信息 -->
<bean class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="queue_text"></constructor-arg>
</bean>
<!--3-2 这个是订阅模式 文本信息 -->
<bean class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic_solr_html"></constructor-arg>
</bean>
<!-- 4、Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
<bean class="org.springframework.jms.core.JmsTemplate">
<!-- 3-1 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<constructor-arg ref="connectionFactory"></constructor-arg>
</bean>
</beans>
测试demo
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-jms-producer.xml")
public class TestQueue {
@Autowired
private JmsTemplate jmsTemplate;
@Autowired
private ActiveMQQueue queueTextDestination;
@Test
public void testSend(){
jmsTemplate.send(queueTextDestination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("SpringJms-测试demo");
}
});
}
}
2 消息消费者
配置文件
<?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">
<!-- 1、真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
<bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!-- 1-1 -->
<property name="brokerURL" value="tcp://192.168.11.11:61616"></property>
</bean>
<!-- 2、Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 2-2 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="activeMQConnectionFactory"></property>
</bean>
<!--3-1这个是队列目的地,点对点的 文本信息 -->
<bean class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="queue_text"></constructor-arg>
</bean>
<!--3-2这个是订阅模式 文本信息 -->
<bean id="pygSolrHtml" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic_solr_html"></constructor-arg>
</bean>
<!-- 4、我的监听类 -->
<bean id="myMessageListener" class="com.it.listenner.SolrMessageListenner"></bean>
<!-- 5、消息监听容器 -->
<bean
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="destination" ref="pygSolrHtml"></property>
<property name="messageListener" ref="myMessageListener"></property>
</bean>
</beans>
消息监听类
public class MyMessageListener implements MessageListener {
public void onMessage(Message message) {
TextMessage textMessage=(TextMessage)message;
try {
System.out.println("接收到消息:"+textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
测试demo
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-jms-consumer-queue.xm
l")
public class TestQueue {
@Test
public void testQueue(){
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}