测试消息队列(记住三要素就行):
① 依赖文件
② 配置文件
③ 实体类
在这之前你的配好,虚拟机的ActiveMQ,安装解压,运行就行,也不用配置。
接下来实现:第一步 消息的提供者:(按照三步走)
① 依赖(核心就前两个,后面是我测试用的)
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.14.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
② 配置文件(注释我写下代码里了)
<!-- 0.扫描-->
<context:component-scan base-package="com.example"/>
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<bean class="org.apache.activemq.ActiveMQConnectionFactory" id="activeMQConnectionFactory">
<property name="brokerURL" value="tcp://182.92.169.74:61616"/>
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean class="org.springframework.jms.connection.SingleConnectionFactory" id="connectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="activeMQConnectionFactory"/>
</bean>
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<bean class="org.springframework.jms.core.JmsTemplate" id="jmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
<!--这个是队列目的地,点对点的 文本信息-->
<bean class="org.apache.activemq.command.ActiveMQQueue" id="queueTextDestination">
<constructor-arg value="queue_text"/>
</bean>
③ 实现类
@Component
public class QueueProducer {
@Autowired
private JmsTemplate jmsTemplate;
@Autowired
private Destination queueTextDestination;
public void sendString(final String message){
jmsTemplate.send(queueTextDestination, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(message);
}
});
}
public void sendTextMessage(final String text){
jmsTemplate.send(queueTextDestination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(text);
}
});
}
}
④测试提供者:运行Text
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-jms-provider.xml")
public class testProvider{
@Autowired
private QueueProducer queueProducer;
@Test
public void testSend(){
queueProducer.sendString("789");
}
}
下面是接收者:
① 依赖(同上,就不加了)
② 配置文件
<!-- 0.扫描-->
<context:component-scan base-package="com.example"/>
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<bean class="org.apache.activemq.ActiveMQConnectionFactory" id="activeMQConnectionFactory">
<property name="brokerURL" value="tcp://182.92.169.74:61616"/>
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean class="org.springframework.jms.connection.SingleConnectionFactory" id="connectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="activeMQConnectionFactory"/>
</bean>
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<bean class="org.springframework.jms.core.JmsTemplate" id="jmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
<!--这个是队列目的地,点对点的 文本信息-->
<bean id="queueTextDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="queue_text"/>
</bean>
<!-- 我的监听类-->
<bean class="com.example.monitor.MyMessageListener" id="MyMessageListener"></bean>
<!--监听消息的容器-->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="queueTextDestination" />
<property name="messageListener" ref="myMessageListener" />
</bean>
== 接收着比提供者多了两个bean:监听类和监听消息容器 ==
③ 监听类(就是实现接口,输入接收的消息)
@Component
public class MyMessageListener implements MessageListener {
@Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage)message;
try {
System.out.println(textMessage.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
}
最后测试,接收者(接收者不需要去注入,只需要开启就可以接收到)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-jms-custom.xml")
public class TextAccept {
@Test
public void testAccept(){
try{
System.in.read();
}catch (Exception e){
e.printStackTrace();
}
}