spring-boot集成activeMQ(二)-使用ActiveMQ Server

在上一篇文章中,介紹了使用spring-boot默認的activemq,也就是不須要鏈接activemq-server的寫法,若是沒看過的同窗,能夠點擊下面的鏈接查看html

http://www.javashuo.com/article/p-vnuptsfa-kk.htmljava

下面,咱們來介紹下,怎麼使用本地起來avtivemq-server的配置,以及代碼linux

 

首先,咱們去 http://activemq.apache.org/download.html 下載activemq,我下載的是5.8linux的版本,各位能夠根據本身的jdk版本以及運行環境進行下載.web

個人環境是: jdk版本號爲1.8,springboot版本號爲1.4.1spring

 

運行起來activemq後,咱們輸入 http://localhost:8161 進入activemq的管理頁面:apache

    點擊 Manage ActiveMQ broker 會讓你輸入 帳號密碼,默認的帳號密碼都是 adminspringboot

    進去後,會看到以下信息session

 

而後點擊 queues 查看隊列:app

    

咱們能夠發現,隊列內目前暫時什麼都沒有,就等咱們往裏面添加tcp

 

------------------------------------分割線-------------------------------------

 

咱們須要先引入pom依賴,pom配置以下:

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.4.1.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
   <java.version>1.8</java.version>
</properties>

<dependencies>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
   </dependency>
    <!--引入對activemq的支持-->
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-activemq</artifactId>
   </dependency>
</dependencies>

 

 

jar包已經引入,接下來,就是寫測試代碼,咱們須要一個消息對象,一個消息監聽者(也就是消費者),一個消息發送者(也就是生產者).

 

消息對象代碼:

/**
 * 項目名:SpringBootDemo
 * 建立人:賀小五
 * 建立時間:16/11/27 下午6:21
 * 類名:MessageObject
 * 類描述:
 *        消息對象
 */
public class MessageObject implements MessageCreator{//實現MessageCreator的createMessage方法,建立消息

   @Override
   public Message createMessage(Session session) throws JMSException {
      return session.createTextMessage("測試消息隊列");//調用session的createTextMessage建立消息
   }
}

 

消費者代碼:

 

/**
 * 項目名:SpringBootDemo
 * 建立人:賀小五
 * 建立時間:16/11/27 下午6:25
 * 類名:MessageListener
 * 類描述:
 *        消息監聽者
 */
//註冊爲一個bean
@Component
public class MessageListener {

   //使用@JmsListenter監聽隊列名爲my-message的隊列是否有新消息
   //若是有新消息,則取出進行處理
   //@JmsListenter 註解是spring4.1版本或者以上纔有的註解
   @JmsListener(destination = "my-message")
   public void removeMessage(String msg){
      System.out.println("接收到的消息內容:<"+msg+">");
   }

}

 

 

下面就是消息發送者的代碼:

/**
 * 項目名:SpringBootDemo
 * 建立人:賀小五
 * 建立時間:16/11/27 下午6:27
 * 類名:MessagePush
 * 類描述:
 *        消息推送者
 */
@Component
//實現CommandLineRunner 接口是爲了程序一塊兒動,就調用run方法,讀者能夠不這麼寫
//使用@Scheduled跟@EnableScheduling來定時執行往隊列內放消息
public class MessagePush implements CommandLineRunner{

   //注入 jmsTemplate模板,調用send方法,發送消息對象到名爲my-message的隊列
   @Autowired
   private JmsTemplate jmsTemplate;

   @Override
   public void run(String... args) throws Exception {
      jmsTemplate.send("my-message",new MessageObject());
   }
}

 

接下來就是執行方法測試了,main方法代碼以下:

@SpringBootApplication(scanBasePackages = {"com"})
public class DemoApplication{

   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

 

最後,咱們在 application.properties配置文件裏面,配置activemq-server的鏈接地址:

#隊列地址
spring.activemq.broker-url=tcp://localhost:61616
#帳戶
spring.activemq.user=
#密碼
spring.activemq.password=
#是不是內存模式
spring.activemq.in-memory=true

 

以上的配置,只是最基礎的配置,須要詳細配置的話,請查閱官方文檔,這裏就不貼出來了

 

DemoApplication的main方法,查看打印結果:

查看activemq隊列的queues管理頁面:

    queues下面前5個title從左往右分別表示

  1. 隊列名稱
  2. 消息掛起數量(就是沒有消費者處理的消息數量)
  3. 消費者個數
  4. 入隊列數
  5. 出隊列數

 

 

說明成功了,以上就是 基礎 activemq-server的基本寫法,

 

 

若是你是做爲一個隊列的消費者,只須要編寫消費者代碼,使用@JmsListenter註解到方法上,而且制定隊列名稱,就能監聽該隊列內的消息.若是你是一個生產者,那就須要定義一個消息對象跟一個消息發送者.以上就是本篇文章的所有內容,具體使用,請根據業務須要進行更改!

 

 

以上,均爲本人測試而得出的結果,可能會有出入,或者錯誤,歡迎指正

 

歡迎轉載,請註明出處跟做者,謝謝!

相關文章
相關標籤/搜索