java如何从outboundgateway向发送方抛出异常
我一直在互联网上搜索,并尝试了许多不同的方法,但我无法让它工作
我想要的是捕获从出站端抛出的异常,例如一些验证,当异常没有成功传递时,它应该向发送方返回异常。我不知道是否可以不使用DirectChannel从服务器发送请求
我希望有人能帮助我
在下面的代码中,我向您展示了我的SI配置,其中带有SPEL表达式“checkRemoteOutputHeaders”的过滤器在执行时抛出异常
<int:channel id="requestChannelSb">
<int:interceptors>
<int:wire-tap channel="timeStampCalllogger" />
</int:interceptors>
</int:channel>
<task:executor id="threadPoolTaskExecutor" pool-size="10" queue-capacity="100" rejection-policy="DISCARD_OLDEST" />
<bean id="taskExecutor" class="org.springframework.integration.util.ErrorHandlingTaskExecutor">
<constructor-arg name="executor" ref="threadPoolTaskExecutor"/>
<constructor-arg name="errorHandler">
<bean class="org.springframework.integration.channel.MessagePublishingErrorHandler">
<property name="defaultErrorChannel" ref="errorChannel"/>
</bean>
</constructor-arg>
</bean>
<int:channel id="gatewayChannelSb">
<int:dispatcher task-executor="taskExecutor" failover="false"/>
<int:interceptors>
<int:wire-tap channel="timeStampInitlogger" />
</int:interceptors>
</int:channel>
<int:channel id="responseChannelSb">
<int:interceptors>
<int:wire-tap channel="timeStampEndlogger" />
</int:interceptors>
</int:channel>
<int:channel id="errorChannel">
<int:interceptors>
<int:wire-tap channel="errorlogger"/>
</int:interceptors>
</int:channel>
<int-http:outbound-gateway request-channel="requestChannelSb"
url="{urlVar}" http-method="POST"
extract-request-payload="true"
expected-response-type="java.lang.String"
charset="UTF-8"
header-mapper="headerMapper"
reply-channel="responseChannelSb"
request-timeout="60000">
<int-http:uri-variable name="urlVar" expression="T(gnf.servicebroker.util.Utils).getUrl(headers)" />
</int-http:outbound-gateway>
<int:chain input-channel="responseChannelSb">
<int:header-enricher>
<int:header name="#{T(gnf.servicebroker.util.Constantes$HEADERS).BEAN}" expression="headers[T(gnf.servicebroker.util.Constantes$HEADERS).BEAN]"/>
</int:header-enricher>
<int:transformer ref="customJsonToObjectTransformer"/>
</int:chain>
<int:chain id="chain" input-channel="gatewayChannelSb" output-channel="requestChannelSb">
<int:header-enricher>
<int:error-channel ref="errorChannel" overwrite="true"/>
<int:header name="#{T(gnf.servicebroker.util.Constantes$HEADERS).REQUEST_ID}" expression="headers[T(gnf.servicebroker.util.Constantes$HEADERS).ID].toString()"/>
</int:header-enricher>
<int:filter expression="T(gnf.servicebroker.util.Utils).checkRemoteOutputHeaders(headers)" discard-channel="errorChannel" />
<int:header-enricher>
<int:header name="#{T(gnf.servicebroker.util.Constantes$HEADERS).CONTENT_TYPE}" value="text/x-json" />
<int:header name="#{T(gnf.servicebroker.util.Constantes$HEADERS).ACCEPT}" value="text/x-json" />
<int:header name="#{T(gnf.servicebroker.util.Constantes$HEADERS).BEAN}" expression="T(gnf.servicebroker.util.Utils).getPayloadCanonicalClassName(payload)" />
<int:header name="#{T(gnf.servicebroker.util.Constantes$HEADERS).SECURITY_USER}" expression="T(gnf.servicebroker.util.Utils).getSecurityUser()"/>
<int:header name="#{T(gnf.servicebroker.util.Constantes$HEADERS).TARGET}" expression="T(gnf.servicebroker.util.Utils).getTargetUrl()"/>
</int:header-enricher>
<int:object-to-json-transformer object-mapper="jsonObjectMapper"/>
</int:chain>
<!-- Logging channels -->
<int:logging-channel-adapter id="timeStampInitlogger" level="INFO" expression="'Inicio de la peticion: '.concat(T(gnf.servicebroker.util.Utils).getRequestInfoTrace(headers))"/>
<int:logging-channel-adapter id="timeStampCalllogger" level="INFO" expression="'Envio de la peticion: '.concat(T(gnf.servicebroker.util.Utils).getRequestInfoTrace(headers))"/>
<int:logging-channel-adapter id="timeStampEndlogger" level="INFO" expression="'Final de la peticion: '.concat(T(gnf.servicebroker.util.Utils).getRequestInfoTrace(headers))"/>
<int:logging-channel-adapter id="destinationCalllogger" level="INFO" expression="'Destino peticion: '.concat(headers[T(gnf.servicebroker.util.Constantes$HEADERS).SUBSYSTEM])"/>
<int:logging-channel-adapter id="errorlogger" level="ERROR" expression="'ERROR: '.concat(T(gnf.servicebroker.util.Utils).getRequestInfoTrace(headers))"/>
<!-- End Logging channels -->
<int:chain input-channel="errorChannel" output-channel="responseChannelSb">
<int:transformer ref="messageHandlingExceptionTransformer"/>
</int:chain>
在以下代码中,我向您展示了转换器“messageHandlingExceptionTransformer”,它将MessagingException转换为ServiceException(自定义异常):
@Transformer
public Message<ServiceException> transform(ErrorMessage message){
LOGGER.debug("INIT - transform(message=" + message + ")");
MessagingException messageException = (MessagingException) message.getPayload();
ServiceException serviceEx = new ServiceException(messageException.getMessage(), messageException.getCause());
Message<?> originalMsg = messageException.getFailedMessage();
Message<ServiceException> transformedObj = MessageBuilder.withPayload(serviceEx).copyHeaders(originalMsg.getHeaders()).build();
LOGGER.debug("END - transform=" + transformedObj);
return transformedObj;
}
另一个转换器“customJsonToObjectTransformer”只是将字符串有效负载(JSON)转换为Java对象并返回它,但是如果有效负载不是字符串,例如异常,则直接返回它
先走一步
# 1 楼答案
最后,我找到了调用者在出站生命周期中没有收到抛出的异常的原因
我只是一个配置问题,因为在另一个XML文件中,我有一个网关定义,该定义指示其错误通道,该通道与出站配置中定义的通道相同。因此,它产生了一个循环调用链,因为在出站时,ErrorMessage(例外情况)在errorChannel中管理一次,然后在网关的errorChannel中再次管理,这是相同的,因此请愿书再次进入管理errorChannel的链中,最后它在不通知调用方的情况下失败
请注意error channel属性,该属性的值与出站配置中的error channel definend相同,因此删除该属性调用方可以捕获异常:
我希望这对任何人都有帮助
谢谢大家