JMS 模板,如何使用 JMS 模板从一个队列接收消息并发送到另一个队列
JMS Template,How can i receive a message from one queue and send to another using JMS Template
public void sendSimpleMessage(String receiver, String sender) {
try {
Message message = jmsTemplate.receive(receiver);
System.out.println(message.getIntProperty("OlQuestionId"));
jmsTemplate.send(sender, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
throw new JMSException("Exception"+message.getIntProperty("OlQuestionId"));
}
});
} catch (JmsException jmsException) {
System.out.println(jmsException);
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
If an exception occurs while sending the received message there would be a loss of message as it is already recieved.
对于 Jms 模板配置,我有:
@Bean
public JmsTemplate jmsTemplate() throws JMSException {
JmsTemplate template = new JmsTemplate();
template.setConnectionFactory(connectionFactory());
//template.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
template.setSessionTransacted(true);
template.setDeliveryMode(2);
return template;
能否请您告诉我方式,以便我可以在一个会话中完成接收和发送。
注意:我也曾尝试 Session.ClientAcknowledge 删除 sessionTransacted,如果出现异常,我不会确认该消息,但仍然会丢失消息。
谢谢
您可以使用客户端确认模式。该消息将一直保留,直到您决定让它消失。
message.acknowledge();
见
public void sendSimpleMessage(String receiver, String sender) {
try {
Message message = jmsTemplate.receive(receiver);
System.out.println(message.getIntProperty("OlQuestionId"));
jmsTemplate.send(sender, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
throw new JMSException("Exception"+message.getIntProperty("OlQuestionId"));
}
});
} catch (JmsException jmsException) {
System.out.println(jmsException);
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
If an exception occurs while sending the received message there would be a loss of message as it is already recieved.
对于 Jms 模板配置,我有:
@Bean
public JmsTemplate jmsTemplate() throws JMSException {
JmsTemplate template = new JmsTemplate();
template.setConnectionFactory(connectionFactory());
//template.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
template.setSessionTransacted(true);
template.setDeliveryMode(2);
return template;
能否请您告诉我方式,以便我可以在一个会话中完成接收和发送。 注意:我也曾尝试 Session.ClientAcknowledge 删除 sessionTransacted,如果出现异常,我不会确认该消息,但仍然会丢失消息。
谢谢
您可以使用客户端确认模式。该消息将一直保留,直到您决定让它消失。
message.acknowledge();
见