启动类
package cn.exrick.xboot;
import javax.jms.*;
import org.activiti.spring.boot.SecurityAutoConfiguration;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.task.TaskExecutor;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
// Activiti5.22需要排除
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
// 启用JPA审计
@EnableJpaAuditing
// 启用缓存
@EnableCaching
// 启用异步
@EnableAsync
// 启用自带定时任务
@EnableScheduling
// 启用java消息服务
@EnableJms
public class startApplication {
@Primary
@Bean
public TaskExecutor primaryTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
return executor;
}
@Bean(name = "msgQueue")
public Queue msgQueue() { return new ActiveMQQueue("MESSAGE_QUEUE"); }
@Bean(name = "objQueue")
public Queue objQueue() { return new ActiveMQQueue("OBJECT_QUEUE"); }
@Bean(name = "msgTopic")
public Topic msgTopic() { return new ActiveMQTopic("MESSAGE_TOPIC"); }
@Bean(name = "objTopic")
public Topic objTopic() { return new ActiveMQTopic("OBJECT_TOPIC"); }
public static void main(String[] args) {
SpringApplication.run(startApplication.class, args);
}
}
消息生产者
import javax.jms.*;
import cn.common.utils.ThreadPoolUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
/**
* @Author: Zhang
* @Date: 2019/7/10 16:52
* @Description: 消息生产者
*/
@Slf4j
@Component
public class Producer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Resource(name = "msgQueue")
private Queue msgQueue;
@Resource(name = "msgTopic")
private Topic msgTopic;
@Resource(name = "objQueue")
private Queue objQueue;
@Resource(name = "objTopic")
private Topic objTopic;
/**
* zhang
* 发送文本消息至ActiveMQ
* 点对点模式
* @param msg
*/
public void producerSendMsgQueue(String msg) {
ThreadPoolUtil.getPool().execute(() -> jmsMessagingTemplate.convertAndSend(msgQueue, msg));
}
/**
* zhang
* 发送文本消息至ActiveMQ
* 发布-订阅模式
* @param msg
*/
public void producerSendMsgTopic(String msg) {
ThreadPoolUtil.getPool().execute(() -> jmsMessagingTemplate.convertAndSend(msgTopic, msg));
}
/**
* zhang
* 发送对象消息至ActiveMQ
* 点对点模式
* @param obj
*/
@Transactional(propagation = Propagation.REQUIRED)
public void producerSendObjToQueue(ActiveMQObjectMessage obj) {
jmsMessagingTemplate.convertAndSend(objQueue, obj);
}
/**
* zhang
* 发送对象消息至ActiveMQ
* 发布-订阅模式
* @param obj
*/
public void producerSendObjToTopic(ObjectMessage obj) {
ThreadPoolUtil.getPool().execute(() -> jmsMessagingTemplate.convertAndSend(objTopic, obj));
}
}
消息消费者
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.stereotype.Component;
import javax.jms.*;
/**
* @Author: Zhang
* @Date: 2019/7/12 14:16
* @Description: 消息消费者
*/
@Slf4j
@Component
public class Consumer{
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerTopic() {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
pub-sub-domain默认是点对点模式(false),开启pub-sub模式(true)
bean.setPubSubDomain(true);
bean.setConnectionFactory(jmsMessagingTemplate.getConnectionFactory());
return new DefaultJmsListenerContainerFactory();
}
/**
* zhang
* 从ActiveMQ接收消息
* 点对点模式
* @param msg
*/
@JmsListener(destination = "CR_TODO_EVENT")
public void consumerReceiveMsgFromQueue(String msg) {
log.info(msg);
/**
* 处理逻辑
*/
}
/**
* zhang
* 从ActiveMQ接收消息
* 发布-订阅模式
* @param msg
*/
@JmsListener(destination = "CR_TODO_EVENT",containerFactory="jmsListenerContainerTopic")
public void consumerReceiveMsgFromTopic(String msg) {
log.info(msg);
/**
* 处理逻辑
*/
}
/**
* zhang
* 从ActiveMQ接收对象消息
* 发布-订阅模式
* @param obj
*/
@JmsListener(destination = "CR_OBJECT_TOPIC",containerFactory="jmsListenerContainerTopic")
public void consumerReceiveObjFromTopic(ObjectMessage obj) {
log.info("从topic中接收对象:{}",obj.toString());
handleObjectMessage(obj);
}
/**
* zhang
* 从ActiveMQ接收对象消息
* 点对点模式
* @param obj
*/
@JmsListener(destination = "${todo.mq}")
public void consumerReceiveObjFromQueue(ObjectMessage obj) {
log.info("从queue中接收对象:{}",obj.toString());
handleObjectMessage(obj);
}
/**
* zhang
* 对mq中获取到的对象信息进行处理
* @param objectMessage
*/
private void handleObjectMessage(ObjectMessage objectMessage){
try {
Object object = objectMessage.getObject();
/**
* 处理逻辑
*/
} catch (JMSException e) {
log.error("从ActiveMQ接收实体对象异常",e);
}
}
}
配置文件application.yml
spring:
profiles:
active: @spring.profiles.active@
activemq:
broker-url: tcp://39.100.**.***:9999
in-memory: false
packages:
trust-all: true
user: admin
password: ****