简体   繁体   English

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. 如果在发送接收到的消息时发生异常,则消息将丢失,因为它已被接收。

For Jms Template configuration i have : 对于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;

Can you please tell me the way so that i can do recieving and sending in a single session. 能否请您告诉我这样的方式,以便我可以在单个会话中进行接收和发送。 Note: i have also tried Session.ClientAcknowledge while removing sessionTransacted, if exception is there i am not acknowledging the message but still there's a message loss. 注意:在删除sessionTransacted的同时,我也尝试过Session.ClientAcknowledge,如果有异常,我不确认消息,但仍然有消息丢失。

Thanks 谢谢

You can use client acknowledge mode. 您可以使用客户端确认模式。 The message will stay until you decide to make it disappear. 该消息将一直保留,直到您决定使其消失。

message.acknowledge();

See How to Give manual Acknowledge using JmsTemplate and delete message from Rabbitmq queue 请参阅如何使用JmsTemplate进行手动确认并从Rabbitmq队列中删除消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM