From 93bc11fca7480970c212696e4f0ba950716f0e20 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Tue, 31 Aug 2010 16:29:04 +0000 Subject: [PATCH] INT-1364 AbstractMessagingGateway's default request and reply timeouts are now 1000 ms (were indefinite). --- .../gateway/AbstractMessagingGateway.java | 17 ++++++--- .../gateway/SimpleMessagingGatewayTests.java | 17 ++++----- .../HttpInvokerInboundGatewayParserTests.java | 8 ++--- .../config/RmiInboundGatewayParserTests.java | 4 +-- .../SimpleWebServiceInboundGatewayTests.java | 35 +++++++++++-------- 5 files changed, 48 insertions(+), 33 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java b/spring-integration-core/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java index 60ff96fdbe..82d70dce74 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java @@ -43,14 +43,17 @@ import org.springframework.util.Assert; * @author Mark Fisher */ public abstract class AbstractMessagingGateway extends AbstractEndpoint { - + + private static final long DEFAULT_TIMEOUT = 1000L; + + private volatile InboundMessageMapper exceptionMapper; private volatile MessageChannel requestChannel; private volatile MessageChannel replyChannel; - private volatile long replyTimeout = 1000; + private volatile long replyTimeout = DEFAULT_TIMEOUT; private final MessagingTemplate messagingTemplate = new MessagingTemplate(); @@ -62,6 +65,12 @@ public abstract class AbstractMessagingGateway extends AbstractEndpoint { private final Object replyMessageCorrelatorMonitor = new Object(); + + public AbstractMessagingGateway() { + this.messagingTemplate.setSendTimeout(DEFAULT_TIMEOUT); + this.messagingTemplate.setReceiveTimeout(this.replyTimeout); + } + @Override public String getComponentType(){ return "gateway"; @@ -88,7 +97,7 @@ public abstract class AbstractMessagingGateway extends AbstractEndpoint { /** * Set the timeout value for sending request messages. If not - * explicitly configured, the default is an indefinite timeout. + * explicitly configured, the default is one second. * * @param requestTimeout the timeout value in milliseconds */ @@ -98,7 +107,7 @@ public abstract class AbstractMessagingGateway extends AbstractEndpoint { /** * Set the timeout value for receiving reply messages. If not - * explicitly configured, the default is an indefinite timeout. + * explicitly configured, the default is one second. * * @param replyTimeout the timeout value in milliseconds */ diff --git a/spring-integration-core/src/test/java/org/springframework/integration/gateway/SimpleMessagingGatewayTests.java b/spring-integration-core/src/test/java/org/springframework/integration/gateway/SimpleMessagingGatewayTests.java index 4c5debec87..0a6d3c23da 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/gateway/SimpleMessagingGatewayTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/gateway/SimpleMessagingGatewayTests.java @@ -17,6 +17,7 @@ package org.springframework.integration.gateway; import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.eq; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.getCurrentArguments; import static org.easymock.EasyMock.isA; @@ -73,7 +74,7 @@ public class SimpleMessagingGatewayTests { @Test public void sendMessage() { - expect(requestChannel.send(messageMock)).andReturn(true); + expect(requestChannel.send(messageMock, 1000L)).andReturn(true); replay(allmocks); this.simpleMessagingGateway.send(messageMock); verify(allmocks); @@ -82,7 +83,7 @@ public class SimpleMessagingGatewayTests { @Test(expected=MessageDeliveryException.class) public void sendMessage_failure() { expect(messageMock.getHeaders()).andReturn(new MessageHeaders(null)); - expect(requestChannel.send(messageMock)).andReturn(false); + expect(requestChannel.send(messageMock, 1000)).andReturn(false); replay(allmocks); this.simpleMessagingGateway.send(messageMock); verify(allmocks); @@ -90,7 +91,7 @@ public class SimpleMessagingGatewayTests { @Test public void sendObject() { - expect(requestChannel.send(isA(Message.class))).andAnswer(new IAnswer() { + expect(requestChannel.send(isA(Message.class), eq(1000L))).andAnswer(new IAnswer() { public Boolean answer() throws Throwable { assertEquals("test", ((Message) getCurrentArguments()[0]).getPayload()); return true; @@ -103,7 +104,7 @@ public class SimpleMessagingGatewayTests { @Test(expected=MessageDeliveryException.class) public void sendObject_failure() { - expect(requestChannel.send(isA(Message.class))).andAnswer(new IAnswer() { + expect(requestChannel.send(isA(Message.class), eq(1000L))).andAnswer(new IAnswer() { public Boolean answer() throws Throwable { assertEquals("test", ((Message) getCurrentArguments()[0]).getPayload()); return false; @@ -129,7 +130,7 @@ public class SimpleMessagingGatewayTests { @Test public void receiveMessage() { - expect(replyChannel.receive()).andReturn(messageMock); + expect(replyChannel.receive(1000)).andReturn(messageMock); expect(messageMock.getPayload()).andReturn("test").anyTimes(); replay(allmocks); assertEquals("test", this.simpleMessagingGateway.receive()); @@ -138,7 +139,7 @@ public class SimpleMessagingGatewayTests { @Test public void receiveMessage_null() { - expect(replyChannel.receive()).andReturn(null); + expect(replyChannel.receive(1000)).andReturn(null); replay(allmocks); assertNull(this.simpleMessagingGateway.receive()); verify(allmocks); @@ -149,7 +150,7 @@ public class SimpleMessagingGatewayTests { @Test public void sendObjectAndReceiveObject() { expect(replyChannel.receive(100)).andReturn(messageMock); - expect(requestChannel.send(isA(Message.class))).andReturn(true); + expect(requestChannel.send(isA(Message.class), eq(1000L))).andReturn(true); replay(allmocks); // TODO: if timeout is 0, this will fail occasionally this.simpleMessagingGateway.setReplyTimeout(100); @@ -191,7 +192,7 @@ public class SimpleMessagingGatewayTests { @Test public void sendObjectAndReceiveMessage() { expect(replyChannel.receive(100)).andReturn(messageMock); - expect(requestChannel.send(isA(Message.class))).andReturn(true); + expect(requestChannel.send(isA(Message.class), eq(1000L))).andReturn(true); replay(allmocks); // TODO: commenting the next line causes the test to hang this.simpleMessagingGateway.setReplyTimeout(100); diff --git a/spring-integration-httpinvoker/src/test/java/org/springframework/integration/httpinvoker/config/HttpInvokerInboundGatewayParserTests.java b/spring-integration-httpinvoker/src/test/java/org/springframework/integration/httpinvoker/config/HttpInvokerInboundGatewayParserTests.java index 4e9afe1ad2..15f8943097 100644 --- a/spring-integration-httpinvoker/src/test/java/org/springframework/integration/httpinvoker/config/HttpInvokerInboundGatewayParserTests.java +++ b/spring-integration-httpinvoker/src/test/java/org/springframework/integration/httpinvoker/config/HttpInvokerInboundGatewayParserTests.java @@ -45,8 +45,8 @@ public class HttpInvokerInboundGatewayParserTests { MessagingTemplate template = (MessagingTemplate) accessor.getPropertyValue("messagingTemplate"); DirectFieldAccessor templateAccessor = new DirectFieldAccessor(template); - assertEquals(-1L, templateAccessor.getPropertyValue("sendTimeout")); - assertEquals(-1L, templateAccessor.getPropertyValue("receiveTimeout")); + assertEquals(1000L, templateAccessor.getPropertyValue("sendTimeout")); + assertEquals(1000L, templateAccessor.getPropertyValue("receiveTimeout")); } @Test @@ -61,8 +61,8 @@ public class HttpInvokerInboundGatewayParserTests { MessagingTemplate template = (MessagingTemplate) accessor.getPropertyValue("messagingTemplate"); DirectFieldAccessor templateAccessor = new DirectFieldAccessor(template); - assertEquals(-1L, templateAccessor.getPropertyValue("sendTimeout")); - assertEquals(-1L, templateAccessor.getPropertyValue("receiveTimeout")); + assertEquals(1000L, templateAccessor.getPropertyValue("sendTimeout")); + assertEquals(1000L, templateAccessor.getPropertyValue("receiveTimeout")); } @Test diff --git a/spring-integration-rmi/src/test/java/org/springframework/integration/rmi/config/RmiInboundGatewayParserTests.java b/spring-integration-rmi/src/test/java/org/springframework/integration/rmi/config/RmiInboundGatewayParserTests.java index c6ec94a32a..fff46974c7 100644 --- a/spring-integration-rmi/src/test/java/org/springframework/integration/rmi/config/RmiInboundGatewayParserTests.java +++ b/spring-integration-rmi/src/test/java/org/springframework/integration/rmi/config/RmiInboundGatewayParserTests.java @@ -45,8 +45,8 @@ public class RmiInboundGatewayParserTests { MessagingTemplate template = (MessagingTemplate) accessor.getPropertyValue("messagingTemplate"); DirectFieldAccessor templateAccessor = new DirectFieldAccessor(template); - assertEquals(-1L, templateAccessor.getPropertyValue("sendTimeout")); - assertEquals(-1L, templateAccessor.getPropertyValue("receiveTimeout")); + assertEquals(1000L, templateAccessor.getPropertyValue("sendTimeout")); + assertEquals(1000L, templateAccessor.getPropertyValue("receiveTimeout")); } @Test diff --git a/spring-integration-ws/src/test/java/org/springframework/integration/ws/SimpleWebServiceInboundGatewayTests.java b/spring-integration-ws/src/test/java/org/springframework/integration/ws/SimpleWebServiceInboundGatewayTests.java index 56f43c8b1e..9f439915dc 100644 --- a/spring-integration-ws/src/test/java/org/springframework/integration/ws/SimpleWebServiceInboundGatewayTests.java +++ b/spring-integration-ws/src/test/java/org/springframework/integration/ws/SimpleWebServiceInboundGatewayTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -16,6 +16,22 @@ package org.springframework.integration.ws; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.anyLong; +import static org.mockito.Matchers.argThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.isA; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.StringReader; +import java.io.StringWriter; + +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; + import org.hamcrest.BaseMatcher; import org.hamcrest.Description; import org.junit.Before; @@ -25,6 +41,7 @@ import org.mockito.Mock; import org.mockito.invocation.InvocationOnMock; import org.mockito.runners.MockitoJUnitRunner; import org.mockito.stubbing.Answer; + import org.springframework.integration.Message; import org.springframework.integration.MessageChannel; import org.springframework.integration.MessageDeliveryException; @@ -32,20 +49,8 @@ import org.springframework.integration.channel.DirectChannel; import org.springframework.ws.WebServiceMessage; import org.springframework.ws.context.MessageContext; -import javax.xml.transform.Result; -import javax.xml.transform.Source; -import javax.xml.transform.stream.StreamResult; -import javax.xml.transform.stream.StreamSource; -import java.io.StringReader; -import java.io.StringWriter; - -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.*; - /** - * * @author Iwein Fuld - * */ @RunWith(MockitoJUnitRunner.class) public class SimpleWebServiceInboundGatewayTests { @@ -85,12 +90,12 @@ public class SimpleWebServiceInboundGatewayTests { @Test public void invokePoxSourceWithReply() throws Exception { - when(requestChannel.send(isA(Message.class))).thenAnswer( + when(requestChannel.send(isA(Message.class), eq(1000L))).thenAnswer( withReplyTo(replyChannel)); when(request.getPayloadSource()).thenReturn(payloadSource); gateway.start(); gateway.invoke(context); - verify(requestChannel).send(messageWithPayload(payloadSource)); + verify(requestChannel).send(messageWithPayload(payloadSource), eq(1000L)); assertTrue(output.toString().endsWith(input)); }