1.点对点模式
1.1消息的生产者
(1)创建普通的maven工程 springjms_producer,在 POM 文件中引入 SpringJms 、activeMQ 以及单元测
试相关依赖
<spring.version>4.2.4.RELEASE</spring.version>
org.springframework
spring-jms
s
p
r
i
n
g
.
v
e
r
s
i
o
n
<
/
v
e
r
s
i
o
n
>
<
/
d
e
p
e
n
d
e
n
c
y
>
<
d
e
p
e
n
d
e
n
c
y
>
<
g
r
o
u
p
I
d
>
o
r
g
.
s
p
r
i
n
g
f
r
a
m
e
w
o
r
k
<
/
g
r
o
u
p
I
d
>
<
a
r
t
i
f
a
c
t
I
d
>
s
p
r
i
n
g
−
t
e
s
t
<
/
a
r
t
i
f
a
c
t
I
d
>
<
v
e
r
s
i
o
n
>
{spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>
spring.version</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring−test</artifactId><version>{spring.version}
junit
junit
4.9
org.apache.activemq
activemq-client
5.13.4
activemq-client这个依赖jar包我自己手打不出来,复制就可以0.0......
(2)在 src/main/resources 下创建 spring 配置文件 applicationContext-jms-producer.xml
<?xml version="1.0" encoding="UTF-8"?><!--刚复制后这里会报错,因为我们还没有创建"com.study.demo"包-->
<context:component-scan base-package="com.study.demo"></context:component-scan>
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!--这里的tcp连接地址根据你自己的来写-->
<property name="brokerURL" value="tcp://192.168.25.128:61616"/>
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory"/>
</bean>
<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
<!--这个是队列目的地,点对点的 文本信息-->
<bean id="activeMQQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="queue"/>
</bean>
我的xmlns:amq="http://activemq.apache.org/schema/core"这部分在idea中一直报红(URI is not registered),实际上并不影响程序的运行.
如果看着比较烦人的话,当然我们也可以解决这个问题,如下图解决方法.
( 3 )在com.study.demo包下面创建QueueProducer生产者
package com.study.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
@Component
public class QueueProducer {
@Autowired
private JmsTemplate jmsTemplate;
@Autowired
private Destination activeMQQueue;//对应于配置文件中的队列目的地id
/**
* 发送文本消息
* @param text
*/
public void sendTextMessage(final String text){
jmsTemplate.send(activeMQQueue, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(text);
}
});
}
}
( 4 )单元测试
在 src/test/java 创建测试类
import com.study.demo.QueueProducer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-jms-producer.xml")
public class TestQueue {
@Autowired
private QueueProducer queueProducer;
@Test
public void testSend(){
queueProducer.sendTextMessage("SpringJms-点对点模式");
}
}
1.2消息的消费者
( 1 )创建普通的maven工程 springjms_consumer,在 POM 文件中引入依赖,和上面一样
(2)创建配置文件 applicationContext-jms-consumer-queue.xml
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.25.128:61616"/>
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory"/>
</bean>
<!--这个是队列目的地,点对点的 文本信息-->
<bean id="activeMQQueue" class="org.apache.activemq.command.ActiveMQQueue">
<!--注意这里的value一定要和生产者的一样,这样才能监听到消息-->
<constructor-arg value="queue"/>
</bean>
<!-- 我的监听类 -->
<bean id="myMessageListener" class="com.study.demo.MyMessageListener"></bean>
<!-- 消息监听容器 -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="activeMQQueue" />
<property name="messageListener" ref="myMessageListener" />
</bean>
( 3 )编写监听类
package com.study.demo;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
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();
}
}
}
( 4 )单元测试
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.IOException;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-jms-consumer-queue.xml")
public class TestQueue {
@Test
public void testQueue() {
try {
//此处为了方便测试,让程序不往下面执行
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
当你生产者单元测试执行了一次后,消费者也执行单元测试会看到如下的结果.
则说明消费者成功的接受到了消息.
写到这里为了不让篇幅过长,所以发布订阅模式下篇文章再写.