JMSTemplate发送消息

操作步骤

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

思路分析

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

代码

public class SpringActivemq {

    // 使用jsmTemplate 发送消息
    @Test
    public 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() {

            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage message = session.createTextMessage("spring activemq send queue message");
                return message;
            }
        });

    }
}

猜你喜欢

转载自blog.csdn.net/nangeali/article/details/81532398