非事务性jmsTemplate等待会话结束

时间:2019-05-06 11:11:48

标签: java spring jms activemq spring-transactions

我希望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 Boot2。

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

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

1 个答案:

答案 0 :(得分:0)

解决方案/解决方法

我直接使用MessageProducer使其工作:

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