SpringBoot-2.X 學習筆記07 整合ActivieMQ-5.X
首先在 ActivieMQ 下載並部署 ActivieMQ ,然後啓動 ActivieMQ-5.X。
1 在 SpringBoot 中加入依賴。
1 在 SpringBoot 的 pom.xml 中加入依賴包
<!-- SpringBoot 整合 ActiveMQ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!-- 如果 ActiveMQ 需要用到線程池 則加入此依賴包 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
2 在 SpringBoot 啓動類中加入 @EnableJms 註解。
package com.xu.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.annotation.EnableJms;
@EnableJms
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4 在 配置中修改配置使 ActivieMQ 同事支持 P2P 和 pub/sub 模式。
/**
*
* @Author: hyacinth
* @Title: ActivieMQConfig.java
* @Package com.xu.springboot.activemq
* @Description: TODO:
* @Date: 2019年8月19日 下午9:41:25
* @Version V-1.0
* @Copyright: 2019 hyacinth
*
*/
package com.xu.springboot.activemq;
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.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
/**
* @Author: hyacinth
* @ClassName: ActivieMQConfig
* @Description: TODO
* @Date: 2019年8月19日 下午9:41:25
* @Copyright: hyacinth
*/
@SpringBootConfiguration
public class ActivieMQConfig {
@Bean
public Queue queue() {
return new ActiveMQQueue("student.queues");
}
@Bean
public Topic topic() {
return new ActiveMQTopic("student.topic");
}
@Bean
public JmsListenerContainerFactory <?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setPubSubDomain(true);
bean.setConnectionFactory(activeMQConnectionFactory);
return bean;
}
}
5 在 SpringBoot 配置文件中加入 ActivieMQ 的配置。
############################################################
# SpringBoot ActivieMQ 配置
############################################################
#spring.activemq.broker-url=failover:(tcp://localhost:61616,tcp://localhost:61617) #集羣配置
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false # 線程池
spring.activemq.pool.max-connections=100
6 在 ActivieMQ 加入任意測試消息隊列。
7 測試代碼。
SpringBoot Service
/**
*
* @Author: hyacinth
* @Title: ProducerService.java
* @Package com.xu.springboot.activemq
* @Description: TODO:
* @Date: 2019年8月19日 下午8:28:02
* @Version V-1.0
* @Copyright: 2019 hyacinth
*
*/
package com.xu.springboot.activemq;
import javax.jms.Destination;
/**
* @Author: hyacinth
* @ClassName: ProducerService
* @Description: TODO
* @Date: 2019年8月19日 下午8:28:02
* @Copyright: hyacinth
*/
public interface ProducerService {
/**
* 發佈消息(P2P)
* @Author: hyacinth
* @Title: sendMessage
* @Description: TODO
* @param destination 目的地
* @param message 消息
* @return void
* @date: 2019年8月19日 下午8:30:30
*/
public void sendMessage(Destination destination,final String message);
/**
* 發佈消息(P2P)
* @Author: hyacinth
* @Title: sendMessage
* @Description: TODO
* @param message 消息
* @return void
* @date: 2019年8月19日 下午8:30:34
*/
public void sendMessage(final String message);
/**
* 消息發佈者(pub/sub)
* @Author: hyacinth
* @Title: name
* @Description: TODO
* @param message
* @return void
* @date: 2019年8月19日 下午9:21:53
*/
public void publish(String message);
}
SpringBoot ServiceImpl
/**
*
* @Author: hyacinth
* @Title: ProducerServiceImpl.java
* @Package com.xu.springboot.activemq
* @Description: TODO:
* @Date: 2019年8月19日 下午8:29:36
* @Version V-1.0
* @Copyright: 2019 hyacinth
*
*/
package com.xu.springboot.activemq;
import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
/**
* @Author: hyacinth
* @ClassName: ProducerServiceImpl
* @Description: TODO
* @Date: 2019年8月19日 下午8:29:36
* @Copyright: hyacinth
*/
@Service
public class ProducerServiceImpl implements ProducerService {
@Autowired
private JmsMessagingTemplate template;
@Autowired
private Queue queue;
@Autowired
private Topic topic;
@Override
public void sendMessage(Destination destination, String message) {
template.convertAndSend(destination,message);
}
@Override
public void sendMessage(String message) {
template.convertAndSend(this.queue,message);
}
@Override
public void publish(String message) {
template.convertAndSend(this.topic,message);
}
}
SpringBoot Contrller
/**
*
* @Author: hyacinth
* @Title: ActiveMQController.java
* @Package com.xu.springboot.activemq
* @Description: TODO:
* @Date: 2019年8月19日 下午8:36:05
* @Version V-1.0
* @Copyright: 2019 hyacinth
*
*/
package com.xu.springboot.activemq;
import javax.jms.Destination;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: hyacinth
* @ClassName: ActiveMQController
* @Description: TODO
* @Date: 2019年8月19日 下午8:36:05
* @Copyright: hyacinth
*/
@RestController
@RequestMapping("/activemq")
public class ActiveMQController {
@Autowired
private ProducerService service;
@RequestMapping("/order1")
public Object order(String msg) {
Destination destination=new ActiveMQQueue("student.queues");
service.sendMessage(destination, msg);
return 1;
}
@RequestMapping("/order2")
public Object common(String msg) {
service.sendMessage(msg);
return 1;
}
@RequestMapping("/topic")
public Object topic(String msg) {
service.publish(msg);
return 1;
}
}
SpringBoot ActivieMQ 消費者(P2P)
/**
*
* @Author: hyacinth
* @Title: ActivieMQOrder.java
* @Package com.xu.springboot.activemq
* @Description: TODO:
* @Date: 2019年8月19日 下午9:36:26
* @Version V-1.0
* @Copyright: 2019 hyacinth
*
*/
package com.xu.springboot.activemq;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
* @Author: hyacinth
* @ClassName: ActivieMQOrder
* @Description: TODO
* @Date: 2019年8月19日 下午9:36:26
* @Copyright: hyacinth
*/
@Component
public class ActivieMQOrder {
@JmsListener(destination = "student.queues")
public void recive1(String msg) {
System.out.printf("%-10s: %-100s","(P2P)","消費者 1 接收到的 message : "+msg+"\n");
}
}
SpringBoot ActivieMQ 接收者(Pub/Sub)
註解中的@JmsListener(destination = “student.topic” ) 系需要加入 containerFactory = “jmsListenerContainerTopic” 否則不能同事支持 Pub/Sub 和 P2P。
/**
*
* @Author: hyacinth
* @Title: ActivieMQSubscribe.java
* @Package com.xu.springboot.activemq
* @Description: TODO:
* @Date: 2019年8月19日 下午9:06:00
* @Version V-1.0
* @Copyright: 2019 hyacinth
*
*/
package com.xu.springboot.activemq;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
* @Author: hyacinth
* @ClassName: ActivieMQSubscribe
* @Description: TODO
* @Date: 2019年8月19日 下午9:06:00
* @Copyright: hyacinth
*/
@Component
public class ActivieMQSubscribe {
@JmsListener(destination = "student.topic" , containerFactory = "jmsListenerContainerTopic")
public void recive1(String msg) {
System.out.printf("%-10s: %-100s","(pub/sub)","接收者 2 接收到的 message : "+msg+"\n");
}
@JmsListener(destination = "student.topic" , containerFactory = "jmsListenerContainerTopic")
public void recive2(String msg) {
System.out.printf("%-10s: %-100s","(pub/sub)","接收者 2 接收到的 message : "+msg+"\n");
}
@JmsListener(destination = "student.topic" , containerFactory = "jmsListenerContainerTopic")
public void recive3(String msg) {
System.out.printf("%-10s: %-100s","(pub/sub)","接收者 3 接收到的 message : "+msg+"\n");
}
@JmsListener(destination = "student.topic" , containerFactory = "jmsListenerContainerTopic")
public void recive4(String msg) {
System.out.printf("%-10s: %-100s","(pub/sub)","接收者 4 接收到的 message : "+msg+"\n");
}
}