【问题标题】:Configuring jms receive and send queues in same config class在同一个配置类中配置 jms 接收和发送队列
【发布时间】:2020-03-04 01:59:29
【问题描述】:

我有以下 mq 配置类,通过它我可以在接收队列中接收消息,但是当使用 JmsTemplate bean 时,我的消息没有发送到队列。 我没有得到任何JmsException 或任何异常,send() 似乎成功了。它是一段 XML 作为字符串,我将其作为有效负载发送如下:

jmsTemplate.send(session -> session.createTextMessage(payload));

@EnableJms
@Configuration
public class MessageQueueConfiguration {

    @Bean(name = "test-factory")
    public ConnectionFactory getMqConnectionFactory(String host, int port, String queueManager, String channel) throws JMSException {
        final MQConnectionFactory connectionFactory = new MQConnectionFactory();
        connectionFactory.setQueueManager(queueManager);
        connectionFactory.setHostName(host);
        connectionFactory.setPort(port);
        connectionFactory.setChannel(channel);
        connectionFactory.setTransportType(WMQ_CM_CLIENT);
        return connectionFactory;
    }

    @Bean("test-container")
    public JmsListenerContainerFactory containerFactory(final ConnectionFactory connectionFactory, final ErrorHandler errorHandler) {
        final DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setErrorHandler(errorHandler);
        return factory;
    }

    @Bean(name = "receive")
    public Destination receive(@Value("${receive-queue}") final String destination) throws JMSException {
        return new MQQueue(destination);
    }

    @Bean(name = "send")
    public Destination send(@Value("${send-queue}") final String destination) throws JMSException {
        return new MQQueue(destination);
    }

    @Bean(name = "sender")
    public JmsTemplate testTemplate(@Qualifier("test-factory") final ConnectionFactory connectionFactory, @Qualifier("send") final Destination destination) {
        final JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
        jmsTemplate.setDefaultDestination(destination);
        return jmsTemplate;
    }
}

我的问题是我是否配置错误?我是否需要多个连接工厂或容器工厂,因为我有接收和发送队列?

我的听众:

@JmsListener(destination = "${receive}", concurrency = "1-1", containerFactory = "test-container")
public Model<Message> getMessage(@Payload final String message) {...}

【问题讨论】:

    标签: java spring jms spring-jms jmstemplate


    【解决方案1】:

    你不显示你的听众,但为什么你有两个队列?

    看起来您正在发送到一个队列并从另一个队列接收。您需要从您发送到的同一目的地接收。

    【讨论】:

    • 它们是两个独立的队列,一个用于接收,一个用于发送,但它们在同一个通道/队列管理器上。所以我认为这是一个错误配置,我需要为接收方和发送方创建不同的连接工厂?我的理解正确吗?
    • 这毫无意义;如果您发送到目的地foo 并从目的地bar 接收,您将一无所获,消息将累积在队列foo 中。
    • 我从foo(本地mq)接收并发送到bar(远程队列)。他们都在队列管理器 X 上。我收到来自foo 的消息,就像上面的当前设置一样。与听众更新的问题。
    猜你喜欢
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 2017-02-18
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    相关资源
    最近更新 更多