springboot引入activiemq(一)

首先安裝activiemq服務器 下載地址如下:http://activemq.apache.org/download.html,筆者安裝的windows版本的,解壓一下就可以,然後到這個目錄下,雙擊activemq.bat,即 mq服務啓動,訪問http://localhost:8161/admin/,如果能出現如下監控平臺,則代表安裝成功

下面我們就一步一步的集成activemq。

首先簡歷springboot工程

pom文件引入jar包

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.5.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-activemq</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>


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

 

 

重寫 bean依賴, 以及默認springboot直接收queue消息,如果要接收topic,需要設置containerfactory

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

    /**
     * 發佈/訂閱
     * @return
     */
    @Bean
    public Topic topic(){
        return new ActiveMQTopic("zh-topic");
    }
   /**TODO
     *  * JmsListener註解默認只接收queue消息,如果要接收topic消息,需要設置containerFactory
     *  */
    @Bean
    public JmsListenerContainerFactory<?> topicListenerContainer(ConnectionFactory activeMQConnectionFactory) {
        DefaultJmsListenerContainerFactory  topicListenerContainer = new DefaultJmsListenerContainerFactory();
        topicListenerContainer.setPubSubDomain(true);
        topicListenerContainer.setConnectionFactory(activeMQConnectionFactory);
        return topicListenerContainer;
    }

2  然後sevice層負責發送消息封裝了發送消息方法:

 @Override
    public void sendMessage(Destination destination, String message) {

        jmsMessagingTemplate.convertAndSend(destination, message);
    }

以及一個監聽消息方法

  @JmsListener(destination = "return-queue", containerFactory="topicListenerContainer")
    public void Message(String message) {
        System.out.println("product收到參數了:" + message);
    }

然後就是 消費接收消息方法 1) 隊列消息

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

2)topic消息

  // 使用JmsListener配置消費者監聽的隊列,其中text是接收到的消息
    @JmsListener(destination = "zh-topic", containerFactory="topicListenerContainer")
    //會將接收到的消息發送到指定的路由目的地,所有訂閱該消息的用戶都能收到,屬於廣播。
    @SendTo("return-queue")
    public String receiveQueue(String text) {
        System.out.println("Consumer2收到:"+text);
        return "Consumer2收到!";
    }

然後就是contontroller層注入消息發送類

import com.mp.activemqdemo.activemq.service.ProductService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@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);
    }


}

 

然,application.properties 

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

 

這樣,我們基本已經集成了。

然後我們就可以啓動項目,瀏覽器訪問,對應contoroller的url

localhost:8080/activeMq/topic/nihao

localhost:8080/activeMq/queue/nihao

這樣基本就成功了。 源碼地址詳情見git

再此,感謝

https://blog.csdn.net/eumenides_/article/details/78356170,的啓發

 

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