我是用的maven下的jar包,你也可以自己找spring+mq的全套jar包:
Pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>lishaoranMQ</groupId> <artifactId>springMQ</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging>
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.11.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.1.4.RELEASE</version> </dependency> </dependencies> </project> |
咱们先整没有监听器的简单的mq:
Spring整合activemq的配置文件:
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--配置JMS连接工厂--> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <!--ActiveMQ服务的地址和端口--> <property name = "brokerURL" value="tcp://127.0.0.1:61616" /> </bean>
<!--定义消息队列(queue点对点模式)--> <bean id="queue" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg > <value>test.queue</value> </constructor-arg> </bean>
<!--配置JMS模板(Queue),Spring提供的JMS工具类,用它发送、接受消息--> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name = "connectionFactory" ref="connectionFactory" /> <property name = "destination" ref="queue" /> </bean>
</beans> |
消息发送者:
package cn.lishaoran.queue;
import javax.annotation.Resource; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session;
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/** *消息发送者(queue模式) * @author ALWZ * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:applicationContext.xml"}) public class QueueSender{
@Resource private JmsTemplate jmsTemplate;
@Test public void send(){ sendMqMessage(null,"测试点对点模式的发送消息内容+李少然你好!"); }
public void sendMqMessage(Destination destination,final String message){ if (null==destination) { destination=jmsTemplate.getDefaultDestination(); } jmsTemplate.send(destination,new MessageCreator() {
public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } }); System.out.println("spring+mq采用点对点 发送了消息!"); }
public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; }
} |
消息接收者
package cn.lishaoran.queue;
import javax.annotation.Resource; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.TextMessage;
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jms.core.JmsTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/** *消息接收者 * @author ALWZ * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:applicationContext.xml"}) public class QueueReceiver {
@Resource private JmsTemplate jmsTemplate;
public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; }
@Test public void receiveMqMessage(){ Destination destination=jmsTemplate.getDefaultDestination(); receive(destination); }
//接收消息 public void receive(Destination destination){ TextMessage tm =(TextMessage)jmsTemplate.receive(destination); try { System.out.println("从队列"+destination.toString()+"收到了消息:\t"+tm.getText()); } catch (JMSException e) { e.printStackTrace(); } }
} |
先运行消息发送者,再运行消息接收者,然后就可以看到:
发送消息:
接受消息:
不加入监听器的情况是消息接收者伸手和消息发送者要,但是消息接收者不知道什么时候消息发送者发送消息,所以咱们就需要写一个监听器,让监听器监听发送端的情况,一旦发现发送者发送了消息,就通知接收者接收。
监听模式的queue:
配置文件如下:
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!--配置JMS模板(Queue),Spring提供的JMS工具类,用它发送、接受消息--> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name = "connectionFactory" ref="connectionFactory" /> </bean>
<!--配置JMS连接工厂--> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <!--ActiveMQ服务的地址和端口--> <property name = "brokerURL" value="tcp://127.0.0.1:61616" /> </bean>
<!--定义消息队列(queue点对点模式)发送地址--> <bean id="queue" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg > <value>testSpringQueue</value> </constructor-arg> </bean>
<!--配置消息队列监听者(queue)--> <bean id="listener" class="cn.lishaoran.Listener"></bean>
<!--监听器容器 --> <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name = "connectionFactory" ref="connectionFactory" /> <property name = "destination" ref="queue" /> <property name = "messageListener" ref="listener" /> <property name = "receiveTimeout" value="10000" /> </bean>
</beans> |
发送者:
package cn.lishaoran.queue;
import javax.annotation.Resource; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session;
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/** *消息发送者(queue模式) * @author ALWZ * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:applicationContext.xml"}) public class QueueSender{
@Resource private JmsTemplate jmsTemplate; //从配置文件获取发送地址 @Resource @Qualifier("queue") private Destination destination;
@Test public void send(){ sendMqMessage(destination,"测试点对点模式的发送消息内容+李少然你好!"); }
public void sendMqMessage(Destination destination,final String message){ if (null==destination) { destination=jmsTemplate.getDefaultDestination(); } jmsTemplate.send(destination,new MessageCreator() {
public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } }); System.out.println("spring+mq采用点对点 发送了消息!"); }
public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } } |
接收代码:
package cn.lishaoran.queue;
import javax.annotation.Resource; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.TextMessage;
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jms.core.JmsTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/** *消息接收者 * @author ALWZ * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:applicationContext.xml"}) public class QueueReceiver {
@Resource private JmsTemplate jmsTemplate;
public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } //从配置文件获取接收消息地址 @Resource @Qualifier("queue") private Destination destination;
@Test public void receiveMqMessage(){ receive(destination); }
//接收消息 public void receive(Destination destination){ TextMessage tm =(TextMessage)jmsTemplate.receive(destination); try { System.out.println("从队列"+destination.toString()+"收到了消息:\t"+tm.getText()); } catch (JMSException e) { e.printStackTrace(); } } } |
监听器:
package cn.lishaoran;
import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage;
public class Listener implements MessageListener{
public void onMessage(Message message) { TextMessage tm =(TextMessage) message; try { System.out.println("Listener收到了文本消息:\t"+tm.getText()); } catch (JMSException e) { e.printStackTrace(); } }
} |
由于有了监听器的存在,所以只用启动消息发送端就可以了:
启动之后,过一段时间,监听器监听到了,就自动调用消费端消费这些信息。
一段时间后:、
同时程序也就终止了。