Throw Exception if template.convertSendAndReceive consumer message handler throws exception?

StackOverflow https://stackoverflow.com/questions/23192653

  •  06-07-2023
  •  | 
  •  

문제

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 ?

도움이 되었습니까?

해결책

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top