当使用@JmsListener 注释定义的 JMS 侦听器发生异常时,如何要求 JMS 服务器重新发送消息?

How can I ask a JMS server to resend the message when an exception occurs in a JMS listener defined with the @JmsListener annotation?

我一直在 Springboot 应用程序中编写 JMS 侦听器。 我用了两种方式:

1) 定义一个 bean,它是 SimpleMessageListenerContainer,如下所示:

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

或 2) 使用简单的 JMSListener 注释,如下所示:

class MyService{
    @JmsListener(destination="aMessageQueue")
    public void myMethodListener(String argument) {
        // do something...
    }
}

我喜欢最后一种方法。它非常简单,并且非常简单地记录了代码。不幸的是,我注意到这两种方式的行为完全不同。

如果方法1)抛出异常,JMS服务器会重新发送消息给监听器。在方法 2) 中,如果抛出异常,服务器不会重试将消息发送给侦听器。

当使用 JMSListener 注解定义的侦听器发生异常时,如何告诉服务器重新发送消息?

感谢您的帮助。

我只是在 pom.xml 中添加了以下依赖项:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jta-bitronix</artifactId>
    </dependency>

我的听众现在看起来像这样:

class MyService{
  @JmsListener(destination="aMessageQueue")
  public void myMethodListener(String argument) {
    // do something...
  }
}

即使在我的侦听器中抛出异常,它们也会被再次调用。