1. 程式人生 > >十六、spring boot 2.x 整合 activemq

十六、spring boot 2.x 整合 activemq

一、安裝activemq

根據你的作業系統選擇不同的檔案下載到本地,解壓後進入bin 目錄,執行./activemq  start命令啟動activemq,然後在位址列輸入:http://127.0.0.1:8161 進入activemq網頁

點選Manage ActiveMQ broker 登入,使用者名稱密碼預設都是admin,登入後進入首頁如下

二、編碼實現

1、在pom.xml中新增activemq的maven依賴

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

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

2、早application.properties檔案中新增activemq的配置資訊

#activemq
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=10

#activemq 自定義資訊
self.activemq.queueName=ldy-test-queue
self.activemq.topicName=ldy-test-topic

3、編寫配置類用來裝載自定義的activemq資訊:ActiveMQProperties.java

package com.ldy.bootv2.demo.jms;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix="self.activemq")
public class ActiveMQProperties {
	
	//訊息佇列名稱
	private String queueName;
	
	//通知主題名稱
	private String topicName;

	public String getQueueName() {
		return queueName;
	}

	public void setQueueName(String queueName) {
		this.queueName = queueName;
	}

	public String getTopicName() {
		return topicName;
	}

	public void setTopicName(String topicName) {
		this.topicName = topicName;
	}

}

4、配置activemq訊息佇列和通知主題:ActiveMQConfig.java,跟啟動類放在同一層級目錄下

package com.ldy.bootv2.demo;

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

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import com.ldy.bootv2.demo.jms.ActiveMQProperties;

@Configuration
public class ActiveMQConfig {

	@Autowired
	private ActiveMQProperties activeMQProperties;

	/**
	 * 訊息佇列
	 */
	@Bean
	public Queue myQueue() {
		Queue queue = new ActiveMQQueue(activeMQProperties.getQueueName());
		return queue;
	}

	/**
	 * 通知主題
	 */
	@Bean
	public Topic myTopic() {
		Topic t = new ActiveMQTopic(activeMQProperties.getTopicName());
		return t;
	}

	/**
	 * 通知設定
	 */
	@Bean
	public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory connectionFactory, Topic myTopic) {
		DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
		bean.setPubSubDomain(true);// 設定為釋出訂閱方式, 預設情況下使用的生產消費者方式
		bean.setConnectionFactory(connectionFactory);
		return bean;
	}
	

}

5、生成者程式碼:ActiveMQProducer.java

package com.ldy.bootv2.demo.jms;

import java.util.Date;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ActiveMQProducer {
	
	/**
	 * 訊息佇列
	 */
	@Autowired
	private Queue myQueue;
	
	/**
	 * 通知主題
	 */
	@Autowired
	private Topic myTopic;

	@Autowired
	private JmsMessagingTemplate jmsTemplate;
	
	/**
         * 測試訊息,5秒傳送一次<br>
         */
        @Scheduled(fixedRate = 1000*5)
	public void execute1() {
    		String msg = "ldy的訊息 "+ new Date().getTime();
    		this.sendMessage2Queue(msg);
        }
    
        /**
         * 測試通知,10秒傳送一次<br>
         */
        @Scheduled(fixedRate = 1000*10)
	public void execute2() {
    		String msg = "ldy的通知 "+ new Date().getTime();
    		this.sendMessage2Topic(msg);
        }
    

	/** 
	 * 傳送訊息到佇列
	 */
	public void sendMessage2Queue(final String message) {
		jmsTemplate.convertAndSend(myQueue, message);
		System.out.println("Producer傳送的訊息為:" + message);
	}
	
	/** 
	 * 傳送通知到主題
	 */
	public void sendMessage2Topic(final String message) {
		jmsTemplate.convertAndSend(myTopic, message);
		System.out.println("Producer傳送的通知>>>>>>>>>>>>>>>>>>>>" + message);
	}

}

注意,測試方法上的@Scheduled註解需要開啟任務排程才可以生效,你也可以通過其他方法測試。

在入口類上新增@EnableScheduling開啟任務排程,也可以直接在當前類上加該註解

package com.ldy.bootv2.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication
public class BootV2App {

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

6、消費者程式碼:

package com.ldy.bootv2.demo.jms;

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

@Component
public class ActiveMQConsumer {

	/**
	 * @方法名: receiveQueue<br>
	 * @描述: 接收訊息<br>
	 * @param message
	 */
	@JmsListener(destination = "${self.activemq.queueName}", concurrency="10")
	public void receiveQueue(String message) {
		System.out.println("Consumer收到的訊息為:" + message);
	}
	
	/**
	 * @方法名: receiveTopic<br>
	 * @描述: 接收通知<br>
	 * @param message
	 */
	@JmsListener(destination = "${self.activemq.topicName}", containerFactory = "jmsListenerContainerTopic")
	public void receiveTopic(String message) {
		System.out.println("Consumer收到的通知>>>>>>>>>>>>>>>>>>>>" + message);
	}

}

7、啟動專案檢視執行效果如圖