上一篇:SpringBoot如何集成MongoDB? | 带你读《SpringBoot实战教程》之三十四
下一篇:SpringBoot怎样整合RabbitMQ? | 带你读《SpringBoot实战教程》之三十六
本文来自于千锋教育在阿里云开发者社区学习中心上线课程《SpringBoot实战教程》,主讲人杨红艳,点击查看视频内容。
SpringBoot整合ActiveMQ
ActiveMQ是符合JMS规范的消息管理者。
如何使用呢?
在工程中添加依赖:
<!-- 整合ActiveMQ的依赖 -->
       <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-activemq</artifactId>  
       </dependency> 全局配置文件进行配置
spring.activemq.broker-url=tcp://192.168.25.129:61616
spring.activemq.in-memory=true
spring.activemq.user=admin
spring.activemq.password=admin
#如果此处设置为true,需要加如下面的依赖包,否则会自动配置失败,JmsMessagingTemplate
spring.activemq.pool.enabled=false依赖如下:
<dependency>  
                <groupId>org.apache.activemq</groupId>  
                <artifactId>activemq-pool</artifactId> 
     </dependency> 如何实现消息的发送和消息的接收?
新建包:com.qianfeng.activemq:
@Component
public class Producer {
   @Autowired
    private JmsMessagingTemplate jmsTemplate;
    //发送消息
    public void sendMessage(Destination des, String message) {
        jmsTemplate.convertAndSend(des, message);
     }
}
@Component
public class Consumer {
    @JmsListener(destination="myqueues")
    public void receiveMsg(String text) {
        System.out.println(text+"......");
    }
}新建包com.qianfeng.controller:
@Controller
public class TestController {
    @Autowired
    private Producer producer;
    @RequestMapping("/activemq")
    @ResponseBody
    public String tests() {
        //点对点消息
        Destination des = new ActiveMQQueue("myqueues");
        for(int i = 1; i <= 3; i++) {
            producer.sendMessage(des, "hello");
        }
        return "ok";
    }
}在启动类中添加所有需要扫描的包:
@SpringBootApplication(scanBasePackages="com.qianfeng")执行结果:

 
                             
                 
                 
                