JMSTemplate发送消息

偏执的太偏执、 2022-05-17 08:46 344阅读 0赞

操作步骤

第一步:初始化一个spring容器
第二步:从容器中获得JMSTemplate对象。
第三步:从容器中获得一个Destination对象
第四步:使用JMSTemplate对象发送消息,需要知道Destination

思路分析

在配置文件中,配置JMSTemplate
通过加载Spring容器,初始化JMSTemplate对象
再从容器中,获取JMSTemplate对象

代码

  1. public class SpringActivemq {
  2. // 使用jsmTemplate 发送消息
  3. @Test
  4. public void testJmsTemplate() throws Exception {
  5. // 初始化spring容器
  6. ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
  7. "classpath:spring/applicationContext-activemq.xml");
  8. // 从容器中获得JmsTemplate对象
  9. JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class);
  10. // 从容器中获得Destination对象
  11. Destination destination = (Destination) applicationContext.getBean("test-queue");
  12. // 发送消息
  13. jmsTemplate.send(destination, new MessageCreator() {
  14. @Override
  15. public Message createMessage(Session session) throws JMSException {
  16. TextMessage message = session.createTextMessage("spring activemq send queue message");
  17. return message;
  18. }
  19. });
  20. }
  21. }

发表评论

表情:
评论列表 (有 0 条评论,344人围观)

还没有评论,来说两句吧...

相关阅读