GH-3107: Add errorOnTimeout for TcpInboundGateway

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

The `MessagingGatewaySupport` has an `errorOnTimeout` option to throw
a `MessageTimeoutException` when downstream reply doesn't come back in
time for configured reply timeout

* Expose an `errorOnTimeout` option as a `TcpInboundGateway` ctor
property
* Add new factory methods into a `Tcp` factory for Java DSL
* Ensure a property works as expected in the `IpIntegrationTests`
* Document a new option

* Add a setter for MessagingGatewaySupport.errorOnTimeout option
* Expose an `errorOnTimeout` option on the DSL's `MessagingGatewaySpec`
making all the out-of-the-box inbound gateways possible to react to the
`MessageTimeoutException` when no reply during reply timeout
* Propagate properly `errorOnTimeout` in the `JmsInboundGateway`
* Modify docs respectively

* Improve docs about `errorOnTimeout`
This commit is contained in:
Artem Bilan
2019-11-12 15:08:01 -05:00
committed by Gary Russell
parent 0bbdd3a5f7
commit 72f7c72392
10 changed files with 160 additions and 59 deletions

View File

@@ -142,6 +142,10 @@ public class ChannelPublishingJmsMessageListener
this.gatewayDelegate.setReplyTimeout(replyTimeout);
}
public void setErrorOnTimeout(boolean errorOnTimeout) {
this.gatewayDelegate.setErrorOnTimeout(errorOnTimeout);
}
@Override
public void setShouldTrack(boolean shouldTrack) {
this.gatewayDelegate.setShouldTrack(shouldTrack);

View File

@@ -90,6 +90,12 @@ public class JmsInboundGateway extends MessagingGatewaySupport implements Orderl
this.endpoint.getListener().setReplyTimeout(replyTimeout);
}
@Override
public void setErrorOnTimeout(boolean errorOnTimeout) {
super.setErrorOnTimeout(errorOnTimeout);
this.endpoint.getListener().setErrorOnTimeout(errorOnTimeout);
}
@Override
public void setShouldTrack(boolean shouldTrack) {
super.setShouldTrack(shouldTrack);
@@ -108,6 +114,8 @@ public class JmsInboundGateway extends MessagingGatewaySupport implements Orderl
this.endpoint.setShutdownContainerOnStop(shutdownContainerOnStop);
}
@Override
public String getComponentType() {
return this.endpoint.getComponentType();

View File

@@ -28,10 +28,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import javax.jms.ConnectionFactory;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.logging.log4j.Level;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -39,6 +36,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.MessageTimeoutException;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.annotation.MessagingGateway;
@@ -56,11 +54,12 @@ import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.MessageChannels;
import org.springframework.integration.dsl.Pollers;
import org.springframework.integration.endpoint.MethodInvokingMessageSource;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
import org.springframework.integration.jms.ActiveMQMultiContextTests;
import org.springframework.integration.jms.JmsDestinationPollingSource;
import org.springframework.integration.scheduling.PollerMetadata;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.rule.Log4j2LevelAdjuster;
import org.springframework.integration.test.condition.LogLevels;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
@@ -76,7 +75,7 @@ import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.InterceptableChannel;
import org.springframework.stereotype.Component;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.transaction.PlatformTransactionManager;
/**
@@ -86,7 +85,9 @@ import org.springframework.transaction.PlatformTransactionManager;
*
* @since 5.0
*/
@RunWith(SpringRunner.class)
@SpringJUnitConfig
@LogLevels(level = "debug",
categories = { "org.springframework", "org.springframework.integration", "org.apache" })
@DirtiesContext
public class JmsTests extends ActiveMQMultiContextTests {
@@ -145,10 +146,6 @@ public class JmsTests extends ActiveMQMultiContextTests {
@Autowired
private CountDownLatch redeliveryLatch;
@Rule
public final Log4j2LevelAdjuster adjuster = Log4j2LevelAdjuster.forLevel(Level.DEBUG)
.categories("org.springframework", "org.springframework.integration", "org.apache");
@Test
public void testPollingFlow() {
this.controlBus.send("@'jmsTests.ContextConfiguration.integerMessageSource.inboundChannelAdapter'.start()");
@@ -230,6 +227,20 @@ public class JmsTests extends ActiveMQMultiContextTests {
.isEqualTo("HELLO THROUGH THE JMS PIPELINE");
assertThat(this.jmsInboundGatewayChannelCalled.get()).isTrue();
message = MessageBuilder.withPayload("junk")
.setReplyChannel(replyChannel)
.setHeader("destination", "jmsPipelineTest")
.build();
this.jmsOutboundGatewayChannel.send(message);
receive = replyChannel.receive(5000);
assertThat(receive)
.isNotNull()
.extracting(Message::getPayload)
.isEqualTo("error: junk is not convertible");
}
@Test
@@ -406,9 +417,22 @@ public class JmsTests extends ActiveMQMultiContextTests {
return IntegrationFlows.from(
Jms.inboundGateway(jmsConnectionFactory())
.requestChannel(jmsInboundGatewayInputChannel())
.replyTimeout(1)
.errorOnTimeout(true)
.errorChannel(new FixedSubscriberChannel(new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
return "error: " +
((MessageTimeoutException) requestMessage.getPayload())
.getFailedMessage().getPayload() + " is not convertible";
}
}))
.destination("jmsPipelineTest")
.configureListenerContainer(c ->
c.transactionManager(mock(PlatformTransactionManager.class))))
.filter(payload -> !"junk".equals(payload))
.<String, String>transform(String::toUpperCase)
.get();
}