本文主要是介绍java springboot 集成activeMQ(保姆级别教程),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
java springboot 集成activeMQ
1.配置信息
spring: # 配置消息队列activemq:broker-url: tcp://127.0.0.1:61616user: adminpassword: 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;@Beanpublic ConnectionFactory connectionFactory(){return new ActiveMQConnectionFactory(username, password, brokerUrl);}@Beanpublic 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.封装发送信息方法
- 发送信息接口
import javax.jms.Destination;public interface IMQSendMsgService {/*** activeMq发送信息* @param destination* @param message*/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 {@Autowiredpublic JmsMessagingTemplate jmsMessagingTemplate;@Overridepublic void mqSendMessage(Destination destination, String message) {jmsMessagingTemplate.convertAndSend(destination, message);}
}
4.消息队列在项目中的使用
4.1消息队列发送信息
// 下单后记录商品项列表@Value("${spring.activemq.place-order-record-product-items}")private String placeOrderRecordProductItems;@AutowiredIMQSendMsgService 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 {@AutowiredIMQSendMsgService mqSendMsgService;@JmsListener(destination="${spring.activemq.send-msg-after-execute-flow}", containerFactory="queueListener")public void AfterExecuteFlowSendMag(String message){// 业务代码写在这里}
}
有不懂的小伙伴欢迎给我留言,看到之后会回复,感谢大家的关注
这篇关于java springboot 集成activeMQ(保姆级别教程)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!