JMSTemplate发送消息
生活随笔
收集整理的這篇文章主要介紹了
JMSTemplate发送消息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
操作步驟
第一步:初始化一個spring容器
第二步:從容器中獲得JMSTemplate對象。
第三步:從容器中獲得一個Destination對象
第四步:使用JMSTemplate對象發送消息,需要知道Destination
思路分析
在配置文件中,配置JMSTemplate
通過加載Spring容器,初始化JMSTemplate對象
再從容器中,獲取JMSTemplate對象
代碼
public class SpringActivemq {// 使用jsmTemplate 發送消息@Testpublic void testJmsTemplate() throws Exception {// 初始化spring容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");// 從容器中獲得JmsTemplate對象JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class);// 從容器中獲得Destination對象Destination destination = (Destination) applicationContext.getBean("test-queue");// 發送消息jmsTemplate.send(destination, new MessageCreator() {@Overridepublic Message createMessage(Session session) throws JMSException {TextMessage message = session.createTextMessage("spring activemq send queue message");return message;}});} }總結
以上是生活随笔為你收集整理的JMSTemplate发送消息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ActiveMQ中Topic消费者
- 下一篇: Spring整合ActiveMQ接收消息