Spring Boot with JMS - ActiveMQ

今天開始寫Spring Boot 整合JMS ActiveMQ,用Spring Boot 來整合也是相當的簡單。ActiveMQ是一種開源的,實現了JMS1.1規范的面向消息(MOM)的中間件,為應用程序提供高效的、可擴展的、穩定的和安全的企業級消息通信。

  1. 下載ActiveMQ,官網地址:
    http://activemq.apache.org/

  2. 本地啟動ActiveMQ解壓后根據自身的環境啟動,如下:

ActiveMQ bat
ActiveMQ Server

3.打開瀏覽器登錄http://localhost:8161

ActiveMQ

輸入帳號密碼,默認密碼可以通過查看conf文件下面的users.properties文件查看,如下:

users.properties

在application.properties中加入配置

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
通過Maven構建Spring Boot 工程并命名spirng-boot-jms,POM引入依賴:
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
編寫消息生產者:
import com.spring.boot.jms.producter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.jms.Destination;

/**
 * Description: 消息生產者
 * author: 慢慢來了
 * date:  2017/3/23 14:07
 */
@Component
public class Producter{

    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    public void sendMessage(Destination destination, final String message) {
        jmsTemplate.convertAndSend(destination, message);
    }

}

Destination為接收者隊列也就是消息的目的地(消息發送給誰);message消息內容;JmsMessagingTemplate 也可以用JmsTemplate替換,但必須自已在其構造方法中自動配置如:

@Component
public class MyBean {

    private final JmsTemplate jmsTemplate;

    @Autowired
    public MyBean(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    // ...

}
編寫消息消費者:
package com.spring.boot.jms.consumer;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * Description: 消息消費者
 * author: 慢慢來了
 * date:  2017/3/23 14:07
 */
@Component
public class Consumer {

    @JmsListener(destination = "msg.p2p.queue")
    public void processMessage(String content) {

        System.out.println("Receiving a message :" + content);

    }
}

編寫單元測試:
package com.spring.boot.jms;

import com.spring.boot.jms.producter.Producter;
import org.apache.activemq.command.ActiveMQQueue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.jms.Destination;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpirngBootJmsApplicationTests {

    @Autowired
    private Producter producter;

    @Test
    public void contextLoads() {
        Destination p2pMsg = new ActiveMQQueue("msg.p2p.queue");

        producter.sendMessage(p2pMsg , "hello , this is jms msg");
    }

}

運行結果,消費者輸出"Receiving a message :hello , this is jms msg":

Java Test

ActiveMQ:

ActiveMQ Queue
  • 本篇只是演示了單點消息的發送也接收,下篇就會演示消費者收到消息后,處理把結果通知消息生產者;如果文章對你有所幫助請點贊吧~~~~~

參考資料:

http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容