读取而不从 JMS 队列中删除消息

use*_*878 5 java spring message-queue spring-jms

如何从WebSphere MQ读取消息而不从队列中删除原始消息?

我有一个 Spring 应用程序,它从 WebSphere MQ 读取消息。阅读后,我有一个处理方法,它将处理从队列中检索的数据。

步骤1:

response = jmsTemplate.receive();
//Message automatically removed from queue.
Run Code Online (Sandbox Code Playgroud)

第2步:

process(response);
Run Code Online (Sandbox Code Playgroud)

process 方法中有可能抛出异常。如果出现异常,我需要将消息保留在队列中。

是否可以?他们有办法仅在用户确认后删除该消息吗?

我尝试添加以下内容:

jmsTemplate.setSessionAcknowledgeMode(javax.jms.Session.CLIENT_ACKNOWLEDGE);
Run Code Online (Sandbox Code Playgroud)

...但该消息仍然被删除。

JmsTemplate创建代码片段:

JndiConnectionFactorySupport connectionFactoryBean = new JndiConnectionFactorySupport();
    connectionFactoryBean.setBindingsDir(this.bindingDir);


        connectionFactoryBean
                .setConnectionFactoryName(connectionFactoryName);
        connectionFactoryBean.afterPropertiesSet();
        jmsTemplate.setConnectionFactory(connectionFactoryBean.getObject());


    JndiDestinationResolver destinationResolver = new JndiDestinationResolver();
    destinationResolver.setJndiTemplate(connectionFactoryBean
            .getJndiTemplate());

    jmsTemplate.setDestinationResolver(destinationResolver);
    jmsTemplate.setReceiveTimeout(20000);
    jmsTemplate.setDefaultDestinationName(this.defaultDestinationName);

            
Run Code Online (Sandbox Code Playgroud)

尝试了jmsTemplate.execute()如下方法:

@SuppressWarnings({ "unused", "unchecked" })
        Message responseMessage = (Message) jmsTemplate.execute(
            new SessionCallback() { 
                public Object doInJms(Session session)
                        throws JMSException {
                    MessageConsumer consumer = session
                    .createConsumer(jmsTemplate.getDestinationResolver().resolveDestinationName(session, "QUEUE_NAME", false));
                    Message response = consumer.receive(1);
                    try {
                        testMethod();//this method will throw exception.
                        response.acknowledge();
                        consumer.close();
                    } catch(Exception e){
                        consumer.close();//control will come here.
                    }
                    
                    return response;
                }
        }, true);
Run Code Online (Sandbox Code Playgroud)

Gar*_*ell 2

您无法使用receive()方法执行此操作,因为当接收方法返回时操作已完成(从会话角度来看)。

您需要在会话范围内运行可能失败的代码;例如与一个JmsTemplate.execute()与一个SessionCallback-类似这样的东西...

this.jmsTemplate.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
this.jmsTemplate.convertAndSend("foo", "bar");
try {
    String value = this.jmsTemplate.execute(session -> {
        MessageConsumer consumer = session.createConsumer(
                this.jmsTemplate.getDestinationResolver().resolveDestinationName(session, "foo", false));
        String result;
        try {
            Message received = consumer.receive(5000);
            result = (String) this.jmsTemplate.getMessageConverter().fromMessage(received);
            // Do some stuff that might throw an exception
            received.acknowledge();
        }
        finally {
            consumer.close();
        }
        return result;
    }, true);
    System.out.println(value);
}
catch (Exception e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)