Search code examples
springrabbitmqamqpspring-amqp

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?


Solution

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

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