1

我正在使用弹簧批处理和弹簧 amqp。我在我的春季批处理 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批处理可以停止批处理,将其标记为失败并记录异常?

我看过这个但不知道如何实现上述用例?

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

我该怎么做呢 ?

4

1 回答 1

1

没有通用的机制可以做到这一点,尽管我们正在为Spring Integration的未来版本进行研究。

只要您的异常是可序列化的,您就可以将其作为结果发送并在发送系统上测试有效负载类型...

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

而且,在接收方...

if (result instanceof Exception) {
    throw (Exception) result;
}
于 2014-04-21T13:08:36.690 回答