Thursday, May 23, 2013

When Amazon SQS throws Exception..


When exception occurs when try to execute some data from AWS SQS we can rollback the changes/ do any other stuff like this

1. Write a call extending SimpleMessageListenerContainer

public class BoundryMessageListenerContainer extends SimpleMessageListenerContainer {
protected void rollbackOnExceptionIfNecessary(Session session, Throwable ex) throws JMSException {
try {
if (session != null & ex.getMessage() != null) {

JmsUtils.rollbackIfNecessary(session);

String message = ex.getLocalizedMessage();

if (message != null & message.length() > 0) {
// DO WHAT EVER YOU NEED TO DO 
}
}
} catch (IllegalStateException ex2) {
} catch (JMSException ex2) {
} catch (RuntimeException ex2) {
} catch (Error err) {
}
}
}

2. Add this to spring configuration

<bean id="container"
class="...........BoundryMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="messageListener" ref="listener" />
<property name="destination" ref="boundryQueue" />
</bean>