gpt4 book ai didi

java - 非事务性 jmsTemplate 等待 session 结束

转载 作者:行者123 更新时间:2023-12-02 05:56:56 27 4
gpt4 key购买 nike

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

我尝试将 isSessionTransacted 设置为 false 但没有帮助。尽管执行了 jmsTemplate.send(...) ,但我没有看到它反射(reflect)在代理中。

我的设置:

@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();

关于java - 非事务性 jmsTemplate 等待 session 结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56004025/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com