Search code examples
jmsspring-jmsjmstemplate

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 :

@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.

Thanks


Solution

  • 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