在 JMS 中重新传递消息

Redeliver message in JMS

我正在尝试阅读来自 Solace 的消息。我能够成功读取消息,但假设 reading/processing 应用程序崩溃的消息。我怎样才能再次阅读该消息?使用下面的代码,我无法再次阅读该消息。以下是我的配置:

@JmsListener(destination = "myqueue", containerFactory = "jmsContainer", concurrency = "5-10")
public void onMessage(Message msg) {
    String message;
    if (msg instanceof TextMessage) {
        message = ((TextMessage) msg).getText();
        LOG.info("In here START " +  message) ;
        Thread.sleep(60000); //I crash my app while thread is sleeping here
        LOG.info("In here END " +  msg.getJMSDestination() ) ;
        
    }
public class SolaceConfig {

    @Bean("solaceJndiTemplate")
    public JndiTemplate solaceJndiTemplate() {
        JndiTemplate solaceJndiTemplate = new JndiTemplate();
    
           // setting user name /password ommitted for brevity

            solaceJndiTemplate.setEnvironment(properties);
            
        
        return solaceJndiTemplate;
    }   
    
    @Bean
    public JndiObjectFactoryBean solaceConnectionFactory(){
        
        JndiObjectFactoryBean solaceConnectionFactory = new JndiObjectFactoryBean();
        solaceConnectionFactory.setJndiTemplate(solaceJndiTemplate());
        solaceConnectionFactory.setJndiName(getJndiName());
        return solaceConnectionFactory;
    }

    @Primary
    @Bean
    public CachingConnectionFactory solaceCachedConnectionFactory(){
        CachingConnectionFactory solaceCachedConnectionFactory = new CachingConnectionFactory();
        solaceCachedConnectionFactory.setTargetConnectionFactory((ConnectionFactory)solaceConnectionFactory().getObject());
        solaceCachedConnectionFactory.setSessionCacheSize(10);
        return solaceCachedConnectionFactory;
    }

    @Bean
    public JmsTemplate jmsTemplate() {
        JmsTemplate jmsTemplate = new JmsTemplate(solaceCachedConnectionFactory());
        jmsTemplate.setDeliveryPersistent(true);
        jmsTemplate.setExplicitQosEnabled(true);
        return jmsTemplate;
    }

    @Bean
    public DefaultJmsListenerContainerFactory jmsContainer() {
        DefaultJmsListenerContainerFactory container = new DefaultJmsListenerContainerFactory();
        container.setConnectionFactory(solaceCachedConnectionFactory());
    //container.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
        return container;
    }

使用 DMLC 时,您应该启用事务(设置 sessionTransacted)以便回滚确认。

否则,请使用 SimpleMessageListenerContainer

查看 javadocs https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jms/listener/DefaultMessageListenerContainer.html

It is strongly recommended to either set "sessionTransacted" to "true" or specify an external "transactionManager". See the AbstractMessageListenerContainer javadoc for details on acknowledge modes and native transaction options, as well as the AbstractPollingMessageListenerContainer javadoc for details on configuring an external transaction manager. Note that for the default "AUTO_ACKNOWLEDGE" mode, this container applies automatic message acknowledgment before listener execution, with no redelivery in case of an exception.