Question

I am using spring batch and spring amqp. I am producing messages in my spring batch ItemWriter using

public class ImportItemWriter<T> implements ItemWriter<T> {

    private AmqpTemplate template;

    public AmqpTemplate getTemplate() {
        return template;
    }

    public void setTemplate(AmqpTemplate template) {
        this.template = template;
    }

    public void write(List<? extends T> items) throws Exception {
        for (T item : items) {
            template.convertSendAndReceive(item.toString());                
        }
    }

}

and at consumer side, I am using POJO to handle messages using MessageListenerAdapter.

public class ImportMessageListener{

    @Override
    public String handle(String exchange) throws Exception {
        throw new Exception("Command Failed.");
    }

}

I want template.convertSendAndReceive(item.toString()); to throw the same exception thrown by message handler so that spring batch can stop the batch , mark it as failed and log the exception?

I had look at this but couldn't get how to achieve above use case ?

http://docs.spring.io/spring-amqp/docs/1.3.1.RELEASE/reference/html/amqp.html

How do I do this ?

Was it helpful?

Solution

There's no general mechanism to do that, although we are looking into it for a future release of Spring Integration.

As long as your exception is serializable, you can send it as the result and test the payload type on the sending system...

public Object handle(String foo) {
    return new MySerializableException("Command failed");
}

And, on the receiving side...

if (result instanceof Exception) {
    throw (Exception) result;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top