1、引入依赖的jar
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> </dependency> </dependencies>
2、application配置
spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.in-memory=true spring.activemq.pool.enabled=true
3、单向发送消息
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = MqApplication.class) public class MqTest { @Autowired JmsMessagingTemplate jmsMessagingTemplate; //发送String消息 @Test public void testStr() { //往luna队列发送消息 jmsMessagingTemplate.convertAndSend("luna","to luna queue"); } }
接收消息
@Component @Slf4j public class Consumer { @JmsListener(destination = "luna") public void receiveQueue(String text){ log.info("receive:{}",text); } }
查看ActiveMQ控制台
控制台打印
4、双向发送消息
例如 A发送B消费 B消费完毕将结果放在某一个队列中,A再去消费
我们将上面代码继续改造一下
@Component @Slf4j public class Consumer { @JmsListener(destination = "luna") @SendTo("luna_ret")//将消费结果返回 public String receiveQueue(String text){ log.info("receive:{}",text); return "SUCCESS--"+text; } }
写一个消费luna_ret的queue
@Component @Slf4j public class ConsumerRet { @JmsListener(destination = "luna_ret") public void receiveQueue(String text){ log.info("receive:{}",text); } }
ok,我们再执行上面的发送queues代码
ActiveMQ控制台显示
控制台日志显示
由上可知,
@SendTo 注解
可以将返回值发送到指定的queue
-----------------------------------------------
springboot整合ActiveMQ安全配置
全文完,感谢您的耐心阅读~
欢迎大家关注我的公众号