1

我编写了一个异常处理程序类,它查看默认 Spring IntegrationerrorChannel的传入消息类型Message<TradeProcessingContext> message

@Slf4j
public class ExceptionHandler {

    @Autowired
    private ExceptionDAO exceptionDAO;


    @ServiceActivator(inputChannel = DEFAULT_ERROR_CHANNEL)
    public void handleTradeError(Message<TradeProcessingContext> message) {

        TradeProcessingContext tradeProcessingContext = message.getPayload();

        if (tradeProcessingContext != null) {
          //store in database
        }
    }

}

我有如下处理程序实现:

@Slf4j
@MessageEndpoint
public class ChildHandler extends ParentHandler {

    @Autowired
    private SomeDAO someDAO;

    @ServiceActivator(inputChannel = INPUT_CHANNEL, outputChannel = DEFAULT_ERROR_CHANNEL)
    public Message<TradeProcessingContext> handle(final Event event) {
        return process(event);
    }

    @Override
    protected TradeDAO getDAO() {
        return someDAO;
    }
}

这调用了父级process()

public abstract class ParentHandler implements Handler {

    @Resource
    private SomeService service;


    public Message<TradeProcessingContext> process(final Event event) {

        TradeProcessingContext tradeProcessingContext = new TradeProcessingContext();

        //set initial context

        try {
            List<Trade> trades = getDAO().findByEventId(event.getEventId());
            for (Trade trade : trades) {
                tradeProcessingContext.setRef(trade.getRef());
                Future<TradeProcessingContext> thread = service.doSomething(trade, tradeProcessingContext);
                tradeProcessingContext = thread.get();
            }
        } catch (Exception e) {
            return MessageBuilder.withPayload(tradeProcessingContext).build();
        }

        return null;
    }

我知道org.springframework.messaging.MessageHandlingException: nested exception is java.lang.ClassCastException当类型为Message<MessageHandlingException> messagein时我可以得到handleTradeError()

我该如何改进这种方法,以便也处理此类错误,或者tradeprocessingContext也从这种类型中提取底层?

4

1 回答 1

1

错误通道获取一个带有 Throwable 有效负载的 ErrorMessage。通常 Throwable 是消息处理异常,原始消息在 failedMessage 属性中,异常在原因中。

于 2018-03-22T21:07:43.063 回答