在SessionAwareMessageListener中的RuntimeException之后,新消息没有消耗

时间:2015-08-20 18:40:52

标签: java spring jms activemq

我对spring jms SessionAwareMessageListener

有疑问

当我将RedeliveryPolicy添加到ActiveMQConnectionFactory时,我的消息是正确的重新传递,但所有下一条消息(新消息)都被卡住而不消耗(未调用类侦听器)

请帮助,我的代码中有什么问题?

我的JmsPublisher:

    @Component
    public class DocumentMsgSender {

    @Autowired
    private JmsTemplate jmsTemplate;


    public void send(DocumentMsgTemplate message) throws JMSException {
        jmsTemplate.send(new DocMsgCreator(message));
    }


    class DocMsgCreator implements MessageCreator {
        DocumentMsgTemplate message;

        public DocMsgCreator(DocumentMsgTemplate message) {
            this.message = message;
        }

        @Override
        public Message createMessage(Session s) throws JMSException {
            Message resultMessage = s.createObjectMessage(message);
            resultMessage.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
            resultMessage.setJMSPriority(9);
            return resultMessage;
        }
    }
}

My Jms listner:

    @Service
    public class DocumentMsgListener implements SessionAwareMessageListener<Message> {
    @Autowired
    private JmsTemplate sendDocJmsTemplate;

    public void onMessage(Message message, Session session) throws JMSException {
        throw new RuntimeException("ERROR");
    }
}

我的ActiveMQConnectionFactory是

    @Bean
public ActiveMQConnectionFactory connectionFactory() {

    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(env.getProperty("activemq.url"));
    connectionFactory.setRedeliveryPolicy(getRedeliveryPolicy());
    connectionFactory.setMessagePrioritySupported(true);
    return connectionFactory;
}

RedeliveryPolicy

    private RedeliveryPolicy getRedeliveryPolicy() {
    RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
    redeliveryPolicy.setInitialRedeliveryDelay(900000L);
    redeliveryPolicy.setRedeliveryDelay(900000L);
    redeliveryPolicy.setMaximumRedeliveries(48);
    return redeliveryPolicy;
}

我的DefaultMessageListenerContainer是

   @Bean
@Autowired
public DefaultMessageListenerContainer listenerContainer(DocumentMsgListener messageListener) {
    DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
    container.setConnectionFactory(connectionFactory());
    container.setDestinationName("testQueue");
    container.setMessageListener(messageListener);
    container.setSessionTransacted(true);
    return container;
}

我的JmsTemplate:

    @Bean
public JmsTemplate jmsTemplate() {
    JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory());
    jmsTemplate.setDefaultDestinationName("testQueue");
    jmsTemplate.setDeliveryPersistent(true);
    jmsTemplate.setExplicitQosEnabled(true);
    return jmsTemplate;
}

1 个答案:

答案 0 :(得分:0)

答案是

connectionFactory.setNonBlockingRedelivery(真);