Spring整合MQ配置是什么,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

創(chuàng)新互聯(lián)自2013年起,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務公司,擁有項目網(wǎng)站設計、成都網(wǎng)站設計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元松江做網(wǎng)站,已為上家服務,為松江各地企業(yè)和個人服務,聯(lián)系電話:13518219792
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.activemq.pool.PooledConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.*;
import org.springframework.jms.support.converter.SimpleMessageConverter;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import javax.jms.*;
@Configuration
@EnableAsync
@EnableScheduling
public class MQConfig {
/**
* 配置隊列消息模板
* @param jmsFactory
* @return
*/
@Bean
public JmsTemplate queueTemplate(@Autowired ConnectionFactory jmsFactory,
@Autowired ActiveMQQueue activeMQQueue) {
var template = new JmsTemplate(jmsFactory);
template.setDefaultDestination(activeMQQueue);
template.setMessageConverter(new SimpleMessageConverter());
return template;
}
/**
* 配置主題消息模板
* @param jmsFactory
* @param activeMQTopic
* @return
*/
@Bean
public JmsTemplate topicTemplate(@Autowired ConnectionFactory jmsFactory,
@Autowired ActiveMQTopic activeMQTopic) {
var template = new JmsTemplate(jmsFactory);
template.setDefaultDestination(activeMQTopic);
template.setMessageConverter(new SimpleMessageConverter());
return template;
}
/**
* 配置消息生產(chǎn)者
* @return
*/
@Bean
public PooledConnectionFactory JmsFactory() {
return new PooledConnectionFactory();
}
/**
* 配置隊列消息
* @return
*/
@Bean
public ActiveMQQueue activeMQQueue() {
return new ActiveMQQueue();
}
/**
* 配置主題消息
* @return
*/
@Bean
public ActiveMQTopic activeMQTopic() {
return new ActiveMQTopic();
}
/**
* 配置監(jiān)聽器
* @return
*/
@Bean
public MessageListener messageListener() {
MessageListener messageListener = new MessageListener() {
@Override
public void onMessage(Message message) {
if (null != message && message instanceof TextMessage) {
try {
((TextMessage) message).getText();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
};
return messageListener;
}
/**
* 配置監(jiān)聽容器
* @param jmsFactory
* @param activeMQQueue
* @param messageListener
* @return
*/
@Bean
public DefaultMessageListenerContainer jmsContainer(@Autowired ConnectionFactory jmsFactory,
@Autowired ActiveMQQueue activeMQQueue,
@Autowired MessageListener messageListener) {
DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
container.setConnectionFactory(jmsFactory);
container.setDestination(activeMQQueue);
container.setMessageListener(messageListener);
return container;
}
}import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.jms.TextMessage;
@Service
public class MQ {
@Autowired@Qualifier("queueTemplate")
private JmsTemplate queueTemplate;
@Autowired@Qualifier("topicTemplate")
private JmsTemplate topicTemplate;
@Async
public void queueProducer(){
queueTemplate.send(session -> {
TextMessage textMessage = session.createTextMessage("*****");
return textMessage;
});
}
@Async
public void queueConsumer(){
queueTemplate.receiveAndConvert();
}
@Async
public void topicProducer(){
topicTemplate.send(session -> {
TextMessage textMessage = session.createTextMessage("*****");
return textMessage;
});
}
@Async
public void topicConsumer(){
topicTemplate.receiveAndConvert();
}
}關(guān)于Spring整合MQ配置是什么問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
新聞標題:Spring整合MQ配置是什么
網(wǎng)站網(wǎng)址:http://www.chinadenli.net/article36/gcedsg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App設計、用戶體驗、網(wǎng)站設計公司、外貿(mào)建站、電子商務、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)