1. 程式人生 > >spring boot整合activemq rabbitmq

spring boot整合activemq rabbitmq

1.下載並安裝activemq服務(windows),下載地址:http://download.csdn.net/download/loveuserzzz/9938202

2.建立springboot工程,並引入依賴

<dependency>
   <groupId>org.apache.activemq</groupId>
   <artifactId>activemq-all</artifactId>
   <version>5.15.0</version>
</dependency>

<
dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency>
3.編寫生產者程式碼實現

@Component
public class Producer  implements CommandLineRunner{

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    @Override
    public void run(String... args) throws Exception {
        send("Sample message");
        System.out.println("Message was sent to the Queue");
    }

    public void send(String msg) {
        this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
    }

}
4.編寫客戶端實現

@Component
public class Consumer {
    @JmsListener(destination = "sample.queue")
    public void receiveQueue(String text) {
        System.out.println(text);
    }
}
5.編寫application.properties

spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
6.啟動activemq服務


7.啟動main執行檢視結果

下載原始碼:https://github.com/yifanzzz/springboot-mq.git

示例中包含activemq和rabbitmq兩部分,可以加群一起討論QQ:324070788