INT-1770 Support Client Mode For TCP Endpoints

Add client mode for inbound endpoints, and for the
outbound adapter.

INT-1770 Allow Inbound Adapter to Open Connection

Normally, inbound adapters use server sockets and wait for incoming
connection. There are use cases where the adapter should establish
the connection and wait for inbound messages.

INT-1770 Open Connection on Start

Allow configuration of outbound adapter to permit
connection establishment when the adapter is started
rather than when the first message arrives.

INT-1770 Allow Inbound Gateway to Open Connection

Normally, inbound gateways listen for connections. There are
use cases where an inbound gateway might open the connection
and then wait for incoming requests.

INT-1770 Parsers

Update parsers and tests to support attributes for setting
endpoints in client-mode.

INT-1770 Docs

Update reference with client-mode information.

INT-1770 Add Control Bus for Client Mode

Enable control bus commands to check status and to attempt
connection establishment.
This commit is contained in:
Gary Russell
2011-10-14 12:48:49 -04:00
parent a8e752e486
commit a7bda6bbb2
22 changed files with 1279 additions and 109 deletions

View File

@@ -278,4 +278,54 @@
channel="tcpChannel"
connection-factory="server1" />
<ip:tcp-connection-factory id="cfC3"
type="client"
port="#{tcpIpUtils.findAvailableServerSocket(6120)}"
host="localhost"
lookup-host="false"
apply-sequence="false"
/>
<ip:tcp-inbound-channel-adapter id="tcpInClientMode"
channel="tcpChannel"
connection-factory="cfC3"
client-mode="true"
retry-interval="123"
scheduler="sched" />
<ip:tcp-connection-factory id="cfC4"
type="client"
port="#{tcpIpUtils.findAvailableServerSocket(6140)}"
host="localhost"
lookup-host="false"
apply-sequence="false"
/>
<ip:tcp-outbound-channel-adapter id="tcpOutClientMode"
channel="tcpChannel"
connection-factory="cfC4"
client-mode="true"
retry-interval="124"
scheduler="sched" />
<ip:tcp-connection-factory id="cfC5"
type="client"
port="#{tcpIpUtils.findAvailableServerSocket(6160)}"
host="localhost"
lookup-host="false"
apply-sequence="false"
/>
<ip:tcp-inbound-gateway id="inGatewayClientMode"
request-channel="tcpChannel"
reply-channel="replyChannel"
connection-factory="cfC5"
reply-timeout="456"
client-mode="true"
retry-interval="125"
scheduler="sched"
/>
<task:scheduler id="sched"/>
</beans>

View File

