天天看點

SpringBoot 整合 gradle 內建ActiveMQ

開發環境:

1.springBoot 2.3.1.RELEASE

2.gradle 5.6

3.activeMQ 2.3.1.RELEASE

第一步:首先在build.gradle中導入activeMQ的依賴

buildscript {
    dependencies {
        implementation group: 'org.springframework.boot', name: 'spring-boot-starter-activemq', version: '2.3.1.RELEASE'
    }
}           

複制

複制

坐标可以在maven倉庫中尋找位址:

https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-activemq/2.3.1.RELEASE

SpringBoot 整合 gradle 內建ActiveMQ

第二步:在springBoot配置檔案中配置activeMQ

activemq:
    broker-url: tcp://localhost:61616
    user: admin
    password: admin
    pool:
      enabled: false
      max-connections: 50
    #預設包名安全(支援序列化與反序列化)
    packages:
      trust-all: true
  #支援topic訂閱模式
  jms:
    pub-sub-domain: true           

複制

複制

第三部:初始化消息隊列

package com.es;

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

import javax.jms.Queue;

@Configuration
public class ActiveMqConfig {

    /**
     * @Author GONGWENXUE
     * @Description //TODO 消息隊列初始化配置 - 建立消息隊列
     * @version: v1.8.0
     * @Date 13:55 2020/7/31
     * @Param
     * @return
     **/
    @Bean
    public Queue queue() {
        return new ActiveMQQueue("test.queue");
    }

}           

複制

複制

第四部:建立消息釋出者

package com.es.basedata.mq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

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



/**
 * @Author GONGWENXUE
 * @Description //TODO 定義生産者
 * @version: v1.8.0
 * @Date 17:04 2020/7/31
 * @Param
 * @return
 **/
@Service
public class MqProducer {


    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;


    /**
     * @Author GONGWENXUE
     * @Description //TODO 釋出者發送自定義消息
     * @version: v1.8.0
     * @Date 16:58 2020/7/31
     * @Param queueName 隊列名稱
     * @Param message 消息内容
     * @return
     **/
    public void sendMessage(String queueName, String message) {
        //發送隊列消息
        this.jmsMessagingTemplate.convertAndSend(queueName, message);
    }

}           

複制

第五步:建立消費者

package com.es.basedata.mq;

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

/**
 * 定義消費者
 * @Description
 * @author Qz
 * @date 2018年3月27日
 *
 */
@Component
public class MqConsumer {

    @JmsListener(destination = "test.queue")
    public void receiveQueue(String text) {
        System.out.println("消費者:來源于生産者對列的消息:"+text);
    }

}           

複制

複制

第六步:開發接口測試

package com.es.basedata.api;

import com.es.basedata.mq.MqProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @className: TestMq
 * @Description: TODO 測試MQ
 * @version: v1.8.0
 * @author: GONGWENXUE
 * @date: 2020/7/31 17:05
 */
@RestController
public class TestMq {

    @Autowired
    private MqProducer mqProducer;

    @GetMapping("/test-mq/{message}")
    public String test(@PathVariable("message") String message){

        mqProducer.sendMessage("test.queue", message);
        System.out.println("浏覽器上測試mq成功");
        return "浏覽器上測試mq成功";
    }
}           

複制

複制

在浏覽器上輸入 http://localhost:8085/test-mq/浏覽器上測試MQ

得到結果:

SpringBoot 整合 gradle 內建ActiveMQ

背景列印:

SpringBoot 整合 gradle 內建ActiveMQ

到此即大功告成!!!

重點:可能遇到的坑

1.找不到JmsMessagingTemplate,無法比對MessagingTemplateConfiguration

報錯如下:

Field jmsMessagingTemplate in com.es.basedata.mq.MqProducer required a bean of type 'org.springframework.jms.core.JmsMessagingTemplate' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)

The following candidates were found but could not be injected:
	- Bean method 'jmsMessagingTemplate' in 'JmsAutoConfiguration.MessagingTemplateConfiguration' not loaded because Ancestor org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration did not match
           

複制