1. 程式人生 > >activemq 學習系列(五) activemq 與 spring boot 整合

activemq 學習系列(五) activemq 與 spring boot 整合

-a pool autowire mapping pri control ESS fin tid

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 整合