Raja Anbazhagan
Raja Anbazhagan

Reputation: 4564

Keeping messages in queue for retry untill processed successfully

I'm using Spring amqp MessageListener implemetation for handling messages. The bean definitions and working example is as follows.

public class AutomationMessageListenerServiceImpl implements MessageListener {

 public void onMessage(Message message) {
        EntityMessage message= null;
        try {
            message= jsonDeserializer.covertPayload(message.getBody());
            dispatchToHandler(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}





<rabbit:listener-container connection-factory="rabbitConnectionFactory">
        <rabbit:listener queue-names="${listen.queue}" ref="messageListener"/>
    </rabbit:listener-container>

    <bean id="messageListener" class="com.tcl.gvg.itsm.automation.core.messaging.AutomationMessageListenerServiceImpl"/>

The problem here is, I'm handling all the errors in the catch block hence the queue thinks I processed the messages successfully. However, I cannot throw Exception because the parent interface will cause problem as it doesnt throw any Error.

Is there a way I can do this without doing much code changes myself?

Upvotes: 0

Views: 103

Answers (2)

Raja Anbazhagan
Raja Anbazhagan

Reputation: 4564

I wrapped the exception with a RuntimeException and thrown it to it's caller.

catch (Exception e) {
            throw new RuntimeException(e);
}

Upvotes: 0

Gary Russell
Gary Russell

Reputation: 174809

It's not clear what you mean by

because the parent method will cause problem

The container calls onMessage() and your other code is not involved. In any case, even if you requeue it, the message will be immediately redelivered.

For a fatal error like badly formed JSON, you should probably set up a dead-letter exchange/queue and route the message there by throwing an AmqpRejectAndDontRequeueException.

Or, use a RabbitTemplate to send the bad message to the DLQ.

Upvotes: 1

Related Questions