我编写了一个异常处理程序类,它查看默认 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> message
in时我可以得到handleTradeError()
。
我该如何改进这种方法,以便也处理此类错误,或者tradeprocessingContext
也从这种类型中提取底层?