【发布时间】:2017-07-05 12:03:38
【问题描述】:
如何在不从队列中删除原始消息的情况下从 WebSphere MQ 读取消息?
我有一个从 WebSphere MQ 读取消息的 spring 应用程序。 阅读后,我有一个 process 方法,它将处理从队列中检索到的数据。
第 1 步:
response = jmsTemplate.receive();
//Message automatically removed from queue.
第 2 步:
process(response);
在 process 方法中有抛出异常的机会。如果出现异常,我需要将消息保留在队列中。
有可能吗?他们是否只有在用户确认后才能删除消息?
我尝试添加以下内容:
jmsTemplate.setSessionAcknowledgeMode(javax.jms.Session.CLIENT_ACKNOWLEDGE);
...但消息仍然被删除。
JmsTemplate创建代码sn-p:
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);
试过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);
【问题讨论】:
标签: java spring message-queue spring-jms