简体   繁体   中英

Non-transactional jmsTemplate waits for session end

I would like to have jms messages sent by jmsTemplate.send() delivered immediately and not after the transaction ends.

I have tried setting isSessionTransacted to false but it does not help. Despite jmsTemplate.send(...) is executed, I do not see it reflected in a broker.

My setup:

@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;
}

Is there any way to force jmsTemplate to send the messages immediately? I am using ActiveMQ and Spring Boot 2.

When a message comes, I do the following:

@JmsListener
public void test(...) {
  processRequest(...) // process request here
  message.acknowledge();
  jmsTemplate.send(...)  // send response
  // some transaction handling
}
 sender                                       receiver
   +                                             +
   |                                             |
   |                                             |
+--+--+ +---------------------------------->  +--+--+  
|     |                                       |     |
|     |                                       |     |
|     |                                       |     |
|     |                                       |     |
|     |          not sent immediately         |     |
|     | <-----------------------------------+ |     |
|     |                                       |     |
|     |                                       |     |
|     |                                       |     |
|     |                                       |     |
|     |                                       |     |
|     |                                       |     |
|     |                                       |     |
|     |                                       |     |
|     |                                       +--+--+
|     |                                          |
|     |                                          |
|     |                                          |
+-----+                                          |

Solution / workaround

I made it working by using MessageProducer directly:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM