GH-8705 Expose errorOnTimeout on MessagingGateway

Fixes https://github.com/spring-projects/spring-integration/issues/8705

an internal `MethodInvocationGateway` is a `MessagingGatewaySupport`
extension with all the logic available there.
One of the option introduced in `5.2.2` to be able to throw a `MessageTimeoutException`
instead of returning `null` when no reply received in time from downstream flow

* Expose an `errorOnTimeout` on the `@MessagingGateway` and `GatewayEndpointSpec`
* Propagate this option from a `GatewayProxyFactoryBean` down to its internal
`MethodInvocationGateway` implementation
* Modify couple tests to react for `errorOnTimeout` set to `true`
* Document the feature

Fix language in Docs

Co-authored-by: Gary Russell <grussell@vmware.com>
This commit is contained in:
Artem Bilan
2023-08-18 13:46:28 -07:00
parent d85c5e3a0a
commit 4e310bbad6
9 changed files with 85 additions and 12 deletions

View File

@@ -23,6 +23,7 @@ import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.gateway.MessagingGatewaySupport;
/**
* A stereotype annotation to provide an Integration Messaging Gateway Proxy
@@ -165,4 +166,13 @@ public @interface MessagingGateway {
*/
boolean proxyDefaultMethods() default false;
/**
* If errorOnTimeout is true, null won't be returned as a result of a gateway method invocation when a timeout occurs.
* Instead, a {@link org.springframework.integration.MessageTimeoutException} is thrown
* or an error message is published to the error channel.
* @since 6.2
* @see MessagingGatewaySupport#setErrorOnTimeout(boolean)
*/
boolean errorOnTimeout() default false;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 the original author or authors.
* Copyright 2016-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -98,4 +98,16 @@ public class GatewayEndpointSpec extends ConsumerEndpointSpec<GatewayEndpointSpe
return this;
}
/**
* Set a error on timeout flag.
* @param errorOnTimeout true to produce an error in case of a reply timeout.
* @return the spec.
* @since 6.2
* @see org.springframework.integration.gateway.GatewayProxyFactoryBean#setErrorOnTimeout(boolean)
*/
public GatewayEndpointSpec errorOnTimeout(boolean errorOnTimeout) {
this.handler.setErrorOnTimeout(errorOnTimeout);
return this;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 the original author or authors.
* Copyright 2017-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -128,6 +128,8 @@ public class AnnotationGatewayProxyFactoryBean<T> extends GatewayProxyFactoryBea
populateAsyncExecutorIfAny();
setErrorOnTimeout(this.gatewayAttributes.getBoolean("errorOnTimeout"));
boolean proxyDefaultMethods = this.gatewayAttributes.getBoolean("proxyDefaultMethods");
if (proxyDefaultMethods) { // Override only if annotation attribute is different
setProxyDefaultMethods(true);

View File

@@ -81,6 +81,10 @@ public class GatewayMessageHandler extends AbstractReplyProducingMessageHandler
this.gatewayProxyFactoryBean.setDefaultReplyTimeout(replyTimeout);
}
public void setErrorOnTimeout(boolean errorOnTimeout) {
this.gatewayProxyFactoryBean.setErrorOnTimeout(errorOnTimeout);
}
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
if (this.exchanger == null) {

View File

@@ -166,6 +166,8 @@ public class GatewayProxyFactoryBean<T> extends AbstractEndpoint
private MetricsCaptor metricsCaptor;
private boolean errorOnTimeout;
/**
* Create a Factory whose service interface type can be configured by setter injection.
* If none is set, it will fall back to the default service interface type,
@@ -455,6 +457,18 @@ public class GatewayProxyFactoryBean<T> extends AbstractEndpoint
this.gatewayMap.values().forEach(gw -> gw.registerMetricsCaptor(metricsCaptorToRegister));
}
/**
* If errorOnTimeout is true, null won't be returned as a result of a gateway method invocation, when a timeout occurs.
* Instead, a {@link org.springframework.integration.MessageTimeoutException} is thrown
* or an error message is published to the error channel.
* @param errorOnTimeout true to create the error message on reply timeout.
* @since 6.2
* @see MessagingGatewaySupport#setErrorOnTimeout(boolean)
*/
public void setErrorOnTimeout(boolean errorOnTimeout) {
this.errorOnTimeout = errorOnTimeout;
}
@Override
@SuppressWarnings("unchecked")
protected void onInit() {
@@ -881,6 +895,7 @@ public class GatewayProxyFactoryBean<T> extends AbstractEndpoint
gateway.setBeanFactory(getBeanFactory());
gateway.setShouldTrack(this.shouldTrack);
gateway.registerMetricsCaptor(this.metricsCaptor);
gateway.setErrorOnTimeout(this.errorOnTimeout);
gateway.afterPropertiesSet();
return gateway;