码迷,mamicode.com
首页 > 编程语言 > 详细

activemq 学习系列(五) activemq 与 spring boot 整合

时间:2018-11-24 11:25:30      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:and   tco   enable   服务   private   template   ping   实时   listener   

activemq 与 spring boot 整合

1、添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>logback-core</artifactId>
                    <groupId>ch.qos.logback</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>logback-classic</artifactId>
                    <groupId>ch.qos.logback</groupId>
                </exclusion>
            </exclusions>
        </dependency>

2、配置文件

spring:
  activemq:
    broker-url: tcp://localhost:61616
    user: admin
    password: admin
    in-memory: true
    pool:
      enabled: false

3、消息发送服务

@Service
public class ActiveMQProducer {

    @Autowired
    private JmsTemplate jmsTemplate;
    
    /**
     * 实时投递消息
     * @param destination
     * @param message
     */
    public void sendMessage (Destination destination, final String message) {
        this.jmsTemplate.convertAndSend(destination, message);
    }

}

4、发送消息

@RestController
@RequestMapping("/idle")
public class IdleController {

    @Autowired
    private ActiveMQProducer activeMQProducer;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object add (){
        Destination demon = new ActiveMQQueue("demon");
        this.activeMQProducer.sendMessage(demon, "message");
    }

}

5、添加监听

@JmsListener(destination = "demon")
public void imUser (String message) {
    System.out.println(message);    
}

 

activemq 学习系列(五) activemq 与 spring boot 整合

标签:and   tco   enable   服务   private   template   ping   实时   listener   

原文地址:https://www.cnblogs.com/bmw320li/p/10011056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!