我正在使用 spring Batch 和 Spring amqp。我正在使用
在 Spring Batch ItemWriter 中生成消息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());
}
}
}
在消费者端,我使用 POJO 通过 MessageListenerAdapter 来处理消息。
public class ImportMessageListener{
@Override
public String handle(String exchange) throws Exception {
throw new Exception("Command Failed.");
}
}
我想要 template.convertSendAndReceive(item.toString());抛出消息处理程序抛出的相同异常,以便 Spring Batch 可以停止批处理,将其标记为失败并记录异常?
我看过这个,但不知道如何实现上述用例?
http://docs.spring.io/spring-amqp/docs/1.3.1.RELEASE/reference/html/amqp.html
我该怎么做?
最佳答案
虽然我们正在为Spring Integration的 future 版本进行研究,但没有通用的机制可以做到这一点。 .
只要您的异常是可序列化的,您就可以将其作为结果发送并在发送系统上测试有效负载类型...
public Object handle(String foo) {
return new MySerializableException("Command failed");
}
并且,在接收方......
if (result instanceof Exception) {
throw (Exception) result;
}
关于java - 如果 template.convertSendAndReceive 消费者消息处理程序抛出异常,则抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23192653/