Annotations 当使用@JmsListener注释定义的JMS侦听器中发生异常时,如何要求JMS服务器重新发送消息?
我一直在Springboot应用程序中编写JMS侦听器。 我用了两种方法: 1定义了一个bean,它是SimpleMessageListenerContainer,如下所示:Annotations 当使用@JmsListener注释定义的JMS侦听器中发生异常时,如何要求JMS服务器重新发送消息?,annotations,jms,listener,spring-boot,Annotations,Jms,Listener,Spring Boot,我一直在Springboot应用程序中编写JMS侦听器。 我用了两种方法: 1定义了一个bean,它是SimpleMessageListenerContainer,如下所示: @Bean SimpleMessageListenerContainer getMyMessageListenerContainer(ConnectionFactory connectionFactory) { MessageListenerAdapter messageListener =
@Bean
SimpleMessageListenerContainer getMyMessageListenerContainer(ConnectionFactory connectionFactory) {
MessageListenerAdapter messageListener = new MessageListenerAdapter(myService);
messageListener.setDefaultListenerMethod("myMethodListener");
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setMessageListener(messageListener);
container.setConnectionFactory(connectionFactory);
container.setDestinationName("aMessageQueue");
return container;
}
class MyService{
public void myMethodListener(String argument){
// do something...
}
}
class MyService{
@JmsListener(destination="aMessageQueue")
public void myMethodListener(String argument) {
// do something...
}
}
class MyService{
@JmsListener(destination="aMessageQueue")
public void myMethodListener(String argument) {
// do something...
}
}
或者2使用简单的JMSListener注释,如下所示:
@Bean
SimpleMessageListenerContainer getMyMessageListenerContainer(ConnectionFactory connectionFactory) {
MessageListenerAdapter messageListener = new MessageListenerAdapter(myService);
messageListener.setDefaultListenerMethod("myMethodListener");
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setMessageListener(messageListener);
container.setConnectionFactory(connectionFactory);
container.setDestinationName("aMessageQueue");
return container;
}
class MyService{
public void myMethodListener(String argument){
// do something...
}
}
class MyService{
@JmsListener(destination="aMessageQueue")
public void myMethodListener(String argument) {
// do something...
}
}
class MyService{
@JmsListener(destination="aMessageQueue")
public void myMethodListener(String argument) {
// do something...
}
}
我喜欢最后一种方法。它非常简单,文档非常简单。不幸的是,我注意到这两种方式的行为完全不同
如果在方法1中引发异常,JMS服务器将向侦听器重新发送消息。在方法2中,如果抛出异常,服务器不会重试将消息发送到侦听器
在使用JMSListener注释定义的侦听器中发生异常时,如何告诉服务器重新发送消息
谢谢你的帮助
Jean我只是在pom.xml中添加了以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-bitronix</artifactId>
</dependency>
即使在我的侦听器中抛出异常,也会再次调用它们。它们不同,第一个使用SimpleMessageListenerContainer,而第二个使用DefaultMessageListenerContainer,其行为不同。例如,如果您有一个JPA事务管理器,那么DefaultMessageListenerContainer应该具有事务行为,如果是运行时异常,则在出错时回滚!如果没有,请向配置中添加JmsTransactionManager。或者完全由您自己使用JMS环境。感谢mdeinum指导您进入新的领域。我没有使用JPA的这个项目,没有访问SQL数据库。所以我尝试了很多东西。我找到的最简单的解决方案是加载spring boot starter jta bitronix,这就成功了。如果您不需要多个事务性资源,这就太过分了,因为它将启用XA,这与本地事务相比有额外的成本。如果您只需要JMS事务,最简单的方法就是注册一个JmsTransactionManager。在Spring引导应用程序中如何做到这一点?只需将其添加为@Bean即可。。。