javax.jms.ConnectionFactory
接口提供了一个标准的用于创建一个 javax.jms.Connection
的方法, javax.jms.Connection
用于和 JMS
代理( broker
)交互。尽管为了使用 JMS
, Spring
需要一个 ConnectionFactory
,但通常不需要直接使用它,而是依赖于上层消息抽象, Spring Boot
会自动配置发送和接收消息需要的设施( infrastructure
)。
如果发现 ActiveMQ
在 classpath
下可用, Spring Boot
会配置一个 ConnectionFactory
。如果需要代理,将会开启一个内嵌的,已经自动配置好的代理(只要配置中没有指定代理URL)。
引入 spring-boot-starter-activemq
,在 pom.xml
配置文件中增加配置如下(基于之前章节“
Spring Boot 构建框架
”中的 pom.xml
文件):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
ActiveMQ配置是通过spring.activemq.*中的外部配置来控制的。在application.properties配置文件增加如下内容:
spring.activemq.broker-url=tcp://192.168.1.100:9876 spring.activemq.user=admin spring.activemq.password=secret
默认情况下如果目标不存在,ActiveMQ将创建一个,所以目标是通过它们提供的名称解析出来的。
消息生产者,具体代码如下:
import javax.jms.Destination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
@Service("producer")
public class Producer {
// 该方式可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
@Autowired
private JmsMessagingTemplate jmsTemplate;
// 发送消息,destination是发送到的队列,message是待发送的消息
public void sendMessage(Destination destination, final String message){
jmsTemplate.convertAndSend(destination, message);
}
}
两个消息消费者,具体代码如下:
/***************** Consumer1 ******************/
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class Consumer {
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = "mytest.queue")
public void receiveQueue(String text) {
System.out.println("Consumer收到的报文为:"+text);
}
}
/***************** Consumer2 ******************/
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class Consumer2 {
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = "mytest.queue")
public void receiveQueue(String text) {
System.out.println("Consumer2收到的报文为:"+text);
}
}
注意的是消息消费者的类上必须加上 @Component
或 @Service
注解,这样的话,消息消费者类就会被委派给 Listener
类,原理类似于使用 SessionAwareMessageListener
以及 MessageListenerAdapter
来实现消息驱动 POJO
。
简单测试消息情况,具体代码如下:
import javax.jms.Destination;
import org.apache.activemq.command.ActiveMQQueue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootJmsApplicationTests {
@Autowired
private Producer producer;
@Test
public void contextLoads() throws InterruptedException {
Destination destination = new ActiveMQQueue("mytest.queue");
for(int i=0; i<100; i++){
producer.sendMessage(destination, "my site is blog.yoodb.com");
}
}
}
运行结果如下:
Consumer2收到的报文为:my site is blog.yoodb.com Consumer收到的报文为:my site is blog.yoodb.com Consumer2收到的报文为:my site is blog.yoodb.com Consumer收到的报文为:my site is blog.yoodb.com Consumer2收到的报文为:my site is blog.yoodb.com Consumer收到的报文为:my site is blog.yoodb.com Consumer2收到的报文为:my site is blog.yoodb.com Consumer收到的报文为:my site is blog.yoodb.com Consumer2收到的报文为:my site is blog.yoodb.com Consumer收到的报文为:my site is blog.yoodb.com Consumer2收到的报文为:my site is blog.yoodb.com Consumer收到的报文为:my site is blog.yoodb.com Consumer2收到的报文为:my site is blog.yoodb.com