SpringBoot JMS(ActiveMQ) 使用实践
ActiveMQ
1. 下载windows办的activeMQ后,在以下目录可以启动:
2. 启动后会有以下提示
3. 所以我们可以通过http://localhost:8161访问管理页面,通过tcp://localhost:61616来连接消息服务器,用到的用户名和密码都在以下文件中(默认为admin=admin)
springboot连接ActiveMQ
1. 加入依赖:
spring-boot-starter-activemq
2. 配置连接属性:
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.pool.enabled=false
消息的发送和接收
生产者/消费者模式
1. 创建生产者
package com.example.demo8activemq.jms;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.jms.Destination;
* @author Created by yawn on 2017-10-26 16:15
@Service
public class Producer {
@Resource
private JmsMessagingTemplate jmsMessagingTemplate;
public void sendMsg(String destinationName, String message) {
System.out.println("============>>>>> 发送queue消息 " + message);
Destination destination = new ActiveMQQueue(destinationName);
jmsMessagingTemplate.convertAndSend(destination, message);
}
2. 创建消费者
package com.example.demo8activemq.jms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
* @author Created by yawn on 2017-10-26 16:15
@Service
public class Consumer {
@JmsListener(destination = "test.queue")
public void receiveMsg(String text) {
System.out.println("<<<<<<============ 收到消息: " + text);
}
注意: @JmsListener是一个可重复的注解,在java7及以下版本jdk中,可以使用@JmsListeners代替它。
3. 测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo8ActivemqApplicationTests {
@Resource
private Producer producer;
@Test
public void contextLoads() {
for (int i = 0; i < 10; i++) {
producer.sendMsg("test.queue", "Queue Message " + i);
}
4. 运行测试
发布/订阅模式
1. 发布话题
package com.example.demo8activemq.jms;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.jms.Destination;
* @author Created by yawn on 2017-10-28 17:09
@Service
public class Publisher {
@Resource
private JmsMessagingTemplate jmsMessagingTemplate;
public void publish(String destinationName, String message) {
Destination destination = new ActiveMQTopic(destinationName);
System.out.println("============>>>>> 发布topic消息 " + message);
jmsMessagingTemplate.convertAndSend(destination, message);
}
2. 订阅话题
package com.example.demo8activemq.jms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
* @author Created by yawn on 2017-10-28 17:15
@Service
public class Subscriber {
@JmsListener(destination = "test.topic", containerFactory = "myJmsContainerFactory")
public void subscribe(String text) {
System.out.println("===========<<<<<<<<收到订阅的消息" + text);
}
注意: 在pub/sub模式中,对消息的监听需要对containerFactory进行以下配置
@Bean
JmsListenerContainerFactory<?> myJmsContainerFactory(ConnectionFactory connectionFactory){
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(true);
return factory;