覆盖错误通道在 @MessagingGateway 中配置



我已经将@MessagingGateway配置为使用错误通道,该通道按预期工作。

@MessagingGateway(errorChannel = "DefaultInboundErrorHandlerChannel")
public interface InboundMessagingGateway {
@Gateway(requestChannel = "InboundEntryChannel")
void receive(XferRes response);
}

在流程中,我将对象传递给变压器,如下所示:

第 1 步:

@Transformer(inputChannel = "InboundEntryChannel", outputChannel = "TransmissionLogChannel")
public CassandraEntity createEntity(
org.springframework.messaging.Message<XferRes> message) throws ParseException {
XferRes response = message.getPayload();
CassandraEntity entity = new CassandraEntity();
// ... getters & setter ommitted for brevity
return entity;
}

接下来,我按如下方式更新实体:第 2 步:

@ServiceActivator(inputChannel = "TransmissionLogChannel", outputChannel="PublishChannel")
public XferRes updateCassandraEntity(
org.springframework.messaging.Message<XferRes> message) {
XferRes response = message.getPayload();
this.cassandraServiceImpl.update(response);
return response;
}

最后,我发布到Kafka主题如下:第 3 步:

@ServiceActivator(inputChannel = "PublishChannel")
public void publish(org.springframework.messaging.Message<XferRes> message){
XferRes response = message.getPayload();
publisher.post(response);       
}

如果发生错误,我将消息发布到发布错误对象的服务以记录摄取:

@ServiceActivator(inputChannel="defaultInboundErrorHandlerChannel")
public void handleInvalidRequest(org.springframework.messaging.Message<MessageHandlingException> message) throws ParseException {
XferRes originalRequest = (XferRes) message.getPayload().getFailedMessage().getPayload();
this.postToErrorBoard(originalRequest)
}

如果在步骤2:更新数据库时发生错误,那么我也想调用步骤 3。一个简单的方法是删除步骤2并从步骤 1调用更新数据库。

在 Spring 集成中还有其他方法可以调用步骤 3,无论是否发生错误。

这种技术称为PublishSubscribeChannel。由于我看到您在第二步重用有效负载以发送到第三步,因此它绝对是PublishSubscribeChannel和两个顺序订阅者的用例。

我的意思是你创建了一个PublishSubscribeChannel@Bean,那些@ServiceActivator是使用这个频道的名称。

更多信息请参阅参考手册。注意ignoreFailures属性:

/**
* Specify whether failures for one or more of the handlers should be
* ignored. By default this is <code>false</code> meaning that an Exception
* will be thrown whenever a handler fails. To override this and suppress
* Exceptions, set the value to <code>true</code>.
* @param ignoreFailures true if failures should be ignored.
*/
public void setIgnoreFailures(boolean ignoreFailures) {

最新更新