@@ -56,6 +56,7 @@ import org.springframework.integration.ip.udp.MulticastSendingMessageHandler;
import org.springframework.integration.ip.udp.UnicastReceivingChannelAdapter;
import org.springframework.integration.ip.udp.UnicastSendingMessageHandler;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -128,6 +129,15 @@ public class ParserUnitTests {
@Autowired
AbstractConnectionFactory cfC2;
@Autowired
AbstractConnectionFactory cfC3;
@Autowired
AbstractConnectionFactory cfC4;
@Autowired
AbstractConnectionFactory cfC5;
@Autowired
Serializer<?> serializer;
@@ -172,6 +182,19 @@ public class ParserUnitTests {
@Autowired
private DirectChannel tcpChannel;
@Autowired
TcpReceivingChannelAdapter tcpInClientMode;
@Autowired
TcpInboundGateway inGatewayClientMode;
@Autowired
TaskScheduler sched;
@Autowired
@Qualifier(value="org.springframework.integration.ip.tcp.TcpSendingMessageHandler#3")
TcpSendingMessageHandler tcpOutClientMode;
@Test
public void testInUdp() {
DirectFieldAccessor dfa = new DirectFieldAccessor(udpIn);
@@ -297,7 +320,7 @@ public class ParserUnitTests {
@Test
public void testInGateway1() {
DirectFieldAccessor dfa = new DirectFieldAccessor(tcpInboundGateway1);
assertSame(cfS2, dfa.getPropertyValue("connectionFactory"));
assertSame(cfS2, dfa.getPropertyValue("serverConnectionFactory"));
assertEquals(456L, dfa.getPropertyValue("replyTimeout"));
assertEquals("inGateway1",tcpInboundGateway1.getComponentName());
assertEquals("ip:tcp-inbound-gateway", tcpInboundGateway1.getComponentType());
@@ -312,11 +335,14 @@ public class ParserUnitTests {
@Test
public void testInGateway2() {
DirectFieldAccessor dfa = new DirectFieldAccessor(tcpInboundGateway2);
assertSame(cfS3, dfa.getPropertyValue("connectionFactory"));
assertSame(cfS3, dfa.getPropertyValue("serverConnectionFactory"));
assertEquals(456L, dfa.getPropertyValue("replyTimeout"));
assertEquals("inGateway2",tcpInboundGateway2.getComponentName());
assertEquals("ip:tcp-inbound-gateway", tcpInboundGateway2.getComponentType());
assertNull(dfa.getPropertyValue("errorChannel"));
assertEquals(Boolean.FALSE, dfa.getPropertyValue("isClientMode"));
assertNull(dfa.getPropertyValue("scheduler"));
assertEquals(60000L, dfa.getPropertyValue("retryInterval"));
}
@Test
@@ -418,6 +444,9 @@ public class ParserUnitTests {
DirectFieldAccessor dfa = new DirectFieldAccessor(tcpNewOut1);
assertSame(client1, dfa.getPropertyValue("clientConnectionFactory"));
assertEquals(25, dfa.getPropertyValue("order"));
assertEquals(Boolean.FALSE, dfa.getPropertyValue("isClientMode"));
assertNull(dfa.getPropertyValue("scheduler"));
assertEquals(60000L, dfa.getPropertyValue("retryInterval"));
}
@Test
@@ -432,6 +461,9 @@ public class ParserUnitTests {
DirectFieldAccessor dfa = new DirectFieldAccessor(tcpNewIn1);
assertSame(client1, dfa.getPropertyValue("clientConnectionFactory"));
assertNull(dfa.getPropertyValue("errorChannel"));
assertEquals(Boolean.FALSE, dfa.getPropertyValue("isClientMode"));
assertNull(dfa.getPropertyValue("scheduler"));
assertEquals(60000L, dfa.getPropertyValue("retryInterval"));
}
@Test
@@ -456,4 +488,34 @@ public class ParserUnitTests {
assertSame(this.tcpOut, iterator.next()); //35
}
@Test
public void testInClientMode() {
DirectFieldAccessor dfa = new DirectFieldAccessor(tcpInClientMode);
assertSame(cfC3, dfa.getPropertyValue("clientConnectionFactory"));
assertNull(dfa.getPropertyValue("serverConnectionFactory"));
assertEquals(Boolean.TRUE, dfa.getPropertyValue("isClientMode"));
assertSame(sched, dfa.getPropertyValue("scheduler"));
assertEquals(123L, dfa.getPropertyValue("retryInterval"));
}
@Test
public void testOutClientMode() {
DirectFieldAccessor dfa = new DirectFieldAccessor(tcpOutClientMode);
assertSame(cfC4, dfa.getPropertyValue("clientConnectionFactory"));
assertNull(dfa.getPropertyValue("serverConnectionFactory"));
assertEquals(Boolean.TRUE, dfa.getPropertyValue("isClientMode"));
assertSame(sched, dfa.getPropertyValue("scheduler"));
assertEquals(124L, dfa.getPropertyValue("retryInterval"));
}
@Test
public void testInGatewayClientMode() {
DirectFieldAccessor dfa = new DirectFieldAccessor(inGatewayClientMode);
assertSame(cfC5, dfa.getPropertyValue("clientConnectionFactory"));
assertNull(dfa.getPropertyValue("serverConnectionFactory"));
assertEquals(Boolean.TRUE, dfa.getPropertyValue("isClientMode"));
assertSame(sched, dfa.getPropertyValue("scheduler"));
assertEquals(125L, dfa.getPropertyValue("retryInterval"));
}
}

View File

@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip-2.1.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">
<bean id="tcpIpUtils" class="org.springframework.integration.ip.util.SocketTestUtils" />
<int:channel id="in"/>
<int:channel id="dummy">
<int:queue />
</int:channel>
<int-ip:tcp-connection-factory id="server"
type="server"
using-nio="true"
port="#{tcpIpUtils.findAvailableServerSocket(12000)}"
lookup-host="false"
so-timeout="20000"
/>
<int-ip:tcp-connection-factory id="client1"
type="client"
host="localhost"
port="#{server.port}"
lookup-host="false"
so-timeout="100000"
/>
<int-ip:tcp-inbound-gateway id="servergw"
request-channel="dummy"
connection-factory="server"/>
<int-ip:tcp-inbound-channel-adapter
id="tcpIn"
connection-factory="client1"
channel="in"
client-mode="true"/>
<int:channel id="cbChannel" />
<int:control-bus input-channel="cbChannel" />
<int:gateway default-request-channel="cbChannel" service-interface="org.springframework.integration.ip.tcp.ClientModeControlBusTests$ControlBus"/>
</beans>

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2002-2011 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.ip.tcp;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Gary Russell
* @since 2.1
*
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ClientModeControlBusTests {
@Autowired
ControlBus controlBus;
@Autowired
TcpReceivingChannelAdapter tcpIn;
@Test
public void test() throws Exception {
assertTrue(controlBus.boolResult("@tcpIn.isClientMode()"));
int n = 0;
while (!controlBus.boolResult("@tcpIn.isClientModeConnected()")) {
Thread.sleep(100);
n += 100;
if (n > 10000) {
fail("Connection never established");
}
}
assertTrue(controlBus.boolResult("@tcpIn.isRunning()"));
controlBus.voidResult("@tcpIn.retryConnection()");
}
public static interface ControlBus {
boolean boolResult(String command);
void voidResult(String command);
}
}

View File

@@ -22,10 +22,16 @@ import static org.junit.Assert.fail;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.net.ServerSocketFactory;
import javax.net.SocketFactory;
import org.junit.Test;
@@ -37,7 +43,9 @@ import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.core.SubscribableChannel;
import org.springframework.integration.handler.ServiceActivatingHandler;
import org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory;
import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
import org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory;
import org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory;
import org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory;
import org.springframework.integration.ip.util.SocketTestUtils;
@@ -112,6 +120,57 @@ public class TcpInboundGatewayTests {
assertEquals("Echo:Test2\r\n", new String(bytes));
}
@Test
public void testNetClientMode() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
ccf.setSingleUse(false);
TcpInboundGateway gateway = new TcpInboundGateway();
gateway.setConnectionFactory(ccf);
final QueueChannel channel = new QueueChannel();
gateway.setRequestChannel(channel);
gateway.setClientMode(true);
gateway.setRetryInterval(10000);
gateway.afterPropertiesSet();
ServiceActivatingHandler handler = new ServiceActivatingHandler(new Service());
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final CountDownLatch latch3 = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
Executors.newSingleThreadExecutor().execute(new Runnable() {
public void run() {
try {
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port, 10);
latch1.countDown();
Socket socket = server.accept();
socket.getOutputStream().write("Test1\r\nTest2\r\n".getBytes());
byte[] bytes = new byte[12];
readFully(socket.getInputStream(), bytes);
assertEquals("Echo:Test1\r\n", new String(bytes));
readFully(socket.getInputStream(), bytes);
assertEquals("Echo:Test2\r\n", new String(bytes));
latch2.await();
socket.close();
server.close();
done.set(true);
latch3.countDown();
}
catch (Exception e) {
if (!done.get()) {
e.printStackTrace();
}
}
}
});
assertTrue(latch1.await(10, TimeUnit.SECONDS));
gateway.start();
handler.handleMessage(channel.receive());
handler.handleMessage(channel.receive());
latch2.countDown();
assertTrue(latch3.await(10, TimeUnit.SECONDS));
assertTrue(done.get());
}
@Test
public void testNioSingle() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();

View File

@@ -25,14 +25,19 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.net.ServerSocketFactory;
import javax.net.SocketFactory;
import org.junit.Test;
@@ -43,10 +48,12 @@ import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.SubscribableChannel;
import org.springframework.integration.handler.ServiceActivatingHandler;
import org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory;
import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
import org.springframework.integration.ip.tcp.connection.HelloWorldInterceptorFactory;
import org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactory;
import org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain;
import org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory;
import org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory;
import org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory;
import org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer;
@@ -58,7 +65,7 @@ import org.springframework.integration.ip.util.SocketTestUtils;
public class TcpReceivingChannelAdapterTests {
@Test
public void newTestNet() throws Exception {
public void testNet() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(port);
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
@@ -76,6 +83,7 @@ public class TcpReceivingChannelAdapterTests {
}
QueueChannel channel = new QueueChannel();
adapter.setOutputChannel(channel);
adapter.afterPropertiesSet();
Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
socket.getOutputStream().write("Test1\r\n".getBytes());
socket.getOutputStream().write("Test2\r\n".getBytes());
@@ -88,7 +96,57 @@ public class TcpReceivingChannelAdapterTests {
}
@Test
public void newTestNio() throws Exception {
public void testNetClientMode() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
Executors.newSingleThreadExecutor().execute(new Runnable() {
public void run() {
try {
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port, 10);
latch1.countDown();
Socket socket = server.accept();
socket.getOutputStream().write("Test1\r\nTest2\r\n".getBytes());
latch2.await();
socket.close();
server.close();
}
catch (Exception e) {
if (!done.get()) {
e.printStackTrace();
}
}
}
});
AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
ccf.setSerializer(serializer);
ccf.setDeserializer(serializer);
ccf.setSoTimeout(Integer.MAX_VALUE);
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
adapter.setConnectionFactory(ccf);
adapter.setClientMode(true);
QueueChannel channel = new QueueChannel();
adapter.setOutputChannel(channel);
adapter.afterPropertiesSet();
assertTrue(latch1.await(10, TimeUnit.SECONDS));
adapter.setRetryInterval(10000);
adapter.start();
Message<?> message = channel.receive(10000);
assertNotNull(message);
assertEquals("Test1", new String((byte[]) message.getPayload()));
message = channel.receive(10000);
assertNotNull(message);
assertEquals("Test2", new String((byte[]) message.getPayload()));
adapter.stop();
adapter.start();
adapter.stop();
latch2.countDown();
}
@Test
public void testNio() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(port);
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
@@ -123,7 +181,7 @@ public class TcpReceivingChannelAdapterTests {
}
@Test
public void newTestNetShared() throws Exception {
public void testNetShared() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(port);
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
@@ -161,7 +219,7 @@ public class TcpReceivingChannelAdapterTests {
}
@Test
public void newTestNioShared() throws Exception {
public void testNioShared() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(port);
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
@@ -199,7 +257,7 @@ public class TcpReceivingChannelAdapterTests {
}
@Test
public void newTestNetSingleNoOutbound() throws Exception {
public void testNetSingleNoOutbound() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(port);
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
@@ -235,7 +293,7 @@ public class TcpReceivingChannelAdapterTests {
}
@Test
public void newTestNioSingleNoOutbound() throws Exception {
public void testNioSingleNoOutbound() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(port);
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
@@ -281,7 +339,7 @@ public class TcpReceivingChannelAdapterTests {
}
@Test
public void newTestNetSingleShared() throws Exception {
public void testNetSingleShared() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(port);
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
@@ -322,7 +380,7 @@ public class TcpReceivingChannelAdapterTests {
}
@Test
public void newTestNioSingleShared() throws Exception {
public void testNioSingleShared() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(port);
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
@@ -363,7 +421,7 @@ public class TcpReceivingChannelAdapterTests {
}
@Test
public void newTestNioSingleSharedMany() throws Exception {
public void testNioSingleSharedMany() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(port);
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
@@ -407,42 +465,42 @@ public class TcpReceivingChannelAdapterTests {
}
@Test
public void newTestNetInterceptors() throws Exception {
public void testNetInterceptors() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(port);
interceptorsGuts(port, scf);
}
@Test
public void newTestNetSingleNoOutboundInterceptors() throws Exception {
public void testNetSingleNoOutboundInterceptors() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(port);
singleNoOutboundInterceptorsGuts(port, scf);
}
@Test
public void newTestNetSingleSharedInterceptors() throws Exception {
public void testNetSingleSharedInterceptors() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(port);
singleSharedInterceptorsGuts(port, scf);
}
@Test
public void newTestNioInterceptors() throws Exception {
public void testNioInterceptors() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
AbstractServerConnectionFactory scf = new TcpNioServerConnectionFactory(port);
interceptorsGuts(port, scf);
}
@Test
public void newTestNioSingleNoOutboundInterceptors() throws Exception {
public void testNioSingleNoOutboundInterceptors() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
AbstractServerConnectionFactory scf = new TcpNioServerConnectionFactory(port);
singleNoOutboundInterceptorsGuts(port, scf);
}
@Test
public void newTestNioSingleSharedInterceptors() throws Exception {
public void testNioSingleSharedInterceptors() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
AbstractServerConnectionFactory scf = new TcpNioServerConnectionFactory(port);
singleSharedInterceptorsGuts(port, scf);

View File

@@ -76,7 +76,7 @@ public class TcpSendingMessageHandlerTests {
}
@Test
public void newTestNetCrLf() throws Exception {
public void testNetCrLf() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
@@ -126,7 +126,64 @@ public class TcpSendingMessageHandlerTests {
}
@Test
public void newTestNioCrLf() throws Exception {
public void testNetCrLfClientMode() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
Executors.newSingleThreadExecutor().execute(new Runnable() {
public void run() {
try {
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
latch.countDown();
Socket socket = server.accept();
int i = 0;
while (true) {
byte[] b = new byte[6];
readFully(socket.getInputStream(), b);
b = ("Reply" + (++i) + "\r\n").getBytes();
socket.getOutputStream().write(b);
}
}
catch (Exception e) {
if (!done.get()) {
e.printStackTrace();
}
}
}
});
AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
ccf.setSerializer(serializer);
ccf.setDeserializer(serializer);
ccf.setSoTimeout(Integer.MAX_VALUE);
TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
handler.setConnectionFactory(ccf);
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
adapter.setConnectionFactory(ccf);
QueueChannel channel = new QueueChannel();
adapter.setOutputChannel(channel);
assertTrue(latch.await(10, TimeUnit.SECONDS));
handler.setClientMode(true);
handler.setRetryInterval(10000);
handler.afterPropertiesSet();
handler.start();
adapter.start();
handler.handleMessage(MessageBuilder.withPayload("Test").build());
handler.handleMessage(MessageBuilder.withPayload("Test").build());
Message<?> mOut = channel.receive(10000);
assertNotNull(mOut);
assertEquals("Reply1", new String((byte[]) mOut.getPayload()));
mOut = channel.receive(10000);
assertNotNull(mOut);
assertEquals("Reply2", new String((byte[]) mOut.getPayload()));
done.set(true);
handler.stop();
handler.start();
handler.stop();
}
@Test
public void testNioCrLf() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
@@ -178,7 +235,7 @@ public class TcpSendingMessageHandlerTests {
}
@Test
public void newTestNetStxEtx() throws Exception {
public void testNetStxEtx() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
@@ -227,7 +284,7 @@ public class TcpSendingMessageHandlerTests {
}
@Test
public void newTestNioStxEtx() throws Exception {
public void testNioStxEtx() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
@@ -279,7 +336,7 @@ public class TcpSendingMessageHandlerTests {
}
@Test
public void newTestNetLength() throws Exception {
public void testNetLength() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
@@ -331,7 +388,7 @@ public class TcpSendingMessageHandlerTests {
}
@Test
public void newTestNioLength() throws Exception {
public void testNioLength() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
@@ -386,7 +443,7 @@ public class TcpSendingMessageHandlerTests {
}
@Test
public void newTestNetSerial() throws Exception {
public void testNetSerial() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
@@ -434,7 +491,7 @@ public class TcpSendingMessageHandlerTests {
}
@Test
public void newTestNioSerial() throws Exception {
public void testNioSerial() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
@@ -485,7 +542,7 @@ public class TcpSendingMessageHandlerTests {
}
@Test
public void newTestNetSingleUseNoInbound() throws Exception {
public void testNetSingleUseNoInbound() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch = new CountDownLatch(1);
final Semaphore semaphore = new Semaphore(0);
@@ -526,7 +583,7 @@ public class TcpSendingMessageHandlerTests {
}
@Test
public void newTestNioSingleUseNoInbound() throws Exception {
public void testNioSingleUseNoInbound() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch = new CountDownLatch(1);
final Semaphore semaphore = new Semaphore(0);
@@ -567,7 +624,7 @@ public class TcpSendingMessageHandlerTests {
}
@Test
public void newTestNetSingleUseWithInbound() throws Exception {
public void testNetSingleUseWithInbound() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch = new CountDownLatch(1);
final Semaphore semaphore = new Semaphore(0);
@@ -622,7 +679,7 @@ public class TcpSendingMessageHandlerTests {
}
@Test
public void newTestNioSingleUseWithInbound() throws Exception {
public void testNioSingleUseWithInbound() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch = new CountDownLatch(1);
final Semaphore semaphore = new Semaphore(0);
@@ -677,7 +734,7 @@ public class TcpSendingMessageHandlerTests {
}
@Test
public void newTestNioSingleUseWithInboundMany() throws Exception {
public void testNioSingleUseWithInboundMany() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch = new CountDownLatch(1);
final Semaphore semaphore = new Semaphore(0);
@@ -743,7 +800,7 @@ public class TcpSendingMessageHandlerTests {
}
@Test
public void newTestNetNegotiate() throws Exception {
public void testNetNegotiate() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
@@ -810,7 +867,7 @@ public class TcpSendingMessageHandlerTests {
}
@Test
public void newTestNioNegotiate() throws Exception {
public void testNioNegotiate() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
@@ -875,7 +932,7 @@ public class TcpSendingMessageHandlerTests {
}
@Test
public void newTestNetNegotiateSingleNoListen() throws Exception {
public void testNetNegotiateSingleNoListen() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
@@ -932,7 +989,7 @@ public class TcpSendingMessageHandlerTests {
}
@Test
public void newTestNioNegotiateSingleNoListen() throws Exception {
public void testNioNegotiateSingleNoListen() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();