Creating temporary JMS jms topic in Spring

时间秒杀一切 提交于 2019-12-01 05:40:34

问题


I'm trying to refactor some legacy code to use Spring to handle the jms connections to a mainframe service. I need to connect create a temporary topic for the mainframe service reply and set that as the message.setJMSReplyTo(replyTo); in the message before I send the message.

Can anyone provide examples of this? I have not found anything in the documentation that allows you to get to the low level jms objects such as the session or TopicConnection in order to create a temporary topic.


回答1:


If you need low-level access to the JMS API using JmsTemplate, then you need to use one of JmsTemplate's execute(...) methods. The simplest of these is execute(SessionCallBack), where the SessionCallback provides you with the JMS Session object. With that, you can call createTemporaryQueue() or createTemporaryTopic(). You can probably use one of the other execute() methods do some of the initial work for you, though, such as this one.




回答2:


I was able to create a queue dynamically using the following code in a Spring Boot app:

In Application.java

@Bean 
public ConnectionFactory jmsFactory()
{
    ActiveMQConnectionFactory amq = new ActiveMQConnectionFactory()

    amq.setBrokerURL("tcp://somehost");

    return amq;
}

@Bean 
public JmsTemplate myJmsTemplate()
{
    JmsTemplate jmsTemplate = new JmsTemplate(jmsFactory());

    jmsTemplate.setPubSubDomain(false);
    return jmsTemplate;
}

Then in another class which creates the queue dynamically:

@Component
public class Foo {
    @Autowired
    private ConnectionFactory jmsFactory;

    public void someMethod () {
        DefaultMessageListenerContainer messageListener = new DefaultMessageListenerContainer();

        messageListener.setDestinationName("queueName");
        messageListener.setConnectionFactory(jmsFactory);
        messageListener.setMessageListener(new Consumer("queueName"));
        messageListener.setPubSubDomain(false);
        messageListener.initialize();
        messageListener.start();
    }
}


来源:https://stackoverflow.com/questions/1590306/creating-temporary-jms-jms-topic-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!