Reputation: 896
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 | |
| | <-----------------------------------+ | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | +--+--+
| | |
| | |
| | |
+-----+ |
Upvotes: 2
Views: 1335
Reputation: 896
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();
Upvotes: 2