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

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

  •  06-07-2023
  •  | 
  •  

Domanda

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 ?

È stato utile?

Soluzione

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;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top