java springboot 集成activeMQ
1.配置信息
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
2.ActiveMqConfig 消息队列配置信息
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());
}
@Bean("queueListener")
public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(ConnectionFactory connectionFactory){
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(false);
return factory;
}
}
3.封装发送信息方法
import javax.jms.Destination;
public interface IMQSendMsgService {
public void mqSendMessage(Destination destination, final String message);
}
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消息队列发送信息
@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消息队列监听获取消费消息
@Slf4j
@Component
public class ActiveMQListenSendMsg {
@Autowired
IMQSendMsgService mqSendMsgService;
@JmsListener(destination="${spring.activemq.send-msg-after-execute-flow}", containerFactory="queueListener")
public void AfterExecuteFlowSendMag(String message){
}
}
有不懂的小伙伴欢迎给我留言,看到之后会回复,感谢大家的关注