java springboot 集成activeMQ
1.配置信息
yml
spring:
# 配置消息队列
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
2.ActiveMqConfig 消息队列配置信息
java
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
import org.springframework.jms.core.JmsMessagingTemplate;
import javax.jms.ConnectionFactory;
@Configuration
@EnableJms //启动消息队列
public class ActiveMqConfig {
@Value("${spring.activemq.broker-url}")
private String brokerUrl;
@Value("${spring.activemq.user}")
private String username;
@Value("${spring.activemq.password}")
private String password;
@Bean
public ConnectionFactory connectionFactory(){
return new ActiveMQConnectionFactory(username, password, brokerUrl);
}
@Bean
public JmsMessagingTemplate jmsMessageTemplate(){
return new JmsMessagingTemplate(connectionFactory());
}
// 在Queue模式中,对消息的监听需要对containerFactory进行配置
@Bean("queueListener")
public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(ConnectionFactory connectionFactory){
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(false);
return factory;
}
}
3.封装发送信息方法
- 发送信息接口
java
import javax.jms.Destination;
public interface IMQSendMsgService {
/**
* activeMq发送信息
* @param destination
* @param message
*/
public void mqSendMessage(Destination destination, final String message);
}
- 接口实现
java
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.activemq.ISendMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.jms.Destination;
@Slf4j
@Service
public class MQSendMsgServiceImpl implements IMQSendMsgService {
@Autowired
public JmsMessagingTemplate jmsMessagingTemplate;
@Override
public void mqSendMessage(Destination destination, String message) {
jmsMessagingTemplate.convertAndSend(destination, message);
}
}
4.消息队列在项目中的使用
4.1消息队列发送信息
java
// 下单后记录商品项列表
@Value("${spring.activemq.place-order-record-product-items}")
private String placeOrderRecordProductItems;
@Autowired
IMQSendMsgService mqSendMsgService;
@PostMapping(value = "/send")
public String sendMSG(@RequestBody User user){
String orderHistoryStr = JSON.toJSONString(user);
mqSendMsgService.mqSendMessage(new ActiveMQQueue(placeOrderRecordProductItems), orderHistoryStr);
}
4.2消息队列监听获取消费消息
java
@Slf4j
@Component
public class ActiveMQListenSendMsg {
@Autowired
IMQSendMsgService mqSendMsgService;
@JmsListener(destination="${spring.activemq.send-msg-after-execute-flow}", containerFactory="queueListener")
public void AfterExecuteFlowSendMag(String message){
// 业务代码写在这里
}
}