TCP Async - Add Timeout Test
http://stackoverflow.com/questions/32614395/spring-integration-tcp-outbound-channel-adapter-that-detects-connection-loss-and/32617568#comment54590607_32617568 Polishing - PR Comments
This commit is contained in:
committed by
Artem Bilan
parent
6cc1ff6542
commit
15f78146e0
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.integration.samples.tcpclientserver;
|
||||
|
||||
import org.springframework.integration.MessageTimeoutException;
|
||||
|
||||
/**
|
||||
* Simple service that receives data in a byte array,
|
||||
* converts it to a String and appends it with ':echo'.
|
||||
@@ -25,11 +27,19 @@ package org.springframework.integration.samples.tcpclientserver;
|
||||
*/
|
||||
public class EchoService {
|
||||
|
||||
public String test(String input) {
|
||||
public String test(String input) throws InterruptedException {
|
||||
if ("FAIL".equals(input)) {
|
||||
throw new RuntimeException("Failure Demonstration");
|
||||
}
|
||||
else if("TIMEOUT_TEST".equals(input)){
|
||||
Thread.sleep(3000);
|
||||
}
|
||||
|
||||
return input + ":echo";
|
||||
}
|
||||
|
||||
}
|
||||
public MessageTimeoutException noResponse(String input) {
|
||||
return new MessageTimeoutException("No response received for " + input);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
xmlns="http://www.springframework.org/schema/integration"
|
||||
xmlns:ip="http://www.springframework.org/schema/integration/ip"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
|
||||
|
||||
<beans:description>
|
||||
Uses conversion service and collaborating channel adapters.
|
||||
@@ -29,8 +29,9 @@
|
||||
<!-- Client side -->
|
||||
|
||||
<gateway id="gw"
|
||||
service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway"
|
||||
default-request-channel="input" />
|
||||
service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway"
|
||||
default-reply-timeout="20000"
|
||||
default-request-channel="input" />
|
||||
|
||||
<ip:tcp-connection-factory id="client"
|
||||
type="client"
|
||||
@@ -65,9 +66,17 @@
|
||||
|
||||
<aggregator input-channel="toAggregator.client"
|
||||
output-channel="toTransformer.client"
|
||||
expire-groups-upon-completion="true"
|
||||
expire-groups-upon-timeout="true"
|
||||
discard-channel="noResponseChannel"
|
||||
group-timeout="1000"
|
||||
correlation-strategy-expression="payload.substring(0,3)"
|
||||
release-strategy-expression="size() == 2" />
|
||||
|
||||
<channel id="noResponseChannel" />
|
||||
|
||||
<service-activator input-channel="noResponseChannel" ref="echoService" method="noResponse" />
|
||||
|
||||
<transformer input-channel="toTransformer.client"
|
||||
expression="payload.get(1)"/> <!-- The response is always second -->
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -15,8 +15,11 @@
|
||||
*/
|
||||
package org.springframework.integration.samples.tcpclientserver;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
@@ -26,9 +29,11 @@ import java.util.concurrent.TimeUnit;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
||||
import org.springframework.core.task.TaskExecutor;
|
||||
import org.springframework.integration.MessageTimeoutException;
|
||||
import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
|
||||
import org.springframework.integration.ip.util.TestingUtilities;
|
||||
import org.springframework.integration.samples.tcpclientserver.support.CustomTestContextLoader;
|
||||
@@ -79,6 +84,7 @@ public class TcpClientServerDemoTest {
|
||||
results.add(i);
|
||||
final int j = i;
|
||||
executor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String result = gw.send(j + "Hello world!"); // first 3 bytes is correlationid
|
||||
assertEquals(j + "Hello world!:echo", result);
|
||||
@@ -89,4 +95,16 @@ public class TcpClientServerDemoTest {
|
||||
assertTrue(latch.await(10, TimeUnit.SECONDS));
|
||||
assertEquals(0, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTimeout() {
|
||||
try {
|
||||
gw.send("TIMEOUT_TEST");
|
||||
fail("expected exception");
|
||||
}
|
||||
catch (MessageTimeoutException e) {
|
||||
assertThat(e.getMessage(), containsString("No response received for TIMEOUT_TEST"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user