英文:
Handle exceptions/errors other than MessagingException ie.., other error/exception which are not wrapped as MessagingException in spring-integration
问题
以下是您提供的代码部分的翻译:
在 Spring Integration 中,我的代码故意抛出 SourceAssertionError。在 spring-integration 的 5.1.6.release 版本中,对于任何错误,之前在 RequestHandlerRetryAdvice 中,无论出现什么错误,都会将其包装在 MessagingException 中。在 RequestHandlerRetryAdvice 的 doInvoke() 方法中如下所示:
@Override
protected Object doInvoke(final ExecutionCallback callback, Object target, final Message<?> message)
throws Exception {
RetryState retryState = null;
retryState = this.retryStateGenerator.determineRetryState(message);
messageHolder.set(message);
try {
return this.retryTemplate.execute(context -> callback.cloneAndExecute(), this.recoveryCallback, retryState);
}
catch (MessagingException e) {
if (e.getFailedMessage() == null) {
throw new MessagingException(message, "Failed to invoke handler", e);
}
throw e;
}
catch (Exception e) {
throw new MessagingException(message, "Failed to invoke handler", unwrapExceptionIfNecessary(e));
}
finally {
messageHolder.remove();
}
}
但是在 5.3.2.Release 版本中,现在不再将其包装在 MessagingException 中,而是作为 ThrowableHolderException 抛出,如下所示:
@Override
protected Object doInvoke(final ExecutionCallback callback, Object target, final Message<?> message) {
RetryState retryState = this.retryStateGenerator.determineRetryState(message);
MESSAGE_HOLDER.set(message);
try {
return this.retryTemplate.execute(context -> callback.cloneAndExecute(), this.recoveryCallback, retryState);
}
catch (MessagingException e) {
if (e.getFailedMessage() == null) {
throw new MessagingException(message, "Failed to invoke handler", e);
}
throw e;
}
catch (ThrowableHolderException e) { // NOSONAR catch and rethrow
throw e;
}
catch (Exception e) {
throw new ThrowableHolderException(e);
}
finally {
MESSAGE_HOLDER.remove();
}
}
现在,在进行 spring-integration 升级后,我的代码流程变为错误(非 MessagingException)不会被路由到错误通道。在 RequestRetryHandlerAdvice 中的 doInvoke() 方法抛出的异常在路由到错误通道之前被拒绝,因为逻辑要求它们是特定类型,即如果异常仅为 MessagingException,则将其路由到错误通道。在 5.3.2.Release 版本中,与升级前不同,我的错误已被包装为 MessagingException,并且流程一直到达 MessagingGatewaySupport 中的 handleSendAndReceiveError(..) 方法,在该方法中通常会被路由到自定义定义的错误通道。但在升级后,流程在此之前就已经结束了。
我的 Spring 配置非常简单,我只定义了一个自定义的错误通道,仅用于网关。
<int:gateway id="gateway" error-channel="myErrorChannel" />
另外,我为此错误通道定义了一个转换器。现在我的问题是如何处理此错误?我是否可以通过将其路由到新定义的错误通道来处理,或者是否可以将其处理到已经定义的 myErrorChannel?
编辑 1:如 Artem Bilan 所要求,添加配置文件。
<bean id="myPreferences" class="package.MyPreferences" />
<bean id="myExceptionTransformer" class="package.myExceptionTransformer" />
<int:channel id="myErrorChannel" />
<int:transformer input-channel="myErrorChannel" ref="myExceptionTransformer" />
<bean id="Logger" class="package.Logger" />
<int:gateway id="gateway" error-channel="myErrorChannel"
service-interface="package.myClient"
default-request-channel="myRoutingChannel">
<!-- 方法配置 -->
</int:gateway>
<int:publish-subscribe-channel id="myRoutingChannel" />
<int:header-value-router input-channel="myRoutingChannel"
header-name="#{T(package.AppContext).REQUEST_TYPE}">
<int:mapping value="#{T(value1)}" channel="authGatewayChannel" />
<int:mapping value="#{T(value2)}" channel="provGatewayChannel" />
</int:header-value-router>
<int-ws:request-handler-advice-chain>
<ref bean="provRetryAdvice" />
</int-ws:request-handler-advice-chain>
</int-ws:outbound-gateway>
<bean id="provRetryAdvice" class="org.springframework.integration.handler.advice.RequestHandlerRetryAdvice">
<!-- 配置 -->
</bean>
我的转换器定义如下:
@Transformer
public Message<?> transformErrorToResponse(Message<?> exceptionMessage) {
// 转换逻辑
return errorMessage;
}
这些是您提供的代码部分的翻译。如果您有关于代码部分的任何问题,请随时问我。
英文:
My code deliberately throws a SourceAssertionError. Earlier in 5.1.6.release of spring-integraton for any error , in RequestHandlerRetryAdvice, for any error, it would wrap it in MessagingException. In Below doInvoke() of RequestHandlerRetryAdvice,
@Override
protected Object doInvoke(final ExecutionCallback callback, Object target, final Message<?> message)
throws Exception {
RetryState retryState = null;
retryState = this.retryStateGenerator.determineRetryState(message);
messageHolder.set(message);
try {
return this.retryTemplate.execute(context -> callback.cloneAndExecute(), this.recoveryCallback, retryState);
}
catch (MessagingException e) {
if (e.getFailedMessage() == null) {
throw new MessagingException(message, "Failed to invoke handler", e);
}
throw e;
}
catch (Exception e) {
throw new MessagingException(message, "Failed to invoke handler", unwrapExceptionIfNecessary(e));
}
finally {
messageHolder.remove();
}
}
but now in 5.3.2.Release, its not wrapped in MEssagingException but rather thrown as THrowableHolderException as seen below
@Override
protected Object doInvoke(final ExecutionCallback callback, Object target, final Message<?> message) {
RetryState retryState = this.retryStateGenerator.determineRetryState(message);
MESSAGE_HOLDER.set(message);
try {
return this.retryTemplate.execute(context -> callback.cloneAndExecute(), this.recoveryCallback, retryState);
}
catch (MessagingException e) {
if (e.getFailedMessage() == null) {
throw new MessagingException(message, "Failed to invoke handler", e);
}
throw e;
}
catch (ThrowableHolderException e) { // NOSONAR catch and rethrow
throw e;
}
catch (Exception e) {
throw new ThrowableHolderException(e);
}
finally {
MESSAGE_HOLDER.remove();
}
}
Now after spring-integration upgrade my code flow goes in such a way that error(Not MessagingException) is not being routed to error-channel. The exception thrown by doInvoke() in RequestRetryHandlerAdvice is rejected before its routed to error channel because the logic required them to be of particular type i.e., if only the exception is MessaginException then its routed to error-channel. The flow exits with exception thrown by rethrowExceptionCauseIfPossible(..) in GatewayProxyFactoryBean. Before upgrade my error had been wrapped to MessagingException and flow reaches till handleSendAndReceiveError(..) in MessagingGatewaySupport where it usualy is routed to custom defined error-channel. This is not happening after upgrade to 5.3.2.Release as flow ends before itself
my spring configuration is very straightforward, I just have one custom error channel defined only to gateway.
<int:gateway id="gateway" error-channel="myErrorChannel". and defined a transformer for this error-channel
Now my question is how can I handle this error? is there anyway I could handle it by routing to any newly defined error-channel or can I do it to my already defined myErrorChannel?
EDIT1: Adding config file as Artem Bilan Requested
<bean id="myPreferences" class="package.MyPreferences" />
<bean id="myExceptionTransformer"
class="package.myExceptionTransformer" />
<int:channel id="myErrorChannel" />
<int:transformer input-channel="myErrorChannel"
ref="myExceptionTransformer" />
<bean id="Logger" class="package.Logger" />
<int:gateway id="gateway" error-channel="myErrorChannel"
service-interface="package.myClient"
default-request-channel="myRoutingChannel">
<int:method name="invokeAuthenticationRequest">
<int:header
name="#{T(name1)}"
value="#{T(value1)}" />
</int:method>
<int:method name="invokeProvRequest">
<int:header
name="#{T(name2)}"
value="#{T(value2)}" />
</int:method>
</int:gateway>
<int:publish-subscribe-channel id="myRoutingChannel" />
<int:header-value-router input-channel="myRoutingChannel"
header-name="#{T(package.AppContext).REQUEST_TYPE}">
<int:mapping
value="#{T(value1)}"
channel="authGatewayChannel" />
<int:mapping
value="#{T(value2)}"
channel="provGatewayChannel" />
</int:header-value-router>
<int-ws:request-handler-advice-chain>
<ref bean="provRetryAdvice" />
</int-ws:request-handler-advice-chain>
</int-ws:outbound-gateway>
<bean id="provRetryAdvice"
class="org.springframework.integration.handler.advice.RequestHandlerRetryAdvice">
<property name="retryTemplate">
<bean class="org.springframework.retry.support.RetryTemplate">
<property name="backOffPolicy">
<bean class="org.springframework.retry.backoff.FixedBackOffPolicy">
<property name="backOffPeriod" value="4000" />
</bean>
</property>
<property name="retryPolicy">
<bean class="org.springframework.retry.policy.SimpleRetryPolicy">
<property name="maxAttempts" value="#{provisioningRetryCount.intValue()}" />
</bean>
</property>
</bean>
</property>
</bean>
And my Transformer is defined as
@Transformer
public Message<?> transformErrorToResponse(Message<?> exceptionMessage) {
Object exceptionMessagePayload = exceptionMessage.getPayload();
Throwable cause = ((MessagingException) exceptionMessagePayload).getCause();
newPayload = new myException(msg);
/*add headers*/
return errorMessage;
}
答案1
得分: 1
"regression"的处理已经通过此更改完成:https://github.com/spring-projects/spring-integration/commit/b187bca36e2565e80e72e662c204a57c05aab11d。
因此,是的,我们不再针对常规异常抛出MessagingException
包装,以避免不必要的较大堆栈跟踪。
我不太确定您的异常如何无法到达该错误通道。MessagingGatewaySupport
中的代码如下:
try {
...
reply = this.messagingTemplate.sendAndReceive(channel, requestMessage);
...
}
catch (Exception ex) {
...
reply = ex;
...
}
if (reply instanceof Throwable || reply instanceof ErrorMessage) {
Throwable error =
reply instanceof ErrorMessage
? ((ErrorMessage) reply).getPayload()
: (Throwable) reply;
return handleSendAndReceiveError(object, requestMessage, error, shouldConvert);
}
可能是您错误处理程序中的逻辑仅检查接收到的ErrorMessage
中的MessagingException
,这种情况不再适用...
我们可以查看您的配置,以确保发生了什么,并可能复现以引导您找到适当的解决方案吗?
英文:
The "regression" has been done with this change: https://github.com/spring-projects/spring-integration/commit/b187bca36e2565e80e72e662c204a57c05aab11d.
So, yes, we don't throw a MessagingException
wrapper for regular exceptions any more to avoid bigger stack trace for nothing.
I'm not fully sure how your exception cannot reach that error channel. The code in the MessagingGatewaySupport
is like this:
try {
...
reply = this.messagingTemplate.sendAndReceive(channel, requestMessage);
...
}
catch (Exception ex) {
...
reply = ex;
...
}
if (reply instanceof Throwable || reply instanceof ErrorMessage) {
Throwable error =
reply instanceof ErrorMessage
? ((ErrorMessage) reply).getPayload()
: (Throwable) reply;
return handleSendAndReceiveError(object, requestMessage, error, shouldConvert);
}
Probably the logic in your error handler to check only for the MessagingException
in the received ErrorMessage
which is not the case any more...
May we see your configuration to be sure what is going on and maybe reproduce to lead you to the proper solution?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论