spring boot整合activeMQ,實現queue和topic兩者消息模式

如何下載安裝MQ我就不說了,百度一大把,老規矩先上一下項目目錄結構:
這裏寫圖片描述

先看一下配置文件,主要是中間件的配置:
ps:1.主要注意的是activeMQ默認提供ptp模式,若要使用topic模式需要假如最後一個配置爲true

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=true  
spring.activemq.pool.enabled=true
#默認情況下activemq提供的是queue模式,若要使用topic模式需要配置下面配置
spring.jms.pub-sub-domain=true

看一下pom用到哪些包吧,其實除了spring boot基本的兩個包以外在引入兩個activemq的依賴就行了:

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

		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-pool</artifactId>
		</dependency>

		<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>
	</dependencies>

然後就是像spring容器聲明配置類:
ps:
1.其實學習一段時間spring boot有點小心得了,當我們使用外部插件時需要自己聲明並且寫配置類,加@Bean註解來爲我們需要提供服務的類進行注入,以往傳統的springMVC就是在配置文件配置bean,指定class,properties之類的屬性等等
2.ptp和topic兩種消息模式我也不贅述了,很好理解,可以理解爲qq的私聊和羣聊的關係

@Configuration
@EnableJms
public class MsgListeners {

    /**
     * 點對點
     * @return
     */
    @Bean
    public Queue queue(){
        return new ActiveMQQueue("zh-queue");
    }

    /**
     * 發佈/訂閱
     * @return
     */
    @Bean
    public Topic topic(){
        return new ActiveMQTopic("zh-topic");
    }
}

接下來是controller層接受請求

@RestController
@RequestMapping("/activeMq")
public class ActiveMqController {
    @Autowired
    private Queue queue;
    @Autowired
    private Topic topic;
    @Autowired
    private ProductService productService;

    @GetMapping("/queue/{msg}")
    public void sendQueue(@PathVariable("msg") String msg){
        productService.sendMessage(this.queue,msg);
    }

    @GetMapping("/topic/{msg}")
    public void sendTopic(@PathVariable("msg")String msg){
        productService.sendMessage(this.topic,msg);
    }
}

然後是生產者service接口和其實現類:
ps:1.第一個方法實現了接口,把接收到的消息和消息模式放到了隊列裏或者主題裏就是queue或topic裏,然後只要寫消費者加上@JmsListener監聽隊列消息就可以自動獲取
2.這裏的第二個方法加了監聽註解就可以收到消費者反饋的信息,前提消費者要加@SendTo註解,具體看下面消費者類

public interface ProductService {
    void sendMessage(Destination destination,String message);
}
@Service
public class ProductServiceImpl implements ProductService{

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;


    @Override
    public void sendMessage(Destination destination, String message) {
        jmsMessagingTemplate.convertAndSend(destination,message);
    }

    @JmsListener(destination = "return-queue")
    public void Message(String message){
        System.out.println("Product收到:"+message);
    }
}

消費者類,2個

@Service
public class Consumer {
    // 使用JmsListener配置消費者監聽的隊列,其中text是接收到的消息
    @JmsListener(destination = "zh-topic")
    public void receiveQueue(String text) {
        System.out.println("Consumer收到:"+text);
    }
}
@Service
public class Consumer2 {
    // 使用JmsListener配置消費者監聽的隊列,其中text是接收到的消息
    @JmsListener(destination = "zh-topic")
    @SendTo("return-queue")
    public String receiveQueue(String text) {
        System.out.println("Consumer2收到:"+text);
        return "Consumer2收到!";
    }
}

然後使用瀏覽器或者postman測試一下就好啦,至於如何同時支持ptp模式和topic我還沒有辦法呢,不知道你有沒有呢?
對了,MQ還支持對象,Map,流,字節等傳輸我還沒試過,以後在做記錄吧

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章