我想立即发送由jmsTemplate.send()发送的jms消息,而不是在事务结束后发送。

我尝试将isSessionTransacted设置为false,但这无济于事。尽管已执行jmsTemplate.send(...),但在代理中看不到它。

我的设置:

@Bean
public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) {
    JmsTemplate template = new JmsTemplate(connectionFactory);
    //
    template.setSessionTransacted(false);
    // messages are ack-ed with message.acknowledge()
    template.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
    return template;
}

@Bean
public ActiveMQConnectionFactory connectionFactory() {
    ActiveMQConnectionFactory factory = new
        ActiveMQConnectionFactory(brokerUsername, brokerPassword, brokerURL);
    factory.setTransactedIndividualAck(true);
    factory.setAlwaysSyncSend(true);
    factory.setAlwaysSessionAsync(false);
    factory.setUseCompression(true);
    return factory;
}

@Bean
public DefaultJmsListenerContainerFactory defaultJmsListenerContainerFactory(
        ActiveMQConnectionFactory jmsConnectionFactory,
        DefaultJmsListenerContainerFactoryConfigurer configurer) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    configurer.configure(factory, jmsConnectionFactory);
    factory.setSessionTransacted(false);
    factory.setTransactionManager(null);
    return factory;
}


有什么方法可以强制jmsTemplate立即发送消息?
我正在使用ActiveMQ和Spring Boot 2。

收到消息时,我将执行以下操作:

@JmsListener
public void test(...) {
  processRequest(...) // process request here
  message.acknowledge();
  jmsTemplate.send(...)  // send response
  // some transaction handling
}


 sender                                       receiver
   +                                             +
   |                                             |
   |                                             |
+--+--+ +---------------------------------->  +--+--+
|     |                                       |     |
|     |                                       |     |
|     |                                       |     |
|     |                                       |     |
|     |          not sent immediately         |     |
|     | <-----------------------------------+ |     |
|     |                                       |     |
|     |                                       |     |
|     |                                       |     |
|     |                                       |     |
|     |                                       |     |
|     |                                       |     |
|     |                                       |     |
|     |                                       |     |
|     |                                       +--+--+
|     |                                          |
|     |                                          |
|     |                                          |
+-----+                                          |

最佳答案

解决方案/解决方法

我通过直接使用MessageProducer使其工作:

MessageProducer messageProducer = session.createProducer(message.getJMSReplyTo());
messageProducer.send(createResponse(message, outgoingEntity, session));
session.commit();
messageProducer.close();

08-05 09:21