INT-1269 Refactor TCP Adapters to Separate Out Connection Factories - Docs to Follow
This commit is contained in:
@@ -277,5 +277,64 @@
|
||||
so-timeout="226"
|
||||
close="true"
|
||||
/>
|
||||
|
||||
<ip:tcp-connection-factory
|
||||
id="client1"
|
||||
type="client"
|
||||
host="localhost"
|
||||
port="9876"
|
||||
input-converter="serial"
|
||||
output-converter="serial"
|
||||
so-keep-alive="true"
|
||||
so-linger="54"
|
||||
so-receive-buffer-size="1234"
|
||||
so-send-buffer-size="1235"
|
||||
so-tcp-no-delay="true"
|
||||
so-timeout="1236"
|
||||
so-traffic-class="12"
|
||||
using-nio="true"
|
||||
single-use="true"
|
||||
task-executor="externalTE"
|
||||
pool-size="321"
|
||||
/>
|
||||
|
||||
<ip:tcp-connection-factory
|
||||
id="server1"
|
||||
type="server"
|
||||
port="9876"
|
||||
local-address="127.0.0.1"
|
||||
input-converter="serial"
|
||||
output-converter="serial"
|
||||
so-keep-alive="true"
|
||||
so-linger="55"
|
||||
so-receive-buffer-size="1234"
|
||||
so-send-buffer-size="1235"
|
||||
so-tcp-no-delay="true"
|
||||
so-timeout="1236"
|
||||
so-traffic-class="12"
|
||||
using-nio="true"
|
||||
single-use="true"
|
||||
task-executor="externalTE"
|
||||
pool-size="123"
|
||||
/>
|
||||
|
||||
<bean id="serial" class="org.springframework.commons.serializer.JavaSerializationConverter" />
|
||||
|
||||
<ip:tcp-outbound-channel-adapter id="tcpNewOut1"
|
||||
channel="tcpChannel"
|
||||
connection-factory="client1" />
|
||||
|
||||
<ip:tcp-outbound-channel-adapter id="tcpNewOut2"
|
||||
channel="tcpChannel"
|
||||
connection-factory="server1" />
|
||||
|
||||
<ip:tcp-inbound-channel-adapter id="tcpNewIn1"
|
||||
channel="tcpChannel"
|
||||
connection-factory="client1" />
|
||||
|
||||
<ip:tcp-inbound-channel-adapter id="tcpNewIn2"
|
||||
channel="tcpChannel"
|
||||
connection-factory="server1" />
|
||||
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.commons.serializer.InputStreamingConverter;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.task.TaskExecutor;
|
||||
import org.springframework.integration.ip.tcp.CustomNetSocketReader;
|
||||
@@ -38,6 +39,11 @@ import org.springframework.integration.ip.tcp.TcpNetReceivingChannelAdapter;
|
||||
import org.springframework.integration.ip.tcp.TcpNetSendingMessageHandler;
|
||||
import org.springframework.integration.ip.tcp.TcpNioReceivingChannelAdapter;
|
||||
import org.springframework.integration.ip.tcp.TcpNioSendingMessageHandler;
|
||||
import org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter;
|
||||
import org.springframework.integration.ip.tcp.TcpSendingMessageHandler;
|
||||
import org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory;
|
||||
import org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory;
|
||||
import org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory;
|
||||
import org.springframework.integration.ip.udp.DatagramPacketMessageMapper;
|
||||
import org.springframework.integration.ip.udp.MulticastReceivingChannelAdapter;
|
||||
import org.springframework.integration.ip.udp.MulticastSendingMessageHandler;
|
||||
@@ -49,6 +55,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@@ -125,6 +132,33 @@ public class ParserUnitTests {
|
||||
@Qualifier(value="externalTE")
|
||||
TaskExecutor taskExecutor;
|
||||
|
||||
@Autowired
|
||||
@Qualifier(value="client1")
|
||||
AbstractConnectionFactory client1;
|
||||
|
||||
@Autowired
|
||||
InputStreamingConverter<byte[]> converter;
|
||||
|
||||
@Autowired
|
||||
@Qualifier(value="server1")
|
||||
AbstractConnectionFactory server1;
|
||||
|
||||
@Autowired
|
||||
@Qualifier(value="org.springframework.integration.ip.tcp.TcpSendingMessageHandler#0")
|
||||
TcpSendingMessageHandler tcpNewOut1;
|
||||
|
||||
@Autowired
|
||||
@Qualifier(value="org.springframework.integration.ip.tcp.TcpSendingMessageHandler#1")
|
||||
TcpSendingMessageHandler tcpNewOut2;
|
||||
|
||||
@Autowired
|
||||
@Qualifier(value="tcpNewIn1")
|
||||
TcpReceivingChannelAdapter tcpNewIn1;
|
||||
|
||||
@Autowired
|
||||
@Qualifier(value="tcpNewIn2")
|
||||
TcpReceivingChannelAdapter tcpNewIn2;
|
||||
|
||||
@Test
|
||||
public void testInUdp() {
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(udpIn);
|
||||
@@ -383,4 +417,66 @@ public class ParserUnitTests {
|
||||
assertEquals(226, delegateDfa.getPropertyValue("soTimeout"));
|
||||
assertEquals(true, dfa.getPropertyValue("close"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnClient1() {
|
||||
assertTrue(client1 instanceof TcpNioClientConnectionFactory);
|
||||
assertEquals("localhost", client1.getHost());
|
||||
assertEquals(9876, client1.getPort());
|
||||
assertEquals(54, client1.getSoLinger());
|
||||
assertEquals(1234, client1.getSoReceiveBufferSize());
|
||||
assertEquals(1235, client1.getSoSendBufferSize());
|
||||
assertEquals(1236, client1.getSoTimeout());
|
||||
assertEquals(12, client1.getSoTrafficClass());
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(client1);
|
||||
assertSame(converter, dfa.getPropertyValue("inputConverter"));
|
||||
assertSame(converter, dfa.getPropertyValue("outputConverter"));
|
||||
assertEquals(true, dfa.getPropertyValue("soTcpNoDelay"));
|
||||
assertEquals(true, dfa.getPropertyValue("singleUse"));
|
||||
assertSame(taskExecutor, dfa.getPropertyValue("taskExecutor"));
|
||||
assertEquals(321, dfa.getPropertyValue("poolSize"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnServer1() {
|
||||
assertTrue(server1 instanceof TcpNioServerConnectionFactory);
|
||||
assertEquals(9876, server1.getPort());
|
||||
assertEquals(55, server1.getSoLinger());
|
||||
assertEquals(1234, server1.getSoReceiveBufferSize());
|
||||
assertEquals(1235, server1.getSoSendBufferSize());
|
||||
assertEquals(1236, server1.getSoTimeout());
|
||||
assertEquals(12, server1.getSoTrafficClass());
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(server1);
|
||||
assertSame(converter, dfa.getPropertyValue("inputConverter"));
|
||||
assertSame(converter, dfa.getPropertyValue("outputConverter"));
|
||||
assertEquals(true, dfa.getPropertyValue("soTcpNoDelay"));
|
||||
assertEquals(true, dfa.getPropertyValue("singleUse"));
|
||||
assertSame(taskExecutor, dfa.getPropertyValue("taskExecutor"));
|
||||
assertEquals(123, dfa.getPropertyValue("poolSize"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNewOut1() {
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(tcpNewOut1);
|
||||
assertSame(client1, dfa.getPropertyValue("clientConnectionFactory"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNewOut2() {
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(tcpNewOut2);
|
||||
assertSame(server1, dfa.getPropertyValue("serverConnectionFactory"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNewIn1() {
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(tcpNewIn1);
|
||||
assertSame(client1, dfa.getPropertyValue("clientConnectionFactory"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNewIn2() {
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(tcpNewIn2);
|
||||
assertSame(server1, dfa.getPropertyValue("serverConnectionFactory"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
<?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="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
|
||||
|
||||
|
||||
<bean id="tcpIpUtils" class="org.springframework.integration.ip.util.SocketUtils" />
|
||||
|
||||
<int-ip:tcp-connection-factory id="server"
|
||||
type="server"
|
||||
port="#{tcpIpUtils.findAvailableServerSocket(10000)}"
|
||||
/>
|
||||
|
||||
<int-ip:tcp-connection-factory id="client"
|
||||
type="client"
|
||||
host="localhost"
|
||||
port="#{server.port}"
|
||||
single-use="true"
|
||||
so-timeout="100000"
|
||||
/>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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.
|
||||
* 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.assertNotNull;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter;
|
||||
import org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory;
|
||||
import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
|
||||
import org.springframework.integration.ip.tcp.connection.TcpConnection;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
|
||||
/**
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class ConnectionToConnectionTests {
|
||||
|
||||
@Autowired
|
||||
AbstractApplicationContext ctx;
|
||||
|
||||
@Autowired
|
||||
private AbstractClientConnectionFactory client;
|
||||
|
||||
@Autowired
|
||||
private AbstractServerConnectionFactory server;
|
||||
|
||||
private TcpReceivingChannelAdapter receiver;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
receiver = new TcpReceivingChannelAdapter();
|
||||
server.registerListener(receiver);
|
||||
ctx.start();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnect() throws Exception {
|
||||
int n = 0;
|
||||
while (!server.isListening()) {
|
||||
Thread.sleep(100);
|
||||
if (n++ > 100) {
|
||||
throw new Exception("Failed to listen");
|
||||
}
|
||||
}
|
||||
TcpConnection connection = client.getConnection();
|
||||
QueueChannel channel = new QueueChannel();
|
||||
receiver.setOutputChannel(channel);
|
||||
connection.send(MessageBuilder.withPayload("Test").build());
|
||||
Message<?> m = channel.receive(10000);
|
||||
assertNotNull(m);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?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="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
|
||||
|
||||
|
||||
<bean id="tcpIpUtils" class="org.springframework.integration.ip.util.SocketUtils" />
|
||||
|
||||
<bean id="serializer" class="org.springframework.commons.serializer.JavaSerializationConverter" />
|
||||
|
||||
<int-ip:tcp-connection-factory id="server"
|
||||
type="server"
|
||||
port="#{tcpIpUtils.findAvailableServerSocket(10000)}"
|
||||
input-converter="serializer"
|
||||
output-converter="serializer"
|
||||
using-nio="true"
|
||||
single-use="true"
|
||||
/>
|
||||
|
||||
<int-ip:tcp-connection-factory id="client"
|
||||
type="client"
|
||||
host="localhost"
|
||||
port="#{server.port}"
|
||||
single-use="true"
|
||||
so-timeout="10000"
|
||||
input-converter="serializer"
|
||||
output-converter="serializer"
|
||||
/>
|
||||
|
||||
<int:channel id="input" />
|
||||
|
||||
<int:channel id="replies">
|
||||
<int:queue/>
|
||||
</int:channel>
|
||||
|
||||
<int-ip:tcp-outbound-channel-adapter id="outboundClient"
|
||||
channel="input"
|
||||
connection-factory="client"/>
|
||||
|
||||
<int-ip:tcp-inbound-channel-adapter id="inboundClient"
|
||||
channel="replies"
|
||||
connection-factory="client"/>
|
||||
|
||||
<int-ip:tcp-inbound-channel-adapter id="inboundServer"
|
||||
channel="loop"
|
||||
connection-factory="server"/>
|
||||
|
||||
<int-ip:tcp-outbound-channel-adapter id="outboundServer"
|
||||
channel="loop"
|
||||
connection-factory="server"/>
|
||||
|
||||
<int:channel id="loop" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* 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.
|
||||
* 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.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.core.MessageChannel;
|
||||
import org.springframework.integration.ip.tcp.connection.TcpListener;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
|
||||
/**
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class SharedConnectionTests {
|
||||
|
||||
@Autowired
|
||||
AbstractApplicationContext ctx;
|
||||
|
||||
@Autowired
|
||||
@Qualifier(value="inboundServer")
|
||||
TcpListener listener;
|
||||
|
||||
/**
|
||||
* Tests a loopback. The client-side outbound adapter sends a message over
|
||||
* a connection from the client connection factory; the server side
|
||||
* receives the message, puts in on a channel which is the input channel
|
||||
* for the outbound adapter that's sharing the connections. The response
|
||||
* comes back to an inbound adapter that is sharing the client's
|
||||
* connection and we verify we get the echo back as expected.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void test1() throws Exception {
|
||||
int n = 0;
|
||||
while (!listener.isListening()) {
|
||||
Thread.sleep(100);
|
||||
if (n++ > 100) {
|
||||
fail("Failed to listen");
|
||||
}
|
||||
}
|
||||
MessageChannel input = ctx.getBean("input", MessageChannel.class);
|
||||
input.send(MessageBuilder.withPayload("Test").build());
|
||||
QueueChannel replies = ctx.getBean("replies", QueueChannel.class);
|
||||
Message<?> message = replies.receive(10000);
|
||||
assertNotNull(message);
|
||||
assertEquals("Test", message.getPayload());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,13 +17,31 @@ package org.springframework.integration.ip.tcp;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
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 javax.net.SocketFactory;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.ip.AbstractInternetProtocolReceivingChannelAdapter;
|
||||
import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
|
||||
import org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory;
|
||||
import org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory;
|
||||
import org.springframework.integration.ip.tcp.converter.ByteArrayCrLfConverter;
|
||||
import org.springframework.integration.ip.util.SocketUtils;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
|
||||
@@ -212,5 +230,354 @@ public class TcpReceivingChannelAdapterTests {
|
||||
new String((byte[])message.getPayload()));
|
||||
adapter.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNet() throws Exception {
|
||||
final int port = SocketUtils.findAvailableServerSocket();
|
||||
AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(port);
|
||||
ByteArrayCrLfConverter converter = new ByteArrayCrLfConverter();
|
||||
scf.setInputConverter(converter);
|
||||
scf.setOutputConverter(converter);
|
||||
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
|
||||
adapter.setConnectionFactory(scf);
|
||||
scf.start();
|
||||
int n = 0;
|
||||
while (!scf.isListening()) {
|
||||
Thread.sleep(100);
|
||||
if (n++ > 100) {
|
||||
fail("Failed to start listening");
|
||||
}
|
||||
}
|
||||
QueueChannel channel = new QueueChannel();
|
||||
adapter.setOutputChannel(channel);
|
||||
Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
|
||||
socket.getOutputStream().write("Test1\r\n".getBytes());
|
||||
socket.getOutputStream().write("Test2\r\n".getBytes());
|
||||
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()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNio() throws Exception {
|
||||
final int port = SocketUtils.findAvailableServerSocket();
|
||||
TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(port);
|
||||
ByteArrayCrLfConverter converter = new ByteArrayCrLfConverter();
|
||||
scf.setInputConverter(converter);
|
||||
scf.setOutputConverter(converter);
|
||||
scf.setSoTimeout(5000);
|
||||
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
|
||||
adapter.setConnectionFactory(scf);
|
||||
scf.start();
|
||||
int n = 0;
|
||||
while (!scf.isListening()) {
|
||||
Thread.sleep(100);
|
||||
if (n++ > 100) {
|
||||
fail("Failed to start listening");
|
||||
}
|
||||
}
|
||||
QueueChannel channel = new QueueChannel();
|
||||
adapter.setOutputChannel(channel);
|
||||
Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
|
||||
for (int i = 0; i < 100; i++) {
|
||||
socket.getOutputStream().write(("Test" + i + "\r\n").getBytes());
|
||||
// if (i % 10 == 0) {
|
||||
// Thread.sleep(1000);
|
||||
// }
|
||||
}
|
||||
for (int i = 0; i < 100; i++) {
|
||||
Message<?> message = channel.receive(10000);
|
||||
assertNotNull(message);
|
||||
assertEquals("Test" + i, new String((byte[]) message.getPayload()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNetShared() throws Exception {
|
||||
final int port = SocketUtils.findAvailableServerSocket();
|
||||
AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(port);
|
||||
ByteArrayCrLfConverter converter = new ByteArrayCrLfConverter();
|
||||
scf.setInputConverter(converter);
|
||||
scf.setOutputConverter(converter);
|
||||
TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
|
||||
handler.setConnectionFactory(scf);
|
||||
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
|
||||
adapter.setConnectionFactory(scf);
|
||||
scf.start();
|
||||
QueueChannel channel = new QueueChannel();
|
||||
adapter.setOutputChannel(channel);
|
||||
int n = 0;
|
||||
while (!scf.isListening()) {
|
||||
Thread.sleep(100);
|
||||
if (n++ > 100) {
|
||||
fail("Failed to listen");
|
||||
}
|
||||
}
|
||||
Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
|
||||
socket.setSoTimeout(2000);
|
||||
socket.getOutputStream().write("Test\r\n".getBytes());
|
||||
socket.getOutputStream().write("Test\r\n".getBytes());
|
||||
Message<?> message = channel.receive(10000);
|
||||
assertNotNull(message);
|
||||
handler.handleMessage(message);
|
||||
message = channel.receive(10000);
|
||||
assertNotNull(message);
|
||||
handler.handleMessage(message);
|
||||
byte[] b = new byte[6];
|
||||
readFully(socket.getInputStream(), b);
|
||||
assertEquals("Test\r\n", new String(b));
|
||||
readFully(socket.getInputStream(), b);
|
||||
assertEquals("Test\r\n", new String(b));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNioShared() throws Exception {
|
||||
final int port = SocketUtils.findAvailableServerSocket();
|
||||
TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(port);
|
||||
ByteArrayCrLfConverter converter = new ByteArrayCrLfConverter();
|
||||
scf.setInputConverter(converter);
|
||||
scf.setOutputConverter(converter);
|
||||
TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
|
||||
handler.setConnectionFactory(scf);
|
||||
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
|
||||
adapter.setConnectionFactory(scf);
|
||||
scf.start();
|
||||
QueueChannel channel = new QueueChannel();
|
||||
adapter.setOutputChannel(channel);
|
||||
int n = 0;
|
||||
while (!scf.isListening()) {
|
||||
Thread.sleep(100);
|
||||
if (n++ > 100) {
|
||||
fail("Failed to listen");
|
||||
}
|
||||
}
|
||||
Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
|
||||
socket.setSoTimeout(2000);
|
||||
socket.getOutputStream().write("Test\r\n".getBytes());
|
||||
socket.getOutputStream().write("Test\r\n".getBytes());
|
||||
Message<?> message = channel.receive(10000);
|
||||
assertNotNull(message);
|
||||
handler.handleMessage(message);
|
||||
message = channel.receive(10000);
|
||||
assertNotNull(message);
|
||||
handler.handleMessage(message);
|
||||
byte[] b = new byte[6];
|
||||
readFully(socket.getInputStream(), b);
|
||||
assertEquals("Test\r\n", new String(b));
|
||||
readFully(socket.getInputStream(), b);
|
||||
assertEquals("Test\r\n", new String(b));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNetSingleNoOutbound() throws Exception {
|
||||
final int port = SocketUtils.findAvailableServerSocket();
|
||||
AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(port);
|
||||
ByteArrayCrLfConverter converter = new ByteArrayCrLfConverter();
|
||||
scf.setInputConverter(converter);
|
||||
scf.setOutputConverter(converter);
|
||||
scf.setSingleUse(true);
|
||||
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
|
||||
adapter.setConnectionFactory(scf);
|
||||
scf.start();
|
||||
int n = 0;
|
||||
while (!scf.isListening()) {
|
||||
Thread.sleep(100);
|
||||
if (n++ > 100) {
|
||||
fail("Failed to start listening");
|
||||
}
|
||||
}
|
||||
QueueChannel channel = new QueueChannel();
|
||||
adapter.setOutputChannel(channel);
|
||||
Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
|
||||
socket.getOutputStream().write("Test1\r\n".getBytes());
|
||||
socket = SocketFactory.getDefault().createSocket("localhost", port);
|
||||
socket.getOutputStream().write("Test2\r\n".getBytes());
|
||||
Message<?> message = channel.receive(10000);
|
||||
assertNotNull(message);
|
||||
// with single use, results may come back in a different order
|
||||
Set<String> results = new HashSet<String>();
|
||||
results.add(new String((byte[]) message.getPayload()));
|
||||
message = channel.receive(10000);
|
||||
assertNotNull(message);
|
||||
results.add(new String((byte[]) message.getPayload()));
|
||||
assertTrue(results.contains("Test1"));
|
||||
assertTrue(results.contains("Test2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNioSingleNoOutbound() throws Exception {
|
||||
final int port = SocketUtils.findAvailableServerSocket();
|
||||
TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(port);
|
||||
ByteArrayCrLfConverter converter = new ByteArrayCrLfConverter();
|
||||
scf.setInputConverter(converter);
|
||||
scf.setOutputConverter(converter);
|
||||
scf.setSingleUse(true);
|
||||
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
|
||||
adapter.setConnectionFactory(scf);
|
||||
scf.start();
|
||||
int n = 0;
|
||||
while (!scf.isListening()) {
|
||||
Thread.sleep(100);
|
||||
if (n++ > 100) {
|
||||
fail("Failed to start listening");
|
||||
}
|
||||
}
|
||||
QueueChannel channel = new QueueChannel();
|
||||
adapter.setOutputChannel(channel);
|
||||
Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
|
||||
socket.getOutputStream().write("Test1\r\n".getBytes());
|
||||
socket = SocketFactory.getDefault().createSocket("localhost", port);
|
||||
socket.getOutputStream().write("Test2\r\n".getBytes());
|
||||
Message<?> message = channel.receive(10000);
|
||||
assertNotNull(message);
|
||||
// with single use, results may come back in a different order
|
||||
Set<String> results = new HashSet<String>();
|
||||
results.add(new String((byte[]) message.getPayload()));
|
||||
message = channel.receive(10000);
|
||||
assertNotNull(message);
|
||||
results.add(new String((byte[]) message.getPayload()));
|
||||
assertTrue(results.contains("Test1"));
|
||||
assertTrue(results.contains("Test2"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param is
|
||||
* @param buff
|
||||
*/
|
||||
private void readFully(InputStream is, byte[] buff) throws IOException {
|
||||
for (int i = 0; i < buff.length; i++) {
|
||||
buff[i] = (byte) is.read();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNetSingleShared() throws Exception {
|
||||
final int port = SocketUtils.findAvailableServerSocket();
|
||||
AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(port);
|
||||
ByteArrayCrLfConverter converter = new ByteArrayCrLfConverter();
|
||||
scf.setInputConverter(converter);
|
||||
scf.setOutputConverter(converter);
|
||||
scf.setSingleUse(true);
|
||||
TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
|
||||
handler.setConnectionFactory(scf);
|
||||
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
|
||||
adapter.setConnectionFactory(scf);
|
||||
scf.start();
|
||||
QueueChannel channel = new QueueChannel();
|
||||
adapter.setOutputChannel(channel);
|
||||
int n = 0;
|
||||
while (!scf.isListening()) {
|
||||
Thread.sleep(100);
|
||||
if (n++ > 100) {
|
||||
fail("Failed to listen");
|
||||
}
|
||||
}
|
||||
Socket socket1 = SocketFactory.getDefault().createSocket("localhost", port);
|
||||
socket1.setSoTimeout(2000);
|
||||
socket1.getOutputStream().write("Test1\r\n".getBytes());
|
||||
Socket socket2 = SocketFactory.getDefault().createSocket("localhost", port);
|
||||
socket2.setSoTimeout(2000);
|
||||
socket2.getOutputStream().write("Test2\r\n".getBytes());
|
||||
Message<?> message = channel.receive(10000);
|
||||
assertNotNull(message);
|
||||
handler.handleMessage(message);
|
||||
message = channel.receive(10000);
|
||||
assertNotNull(message);
|
||||
handler.handleMessage(message);
|
||||
byte[] b = new byte[7];
|
||||
readFully(socket1.getInputStream(), b);
|
||||
assertEquals("Test1\r\n", new String(b));
|
||||
readFully(socket2.getInputStream(), b);
|
||||
assertEquals("Test2\r\n", new String(b));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNioSingleShared() throws Exception {
|
||||
final int port = SocketUtils.findAvailableServerSocket();
|
||||
TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(port);
|
||||
ByteArrayCrLfConverter converter = new ByteArrayCrLfConverter();
|
||||
scf.setInputConverter(converter);
|
||||
scf.setOutputConverter(converter);
|
||||
scf.setSingleUse(true);
|
||||
TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
|
||||
handler.setConnectionFactory(scf);
|
||||
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
|
||||
adapter.setConnectionFactory(scf);
|
||||
scf.start();
|
||||
QueueChannel channel = new QueueChannel();
|
||||
adapter.setOutputChannel(channel);
|
||||
int n = 0;
|
||||
while (!scf.isListening()) {
|
||||
Thread.sleep(100);
|
||||
if (n++ > 100) {
|
||||
fail("Failed to listen");
|
||||
}
|
||||
}
|
||||
Socket socket1 = SocketFactory.getDefault().createSocket("localhost", port);
|
||||
socket1.setSoTimeout(2000);
|
||||
socket1.getOutputStream().write("Test1\r\n".getBytes());
|
||||
Socket socket2 = SocketFactory.getDefault().createSocket("localhost", port);
|
||||
socket2.setSoTimeout(2000);
|
||||
socket2.getOutputStream().write("Test2\r\n".getBytes());
|
||||
Message<?> message = channel.receive(10000);
|
||||
assertNotNull(message);
|
||||
handler.handleMessage(message);
|
||||
message = channel.receive(10000);
|
||||
assertNotNull(message);
|
||||
handler.handleMessage(message);
|
||||
byte[] b = new byte[7];
|
||||
readFully(socket1.getInputStream(), b);
|
||||
assertEquals("Test1\r\n", new String(b));
|
||||
readFully(socket2.getInputStream(), b);
|
||||
assertEquals("Test2\r\n", new String(b));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNioSingleSharedMany() throws Exception {
|
||||
final int port = SocketUtils.findAvailableServerSocket();
|
||||
TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(port);
|
||||
ByteArrayCrLfConverter converter = new ByteArrayCrLfConverter();
|
||||
scf.setInputConverter(converter);
|
||||
scf.setOutputConverter(converter);
|
||||
scf.setSingleUse(true);
|
||||
TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
|
||||
handler.setConnectionFactory(scf);
|
||||
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
|
||||
adapter.setConnectionFactory(scf);
|
||||
Executor te = Executors.newFixedThreadPool(100);
|
||||
scf.setTaskExecutor(te);
|
||||
scf.start();
|
||||
QueueChannel channel = new QueueChannel();
|
||||
adapter.setOutputChannel(channel);
|
||||
int n = 0;
|
||||
while (!scf.isListening()) {
|
||||
Thread.sleep(100);
|
||||
if (n++ > 100) {
|
||||
fail("Failed to listen");
|
||||
}
|
||||
}
|
||||
List<Socket> sockets = new LinkedList<Socket>();
|
||||
for (int i = 100; i < 200; i++) {
|
||||
Socket socket1 = SocketFactory.getDefault().createSocket("localhost", port);
|
||||
socket1.setSoTimeout(2000);
|
||||
socket1.getOutputStream().write(("Test" + i + "\r\n").getBytes());
|
||||
sockets.add(socket1);
|
||||
}
|
||||
for (int i = 100; i < 200; i++) {
|
||||
Message<?> message = channel.receive(10000);
|
||||
assertNotNull(message);
|
||||
handler.handleMessage(message);
|
||||
}
|
||||
byte[] b = new byte[9];
|
||||
for (int i = 100; i < 200; i++) {
|
||||
readFully(sockets.remove(0).getInputStream(), b);
|
||||
assertEquals("Test" + i + "\r\n", new String(b));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -16,22 +16,42 @@
|
||||
package org.springframework.integration.ip.tcp;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
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.Set;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import javax.net.ServerSocketFactory;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.commons.serializer.JavaSerializationConverter;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory;
|
||||
import org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory;
|
||||
import org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory;
|
||||
import org.springframework.integration.ip.tcp.converter.ByteArrayCrLfConverter;
|
||||
import org.springframework.integration.ip.tcp.converter.ByteArrayLengthHeaderConverter;
|
||||
import org.springframework.integration.ip.tcp.converter.ByteArrayStxEtxConverter;
|
||||
import org.springframework.integration.ip.util.SocketUtils;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
|
||||
|
||||
/**
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
public class TcpSendingMessageHandlerTests {
|
||||
@@ -197,13 +217,655 @@ public class TcpSendingMessageHandlerTests {
|
||||
server.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param is
|
||||
* @param buff
|
||||
*/
|
||||
private void readFully(InputStream is, byte[] buff) throws IOException {
|
||||
for (int i = 0; i < buff.length; i++) {
|
||||
buff[i] = (byte) is.read();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNetCrLf() throws Exception {
|
||||
final int port = SocketUtils.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);
|
||||
ByteArrayCrLfConverter converter = new ByteArrayCrLfConverter();
|
||||
ccf.setInputConverter(converter);
|
||||
ccf.setOutputConverter(converter);
|
||||
ccf.setSoTimeout(10000);
|
||||
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.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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNio() throws Exception {
|
||||
final int port = SocketUtils.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 TcpNioClientConnectionFactory("localhost", port);
|
||||
ByteArrayCrLfConverter converter = new ByteArrayCrLfConverter();
|
||||
ccf.setInputConverter(converter);
|
||||
ccf.setOutputConverter(converter);
|
||||
// ccf.setSoTimeout(10000);
|
||||
ccf.start();
|
||||
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.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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNetStxEtx() throws Exception {
|
||||
final int port = SocketUtils.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 = ("\u0002Reply" + (++i) + "\u0003").getBytes();
|
||||
socket.getOutputStream().write(b);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (!done.get()) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
|
||||
ByteArrayStxEtxConverter converter = new ByteArrayStxEtxConverter();
|
||||
ccf.setInputConverter(converter);
|
||||
ccf.setOutputConverter(converter);
|
||||
ccf.setSoTimeout(10000);
|
||||
ccf.start();
|
||||
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.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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNioStxEtx() throws Exception {
|
||||
final int port = SocketUtils.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 = ("\u0002Reply" + (++i) + "\u0003").getBytes();
|
||||
socket.getOutputStream().write(b);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (!done.get()) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
|
||||
ByteArrayStxEtxConverter converter = new ByteArrayStxEtxConverter();
|
||||
ccf.setInputConverter(converter);
|
||||
ccf.setOutputConverter(converter);
|
||||
ccf.setSoTimeout(10000);
|
||||
ccf.start();
|
||||
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.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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNetLength() throws Exception {
|
||||
final int port = SocketUtils.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[8];
|
||||
readFully(socket.getInputStream(), b);
|
||||
if (!"\u0000\u0000\u0000\u0004Test".equals(new String(b))) {
|
||||
throw new RuntimeException("Bad Data");
|
||||
}
|
||||
b = ("\u0000\u0000\u0000\u0006Reply" + (++i)).getBytes();
|
||||
socket.getOutputStream().write(b);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (!done.get()) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
|
||||
ByteArrayLengthHeaderConverter converter = new ByteArrayLengthHeaderConverter();
|
||||
ccf.setInputConverter(converter);
|
||||
ccf.setOutputConverter(converter);
|
||||
ccf.setSoTimeout(10000);
|
||||
ccf.start();
|
||||
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.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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNioLength() throws Exception {
|
||||
final int port = SocketUtils.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[8];
|
||||
readFully(socket.getInputStream(), b);
|
||||
if (!"\u0000\u0000\u0000\u0004Test".equals(new String(b))) {
|
||||
throw new RuntimeException("Bad Data");
|
||||
}
|
||||
b = ("\u0000\u0000\u0000\u0006Reply" + (++i)).getBytes();
|
||||
socket.getOutputStream().write(b);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (!done.get()) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
|
||||
ByteArrayLengthHeaderConverter converter = new ByteArrayLengthHeaderConverter();
|
||||
ccf.setInputConverter(converter);
|
||||
ccf.setOutputConverter(converter);
|
||||
ccf.setSoTimeout(10000);
|
||||
ccf.start();
|
||||
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.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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNetSerial() throws Exception {
|
||||
final int port = SocketUtils.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) {
|
||||
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
|
||||
ois.readObject();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
|
||||
oos.writeObject("Reply" + (++i));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (!done.get()) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
|
||||
JavaSerializationConverter converter = new JavaSerializationConverter();
|
||||
ccf.setInputConverter(converter);
|
||||
ccf.setOutputConverter(converter);
|
||||
ccf.setSoTimeout(10000);
|
||||
ccf.start();
|
||||
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.handleMessage(MessageBuilder.withPayload("Test").build());
|
||||
handler.handleMessage(MessageBuilder.withPayload("Test").build());
|
||||
Message<?> mOut = channel.receive(10000);
|
||||
assertNotNull(mOut);
|
||||
assertEquals("Reply1", mOut.getPayload());
|
||||
mOut = channel.receive(10000);
|
||||
assertNotNull(mOut);
|
||||
assertEquals("Reply2", mOut.getPayload());
|
||||
done.set(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNioSerial() throws Exception {
|
||||
final int port = SocketUtils.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) {
|
||||
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
|
||||
ois.readObject();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
|
||||
oos.writeObject("Reply" + (++i));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (!done.get()) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
|
||||
JavaSerializationConverter converter = new JavaSerializationConverter();
|
||||
ccf.setInputConverter(converter);
|
||||
ccf.setOutputConverter(converter);
|
||||
ccf.setSoTimeout(10000);
|
||||
ccf.start();
|
||||
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.handleMessage(MessageBuilder.withPayload("Test").build());
|
||||
handler.handleMessage(MessageBuilder.withPayload("Test").build());
|
||||
Message<?> mOut = channel.receive(10000);
|
||||
assertNotNull(mOut);
|
||||
assertEquals("Reply1", mOut.getPayload());
|
||||
mOut = channel.receive(10000);
|
||||
assertNotNull(mOut);
|
||||
assertEquals("Reply2", mOut.getPayload());
|
||||
done.set(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNetSingleUseNoInbound() throws Exception {
|
||||
final int port = SocketUtils.findAvailableServerSocket();
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
final Semaphore semaphore = new Semaphore(0);
|
||||
final AtomicBoolean done = new AtomicBoolean();
|
||||
Executors.newSingleThreadExecutor().execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
|
||||
latch.countDown();
|
||||
while (true) {
|
||||
Socket socket = server.accept();
|
||||
semaphore.release();
|
||||
byte[] b = new byte[6];
|
||||
readFully(socket.getInputStream(), b);
|
||||
semaphore.release();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (!done.get()) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
|
||||
ByteArrayCrLfConverter converter = new ByteArrayCrLfConverter();
|
||||
ccf.setInputConverter(converter);
|
||||
ccf.setOutputConverter(converter);
|
||||
ccf.setSoTimeout(10000);
|
||||
ccf.start();
|
||||
ccf.setSingleUse(true);
|
||||
TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
|
||||
handler.setConnectionFactory(ccf);
|
||||
assertTrue(latch.await(10, TimeUnit.SECONDS));
|
||||
handler.handleMessage(MessageBuilder.withPayload("Test").build());
|
||||
handler.handleMessage(MessageBuilder.withPayload("Test").build());
|
||||
assertTrue(semaphore.tryAcquire(4, 10000, TimeUnit.MILLISECONDS));
|
||||
done.set(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNioSingleUseNoInbound() throws Exception {
|
||||
final int port = SocketUtils.findAvailableServerSocket();
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
final Semaphore semaphore = new Semaphore(0);
|
||||
final AtomicBoolean done = new AtomicBoolean();
|
||||
Executors.newSingleThreadExecutor().execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
|
||||
latch.countDown();
|
||||
while (true) {
|
||||
Socket socket = server.accept();
|
||||
semaphore.release();
|
||||
byte[] b = new byte[6];
|
||||
readFully(socket.getInputStream(), b);
|
||||
semaphore.release();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (!done.get()) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
|
||||
ByteArrayCrLfConverter converter = new ByteArrayCrLfConverter();
|
||||
ccf.setInputConverter(converter);
|
||||
ccf.setOutputConverter(converter);
|
||||
ccf.setSoTimeout(10000);
|
||||
ccf.start();
|
||||
ccf.setSingleUse(true);
|
||||
TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
|
||||
handler.setConnectionFactory(ccf);
|
||||
assertTrue(latch.await(10, TimeUnit.SECONDS));
|
||||
handler.handleMessage(MessageBuilder.withPayload("Test").build());
|
||||
handler.handleMessage(MessageBuilder.withPayload("Test").build());
|
||||
assertTrue(semaphore.tryAcquire(4, 10000, TimeUnit.MILLISECONDS));
|
||||
done.set(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNetSingleUseWithInbound() throws Exception {
|
||||
final int port = SocketUtils.findAvailableServerSocket();
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
final Semaphore semaphore = new Semaphore(0);
|
||||
final AtomicBoolean done = new AtomicBoolean();
|
||||
Executors.newSingleThreadExecutor().execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
|
||||
latch.countDown();
|
||||
int i = 0;
|
||||
while (true) {
|
||||
Socket socket = server.accept();
|
||||
semaphore.release();
|
||||
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);
|
||||
ByteArrayCrLfConverter converter = new ByteArrayCrLfConverter();
|
||||
ccf.setInputConverter(converter);
|
||||
ccf.setOutputConverter(converter);
|
||||
ccf.setSoTimeout(10000);
|
||||
ccf.start();
|
||||
ccf.setSingleUse(true);
|
||||
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.handleMessage(MessageBuilder.withPayload("Test").build());
|
||||
handler.handleMessage(MessageBuilder.withPayload("Test").build());
|
||||
assertTrue(semaphore.tryAcquire(2, 10000, TimeUnit.MILLISECONDS));
|
||||
Set<String> replies = new HashSet<String>();
|
||||
for (int i = 0; i < 2; i++) {
|
||||
Message<?> mOut = channel.receive(10000);
|
||||
assertNotNull(mOut);
|
||||
replies.add(new String((byte[])mOut.getPayload()));
|
||||
}
|
||||
assertTrue(replies.remove("Reply1"));
|
||||
assertTrue(replies.remove("Reply2"));
|
||||
done.set(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNioSingleUseWithInbound() throws Exception {
|
||||
final int port = SocketUtils.findAvailableServerSocket();
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
final Semaphore semaphore = new Semaphore(0);
|
||||
final AtomicBoolean done = new AtomicBoolean();
|
||||
Executors.newSingleThreadExecutor().execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
|
||||
latch.countDown();
|
||||
int i = 0;
|
||||
while (true) {
|
||||
Socket socket = server.accept();
|
||||
semaphore.release();
|
||||
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 TcpNioClientConnectionFactory("localhost", port);
|
||||
ByteArrayCrLfConverter converter = new ByteArrayCrLfConverter();
|
||||
ccf.setInputConverter(converter);
|
||||
ccf.setOutputConverter(converter);
|
||||
ccf.setSoTimeout(10000);
|
||||
ccf.start();
|
||||
ccf.setSingleUse(true);
|
||||
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.handleMessage(MessageBuilder.withPayload("Test").build());
|
||||
handler.handleMessage(MessageBuilder.withPayload("Test").build());
|
||||
assertTrue(semaphore.tryAcquire(2, 10000, TimeUnit.MILLISECONDS));
|
||||
Set<String> replies = new HashSet<String>();
|
||||
for (int i = 0; i < 2; i++) {
|
||||
Message<?> mOut = channel.receive(10000);
|
||||
assertNotNull(mOut);
|
||||
replies.add(new String((byte[])mOut.getPayload()));
|
||||
}
|
||||
assertTrue(replies.remove("Reply1"));
|
||||
assertTrue(replies.remove("Reply2"));
|
||||
done.set(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newTestNioSingleUseWithInboundMany() throws Exception {
|
||||
final int port = SocketUtils.findAvailableServerSocket();
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
final Semaphore semaphore = new Semaphore(0);
|
||||
final AtomicBoolean done = new AtomicBoolean();
|
||||
Executors.newSingleThreadExecutor().execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
|
||||
latch.countDown();
|
||||
int i = 0;
|
||||
while (true) {
|
||||
Socket socket = server.accept();
|
||||
semaphore.release();
|
||||
byte[] b = new byte[8];
|
||||
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 TcpNioClientConnectionFactory("localhost", port);
|
||||
ByteArrayCrLfConverter converter = new ByteArrayCrLfConverter();
|
||||
ccf.setInputConverter(converter);
|
||||
ccf.setOutputConverter(converter);
|
||||
ccf.setSoTimeout(10000);
|
||||
ccf.start();
|
||||
ccf.setSingleUse(true);
|
||||
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));
|
||||
for (int i = 100; i < 200; i++) {
|
||||
handler.handleMessage(MessageBuilder.withPayload("Test" + i).build());
|
||||
}
|
||||
assertTrue(semaphore.tryAcquire(10, 10000, TimeUnit.MILLISECONDS));
|
||||
Set<String> replies = new HashSet<String>();
|
||||
for (int i = 100; i < 200; i++) {
|
||||
Message<?> mOut = channel.receive(10000);
|
||||
assertNotNull(mOut);
|
||||
replies.add(new String((byte[])mOut.getPayload()));
|
||||
}
|
||||
for (int i = 0; i < 100; i++) {
|
||||
assertTrue("Reply" + i + " missing", replies.remove("Reply" + i));
|
||||
}
|
||||
done.set(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* 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.
|
||||
* 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.connection;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import javax.net.ServerSocketFactory;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.ip.util.SocketUtils;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
|
||||
|
||||
/**
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
public class TcpNioConnectionTests {
|
||||
|
||||
@Test
|
||||
public void testWriteTimeout() throws Exception {
|
||||
final int port = SocketUtils.findAvailableServerSocket();
|
||||
TcpNioClientConnectionFactory factory = new TcpNioClientConnectionFactory("localhost", port);
|
||||
factory.setSoTimeout(1000);
|
||||
factory.start();
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
Executors.newSingleThreadExecutor().execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
|
||||
latch.countDown();
|
||||
server.accept();
|
||||
// block so we fill the buffer
|
||||
server.accept();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
|
||||
try {
|
||||
TcpNioConnection connection = factory.getConnection();
|
||||
connection.send(MessageBuilder.withPayload(new byte[1000000]).build());
|
||||
} catch (Exception e) {
|
||||
assertTrue("Expected SocketTimeoutException, got " + e.getClass().getSimpleName() +
|
||||
":" + e.getMessage(), e instanceof SocketTimeoutException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadTimeout() throws Exception {
|
||||
final int port = SocketUtils.findAvailableServerSocket();
|
||||
TcpNioClientConnectionFactory factory = new TcpNioClientConnectionFactory("localhost", port);
|
||||
factory.setSoTimeout(1000);
|
||||
factory.start();
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
Executors.newSingleThreadExecutor().execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
|
||||
latch.countDown();
|
||||
Socket socket = server.accept();
|
||||
byte[] b = new byte[6];
|
||||
readFully(socket.getInputStream(), b);
|
||||
// block to cause timeout on read.
|
||||
server.accept();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
|
||||
try {
|
||||
TcpNioConnection connection = factory.getConnection();
|
||||
connection.send(MessageBuilder.withPayload("Test").build());
|
||||
Thread.sleep(2000);
|
||||
assertTrue(!connection.isOpen());
|
||||
} catch (Exception e) {
|
||||
fail("Unexptected exception " + e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void readFully(InputStream is, byte[] buff) throws IOException {
|
||||
for (int i = 0; i < buff.length; i++) {
|
||||
buff[i] = (byte) is.read();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user