Springboot整合ActiveMQ

一、首先新建一個springboot工程(新建過程略),本文springboot版本 是2.1.1.RELEASEjava

二、在pom.xml文件添加activemq的相關依賴web

<!--activemq消息隊列-->

              <dependency>

                  <groupId>org.springframework.boot</groupId>

                  <artifactId>spring-boot-starter-activemq</artifactId>

              </dependency>

              <!--消息隊列鏈接池  此處使用2.0+的版本-->

              <dependency>

                  <groupId>org.apache.activemq</groupId>

                  <artifactId>activemq-pool</artifactId>

                 <!--  <version>5.15.0</version> -->

              </dependency>

              <!-- 消息隊列鏈接池  此處使用2.1+的版本 -->

              <dependency>

                  <groupId>org.messaginghub</groupId>

                  <artifactId>pooled-jms</artifactId>

              </dependency>

其中鏈接池相關的依賴能夠不用配置spring

三、配置application.properties 或者application.yml 本文以application.properties爲例apache

spring.activemq.broker-url=tcp://localhost:61616

spring.activemq.user=admin

spring.activemq.password=admin

#默認狀況下activemq提供的是queue模式,若要使用topic模式須要配置下面配置

#spring.jms.pub-sub-domain=true

#true 表示使用內置的MQ,false則鏈接服務器

spring.activemq.in-memory=false

#true表示使用鏈接池;false時,每發送一條數據建立一個鏈接

spring.activemq.pool.enabled=true

#鏈接池最大鏈接數

spring.activemq.pool.max-connections=10

#空閒的鏈接過時時間,默認爲30秒

spring.activemq.pool.idle-timeout=15000

 

spring.activemq.pool.enabled=true時要在pom文件中添加鏈接池pool相關的依賴,爲false時不用添加鏈接池pool相關的依賴;springboot

若使用鏈接池pool配置時,注意兩種依賴的配置不然啓動失敗。服務器

工程結構以下圖app

Demo代碼以下dom

package com.example.acmpp.config;

import javax.jms.Queue;
import javax.jms.Topic;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanConfig {
	
	//定義存放消息的隊列
    @Bean
    public Queue queue() {
        return new ActiveMQQueue("ActiveMQQueue");
    }
    //定義存放消息的隊列
    @Bean
    public Topic topic() {
        return new ActiveMQTopic("ActiveMQTopic");
    }
}

 

消息生產者代碼tcp

import javax.jms.Queue;

import javax.jms.Topic;



import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jms.core.JmsMessagingTemplate;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/*

 *

 * 消息生產者

 */

@RestController

public class ProviderController {

      

       @Autowired

       private JmsMessagingTemplate jmsMessagingTemplate;



       @Autowired

       private Queue queue;



       @Autowired

       private Topic topic;



       /**

         * 消息生產者 Queue模式

        *

        */

       @RequestMapping("/sendQ")

       public void sendQ(String msg) {

              //方法一:添加消息到消息隊列

        jmsMessagingTemplate.convertAndSend(queue, msg);

        //方法二:這種方式不須要手動建立queue,系統會自行建立名爲test的隊列

        //jmsMessagingTemplate.convertAndSend("testQ", msg);

       }

       /**

        * 消息生產者 Topic模式

        * @param msg

        */

       @RequestMapping("/sendT")

       public void sendT(String msg) {

              // 指定消息發送的目的地及內容

              System.out.println("@@@@@@@@@@@@@@" + msg);

              this.jmsMessagingTemplate.convertAndSend(this.topic, msg);

       }
}

消息消費者代碼ide

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jms.annotation.JmsListener;

import org.springframework.jms.core.JmsMessagingTemplate;

import org.springframework.messaging.handler.annotation.SendTo;

import org.springframework.stereotype.Component;



/**

 * 消息消費者

 * @author FFF

 *

 */

@Component

public class ConsumerService {

       @Autowired

    private JmsMessagingTemplate jmsMessagingTemplate;

      

       /**

        * 消費ActiveMQQueue

        */

    // 使用JmsListener配置消費者監聽的隊列,其中name是接收到的消息

    @JmsListener(destination = "ActiveMQQueue")

    // SendTo 會將此方法返回的數據, 寫入到 OutQueue 中去.

    @SendTo("SQueue")

    public String handleMessage(String name) {

        System.out.println("ActiveMQQueue成功接受Name" + name);

        return "ActiveMQQueue成功接受Name" + name;

    }

    /**

        * 消費ActiveMQ.DLQ

        */

    // 使用JmsListener配置消費者監聽的隊列,其中name是接收到的消息

    @JmsListener(destination = "ActiveMQ.DLQ")

    public void DLQ(String name) {

        System.out.println("ActiveMQ.DLQ成功接受Name==" + name);

    }

    /**

        * 消費SQueue

        */

    // 使用JmsListener配置消費者監聽的隊列,其中name是接收到的消息

    @JmsListener(destination = "SQueue")

    public void SQueue(String name) {

        System.out.println("SQueue成功接受Name==" + name);

    }

    /**

        * 消費testQ

        */

    // 使用JmsListener配置消費者監聽的隊列,其中name是接收到的消息

    @JmsListener(destination = "testQ")

    public void testQMessage(String name) {

        System.out.println("testQ成功接受Name" + name);

    }

    /**

        * 消費topic

        *

        */

    // 使用JmsListener配置消費者監聽的隊列,其中name是接收到的消息

    @JmsListener(destination = "ActiveMQTopic")

    public void topicMessage(String name) {

        System.out.println("topicMessage成功接受Name" + name);

    }
}

相關文章
相關標籤/搜索