@Service
public class TradeService {
@Autowired
private JmsMessageSender jmsMessageSender;
@Async
public Future<TradeProcessingContext> submitTradeForProcessing(final Trade trade) {
TradeProcessingContext tradeProcessingContext = new TradeProcessingContext();
try {
send(trade);
updateStatus(tradeProcessingContext);
tradeProcessingContext.setEventState(TradeStatus.SUCCESS);
return new AsyncResult<>(tradeProcessingContext);
} catch (TradeProcessingException ex) {
tradeProcessingContext.setEventState(TradeStatus.FAILED);
return new AsyncResult<>(tradeProcessingContext);
}
}
private void send(final Trade trade) throws TradeProcessingException {
Optional<String> xmlTradeMessage = XmlParserUtil.marshall(trade);
if (xmlTradeMessage.isPresent()) {
try {
jmsMessageSender.sendMessage(aqname, xmlTradeMessage.get());
} catch (Exception e) {
log.error("Sending message failed, due to : " + e, e);
throw new TradeProcessingException(e);
}
}
}
}
我有上述Service
课程,由我的Service Activators
调用。
我试图理解我应该如何在spring集成应用程序中处理错误。
例如,我想处理不同类型的异常并确定最佳行动方案,即send()
将是与jms相关的异常,updateStatus()
将是与数据库相关的异常。
这些操作中的任何一个都可能失败,在这种情况下我想从错误通道中选择它们但我不确定如何构造和插入它。我有一个处理程序:
@MessageEndpoint
public class Handler {
@ServiceActivator(inputChannel = TRADE_CHANNEL)
public void handle(final Trade trade) {
submitTradeForProcessing(trade)
}
}
和Gateaway如下:
@MessagingGateway
public interface TradeGateaway {
@Gateway(requestChannel = "tradeChannel")
void processEvent(Trade trade);
@Gateway(requestChannel = "processUnprocessed")
void unprocessedEvent(String eventID);
}
答案 0 :(得分:0)
对于不同类型的例外,您可以考虑使用ErrorMessageExceptionTypeRouter
:
/**
* A Message Router that resolves the target {@link MessageChannel} for
* messages whose payload is a {@link Throwable}.
* The channel resolution is based upon the most specific cause
* of the error for which a channel-mapping exists.
* <p>
* The channel-mapping can be specified for the super classes to avoid mapping duplication
* for the particular exception implementation.
*
* @author Mark Fisher
* @author Oleg Zhurakousky
* @author Artem Bilan
*/
public class ErrorMessageExceptionTypeRouter extends AbstractMappingMessageRouter {
因此,您将此订阅到上述错误通道,并分别为每个特定异常执行逻辑。