zoukankan      html  css  js  c++  java
  • activemq 学习系列(五) activemq 与 spring boot 整合

    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);    
    }
  • 相关阅读:
    【BZOJ2138】stone
    【ARC076F】 Exhausted
    [SDOI2018]战略游戏
    CF536D Tavas in Kansas
    [JSOI2018]战争
    ###学习《C++ Primer》- 5
    ###学习《C++ Primer》- 4
    ###Linux基础
    ###Linux基础
    ###Linux基础
  • 原文地址:https://www.cnblogs.com/bmw320li/p/10011056.html
Copyright © 2011-2022 走看看