INT-1149 Support the specification of a network interface via local-address attribute on inbound endpoints and outbound UDP adapter. Docs to follow.
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
so-receive-buffer-size="30"
|
||||
so-send-buffer-size="31"
|
||||
so-timeout="32"
|
||||
local-address="127.0.0.1"
|
||||
/>
|
||||
|
||||
<ip:inbound-channel-adapter id="testInUdpMulticast"
|
||||
@@ -42,6 +43,7 @@
|
||||
so-receive-buffer-size="30"
|
||||
so-send-buffer-size="31"
|
||||
so-timeout="32"
|
||||
local-address="127.0.0.1"
|
||||
/>
|
||||
|
||||
<ip:inbound-channel-adapter id="testInTcpNio"
|
||||
@@ -58,6 +60,7 @@
|
||||
so-timeout="32"
|
||||
using-direct-buffers="false"
|
||||
using-nio="true"
|
||||
local-address="127.0.0.1"
|
||||
/>
|
||||
|
||||
<ip:inbound-channel-adapter id="testInTcpNioDirect"
|
||||
@@ -89,6 +92,7 @@
|
||||
so-keep-alive="true"
|
||||
so-receive-buffer-size="30"
|
||||
so-timeout="32"
|
||||
local-address="127.0.0.1"
|
||||
/>
|
||||
|
||||
<ip:inbound-channel-adapter id="testInTcpNetSerialized"
|
||||
@@ -118,6 +122,7 @@
|
||||
so-receive-buffer-size="52"
|
||||
so-send-buffer-size="53"
|
||||
so-timeout="54"
|
||||
local-address="127.0.0.1"
|
||||
/>
|
||||
|
||||
<ip:outbound-channel-adapter id="testOutUdpiMulticast"
|
||||
@@ -215,7 +220,7 @@
|
||||
so-receive-buffer-size="124"
|
||||
so-send-buffer-size="125"
|
||||
so-timeout="126"
|
||||
|
||||
local-address="127.0.0.1"
|
||||
/>
|
||||
|
||||
<ip:outbound-gateway id="simpleOutGateway"
|
||||
|
||||
@@ -127,6 +127,7 @@ public class ParserUnitTests {
|
||||
assertEquals(30, dfa.getPropertyValue("soReceiveBufferSize"));
|
||||
assertEquals(31, dfa.getPropertyValue("soSendBufferSize"));
|
||||
assertEquals(32, dfa.getPropertyValue("soTimeout"));
|
||||
assertEquals("127.0.0.1", dfa.getPropertyValue("localAddress"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -139,6 +140,7 @@ public class ParserUnitTests {
|
||||
assertEquals(30, dfa.getPropertyValue("soReceiveBufferSize"));
|
||||
assertEquals(31, dfa.getPropertyValue("soSendBufferSize"));
|
||||
assertEquals(32, dfa.getPropertyValue("soTimeout"));
|
||||
assertEquals("127.0.0.1", dfa.getPropertyValue("localAddress"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -154,6 +156,7 @@ public class ParserUnitTests {
|
||||
assertEquals(30, dfa.getPropertyValue("soReceiveBufferSize"));
|
||||
assertEquals(32, dfa.getPropertyValue("soTimeout"));
|
||||
assertEquals(false, dfa.getPropertyValue("close"));
|
||||
assertEquals("127.0.0.1", dfa.getPropertyValue("localAddress"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -183,6 +186,7 @@ public class ParserUnitTests {
|
||||
assertEquals(30, dfa.getPropertyValue("soReceiveBufferSize"));
|
||||
assertEquals(32, dfa.getPropertyValue("soTimeout"));
|
||||
assertEquals(false, dfa.getPropertyValue("close"));
|
||||
assertEquals("127.0.0.1", dfa.getPropertyValue("localAddress"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -215,6 +219,7 @@ public class ParserUnitTests {
|
||||
assertEquals(52, dfa.getPropertyValue("soReceiveBufferSize"));
|
||||
assertEquals(53, dfa.getPropertyValue("soSendBufferSize"));
|
||||
assertEquals(54, dfa.getPropertyValue("soTimeout"));
|
||||
assertEquals("127.0.0.1", dfa.getPropertyValue("localAddress"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -311,6 +316,7 @@ public class ParserUnitTests {
|
||||
assertEquals(126, dfa.getPropertyValue("soTimeout"));
|
||||
assertEquals(23, dfa.getPropertyValue("poolSize"));
|
||||
assertEquals(false, dfa.getPropertyValue("close"));
|
||||
assertEquals("127.0.0.1", dfa.getPropertyValue("localAddress"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
* Ensures that all messages are correctly assembled and received ok.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
public class MultiClientTests {
|
||||
@@ -46,16 +47,14 @@ public class MultiClientTests {
|
||||
new TcpNetReceivingChannelAdapter(SocketUtils.findAvailableServerSocket());
|
||||
int drivers = 10;
|
||||
adapter.setPoolSize(drivers);
|
||||
adapter.setReceiveBufferSize(10000);
|
||||
QueueChannel queue = new QueueChannel(drivers * 3);
|
||||
adapter.setOutputChannel(queue);
|
||||
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
|
||||
taskScheduler.initialize();
|
||||
adapter.setTaskScheduler(taskScheduler);
|
||||
adapter.start();
|
||||
while (!adapter.isRunning()) {
|
||||
Thread.sleep(50); // wait for server to start listening
|
||||
}
|
||||
Thread.sleep(250); // wait for listener
|
||||
SocketUtils.waitListening(adapter);
|
||||
for (int i = 0; i < drivers; i++) {
|
||||
Thread t = new Thread( new Runnable() {
|
||||
public void run() {
|
||||
@@ -85,6 +84,7 @@ public class MultiClientTests {
|
||||
final TcpNioReceivingChannelAdapter adapter =
|
||||
new TcpNioReceivingChannelAdapter(SocketUtils.findAvailableServerSocket());
|
||||
adapter.setPoolSize(4);
|
||||
adapter.setReceiveBufferSize(10000);
|
||||
int drivers = 10;
|
||||
QueueChannel queue = new QueueChannel(drivers * 3);
|
||||
adapter.setOutputChannel(queue);
|
||||
@@ -92,10 +92,7 @@ public class MultiClientTests {
|
||||
taskScheduler.initialize();
|
||||
adapter.setTaskScheduler(taskScheduler);
|
||||
adapter.start();
|
||||
while (!adapter.isRunning()) {
|
||||
Thread.sleep(50); // wait for server to start its listener.
|
||||
}
|
||||
Thread.sleep(250); // wait for listener
|
||||
SocketUtils.waitListening(adapter);
|
||||
for (int i = 0; i < drivers; i++) {
|
||||
Thread t = new Thread( new Runnable() {
|
||||
public void run() {
|
||||
|
||||
@@ -45,8 +45,9 @@ public class TcpReceivingChannelAdapterTests {
|
||||
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
|
||||
taskScheduler.initialize();
|
||||
adapter.setTaskScheduler(taskScheduler);
|
||||
SocketUtils.setLocalNicIfPossible(adapter);
|
||||
adapter.start();
|
||||
waitListening(adapter);
|
||||
SocketUtils.waitListening(adapter);
|
||||
SocketUtils.testSendLength(port, null); //sends 2 copies of TEST_STRING twice
|
||||
Message<?> message = channel.receive(2000);
|
||||
assertNotNull(message);
|
||||
@@ -75,7 +76,7 @@ public class TcpReceivingChannelAdapterTests {
|
||||
taskScheduler.initialize();
|
||||
adapter.setTaskScheduler(taskScheduler);
|
||||
adapter.start();
|
||||
waitListening(adapter);
|
||||
SocketUtils.waitListening(adapter);
|
||||
SocketUtils.testSendStxEtx(port, null); //sends 2 copies of TEST_STRING twice
|
||||
Message<?> message = channel.receive(4000);
|
||||
assertNotNull(message);
|
||||
@@ -101,8 +102,9 @@ public class TcpReceivingChannelAdapterTests {
|
||||
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
|
||||
taskScheduler.initialize();
|
||||
adapter.setTaskScheduler(taskScheduler);
|
||||
SocketUtils.setLocalNicIfPossible(adapter);
|
||||
adapter.start();
|
||||
waitListening(adapter);
|
||||
SocketUtils.waitListening(adapter);
|
||||
SocketUtils.testSendLength(port, null); //sends 2 copies of TEST_STRING twice
|
||||
Message<?> message = channel.receive(2000);
|
||||
assertNotNull(message);
|
||||
@@ -130,7 +132,7 @@ public class TcpReceivingChannelAdapterTests {
|
||||
taskScheduler.initialize();
|
||||
adapter.setTaskScheduler(taskScheduler);
|
||||
adapter.start();
|
||||
waitListening(adapter);
|
||||
SocketUtils.waitListening(adapter);
|
||||
SocketUtils.testSendStxEtx(port, null); //sends 2 copies of TEST_STRING twice
|
||||
Message<?> message = channel.receive(2000);
|
||||
assertNotNull(message);
|
||||
@@ -160,7 +162,7 @@ public class TcpReceivingChannelAdapterTests {
|
||||
taskScheduler.initialize();
|
||||
adapter.setTaskScheduler(taskScheduler);
|
||||
adapter.start();
|
||||
waitListening(adapter);
|
||||
SocketUtils.waitListening(adapter);
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
SocketUtils.testSendCrLfSingle(port, latch);
|
||||
Message<?> message = channel.receive(5000);
|
||||
@@ -195,7 +197,7 @@ public class TcpReceivingChannelAdapterTests {
|
||||
taskScheduler.initialize();
|
||||
adapter.setTaskScheduler(taskScheduler);
|
||||
adapter.start();
|
||||
waitListening(adapter);
|
||||
SocketUtils.waitListening(adapter);
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
SocketUtils.testSendCrLfSingle(port, latch);
|
||||
Message<?> message = channel.receive(2000);
|
||||
@@ -210,18 +212,5 @@ public class TcpReceivingChannelAdapterTests {
|
||||
new String((byte[])message.getPayload()));
|
||||
adapter.stop();
|
||||
}
|
||||
|
||||
private void waitListening(AbstractInternetProtocolReceivingChannelAdapter adapter) throws Exception {
|
||||
int n = 0;
|
||||
while (!adapter.isListening()) {
|
||||
Thread.sleep(100);
|
||||
if (n++ > 100) {
|
||||
throw new Exception("Gateway failed to listen");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ import org.springframework.integration.ip.IpHeaders;
|
||||
import org.springframework.integration.ip.udp.DatagramPacketMessageMapper;
|
||||
import org.springframework.integration.ip.udp.MulticastSendingMessageHandler;
|
||||
import org.springframework.integration.ip.udp.UnicastSendingMessageHandler;
|
||||
import org.springframework.integration.ip.util.SocketUtils;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
|
||||
/**
|
||||
@@ -47,9 +48,11 @@ import org.springframework.integration.message.MessageBuilder;
|
||||
*/
|
||||
public class DatagramPacketSendingHandlerTests {
|
||||
|
||||
private boolean noMulticast;
|
||||
|
||||
@Test
|
||||
public void verifySend() throws Exception {
|
||||
final int testPort = 27816;
|
||||
final int testPort = SocketUtils.findAvailableUdpSocket();
|
||||
byte[] buffer = new byte[8];
|
||||
final DatagramPacket receivedPacket = new DatagramPacket(buffer, buffer.length);
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
@@ -83,11 +86,12 @@ public class DatagramPacketSendingHandlerTests {
|
||||
|
||||
@Test
|
||||
public void verifySendWithAck() throws Exception {
|
||||
final int testPort = 27816;
|
||||
final int ackPort = 17816;
|
||||
final int testPort = SocketUtils.findAvailableUdpSocket();
|
||||
final int ackPort = SocketUtils.findAvailableUdpSocket(testPort + 1);
|
||||
byte[] buffer = new byte[1000];
|
||||
final DatagramPacket receivedPacket = new DatagramPacket(buffer, buffer.length);
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
final CountDownLatch latch1 = new CountDownLatch(1);
|
||||
final CountDownLatch latch2 = new CountDownLatch(1);
|
||||
UnicastSendingMessageHandler handler =
|
||||
new UnicastSendingMessageHandler("localhost", testPort, true,
|
||||
true, "localhost", ackPort, 5000);
|
||||
@@ -95,6 +99,7 @@ public class DatagramPacketSendingHandlerTests {
|
||||
public void run() {
|
||||
try {
|
||||
DatagramSocket socket = new DatagramSocket(testPort);
|
||||
latch1.countDown();
|
||||
socket.receive(receivedPacket);
|
||||
socket.close();
|
||||
DatagramPacketMessageMapper mapper = new DatagramPacketMessageMapper();
|
||||
@@ -108,17 +113,17 @@ public class DatagramPacketSendingHandlerTests {
|
||||
DatagramSocket out = new DatagramSocket();
|
||||
out.send(ackPack);
|
||||
out.close();
|
||||
latch.countDown();
|
||||
latch2.countDown();
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
Thread.sleep(3000);
|
||||
latch1.await(3000, TimeUnit.MILLISECONDS);
|
||||
String payload = "foobar";
|
||||
handler.handleMessage(MessageBuilder.withPayload(payload).build());
|
||||
assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
|
||||
assertTrue(latch2.await(10000, TimeUnit.MILLISECONDS));
|
||||
byte[] src = receivedPacket.getData();
|
||||
int length = receivedPacket.getLength();
|
||||
int offset = receivedPacket.getOffset();
|
||||
@@ -131,10 +136,11 @@ public class DatagramPacketSendingHandlerTests {
|
||||
@Test
|
||||
@Ignore
|
||||
public void verifySendMulticast() throws Exception {
|
||||
final int testPort = 27816;
|
||||
final int testPort = SocketUtils.findAvailableUdpSocket();
|
||||
final String multicastAddress = "225.6.7.8";
|
||||
final String payload = "foo";
|
||||
final CountDownLatch latch = new CountDownLatch(2);
|
||||
final CountDownLatch latch1 = new CountDownLatch(2);
|
||||
final CountDownLatch latch2 = new CountDownLatch(2);
|
||||
Runnable catcher = new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
@@ -143,6 +149,7 @@ public class DatagramPacketSendingHandlerTests {
|
||||
MulticastSocket socket = new MulticastSocket(testPort);
|
||||
InetAddress group = InetAddress.getByName(multicastAddress);
|
||||
socket.joinGroup(group);
|
||||
latch1.countDown();
|
||||
LogFactory.getLog(getClass())
|
||||
.debug(Thread.currentThread().getName() + " waiting for packet");
|
||||
socket.receive(receivedPacket);
|
||||
@@ -155,9 +162,11 @@ public class DatagramPacketSendingHandlerTests {
|
||||
assertEquals(payload, new String(dest));
|
||||
LogFactory.getLog(getClass())
|
||||
.debug(Thread.currentThread().getName() + " received packet");
|
||||
latch.countDown();
|
||||
latch2.countDown();
|
||||
}
|
||||
catch (Exception e) {
|
||||
noMulticast = true;
|
||||
latch1.countDown();
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -165,21 +174,25 @@ public class DatagramPacketSendingHandlerTests {
|
||||
Executor executor = Executors.newFixedThreadPool(2);
|
||||
executor.execute(catcher);
|
||||
executor.execute(catcher);
|
||||
Thread.sleep(1000);
|
||||
latch1.await(3000, TimeUnit.MILLISECONDS);
|
||||
if (noMulticast) {
|
||||
return;
|
||||
}
|
||||
MulticastSendingMessageHandler handler = new MulticastSendingMessageHandler(multicastAddress, testPort);
|
||||
handler.handleMessage(MessageBuilder.withPayload(payload).build());
|
||||
assertTrue(latch.await(3000, TimeUnit.MILLISECONDS));
|
||||
assertTrue(latch2.await(3000, TimeUnit.MILLISECONDS));
|
||||
handler.shutDown();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void verifySendMulticastWithAcks() throws Exception {
|
||||
final int testPort = 27816;
|
||||
final int ackPort = 17817;
|
||||
final int testPort = SocketUtils.findAvailableUdpSocket();
|
||||
final int ackPort = SocketUtils.findAvailableUdpSocket(testPort + 1);
|
||||
final String multicastAddress = "225.6.7.8";
|
||||
final String payload = "foobar";
|
||||
final CountDownLatch latch = new CountDownLatch(2);
|
||||
final CountDownLatch latch1 = new CountDownLatch(2);
|
||||
final CountDownLatch latch2 = new CountDownLatch(2);
|
||||
Runnable catcher = new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
@@ -188,6 +201,7 @@ public class DatagramPacketSendingHandlerTests {
|
||||
MulticastSocket socket = new MulticastSocket(testPort);
|
||||
InetAddress group = InetAddress.getByName(multicastAddress);
|
||||
socket.joinGroup(group);
|
||||
latch1.countDown();
|
||||
LogFactory.getLog(getClass()).debug(Thread.currentThread().getName() + " waiting for packet");
|
||||
socket.receive(receivedPacket);
|
||||
socket.close();
|
||||
@@ -209,9 +223,11 @@ public class DatagramPacketSendingHandlerTests {
|
||||
DatagramSocket out = new DatagramSocket();
|
||||
out.send(ackPack);
|
||||
out.close();
|
||||
latch.countDown();
|
||||
latch2.countDown();
|
||||
}
|
||||
catch (Exception e) {
|
||||
noMulticast = true;
|
||||
latch1.countDown();
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -219,13 +235,16 @@ public class DatagramPacketSendingHandlerTests {
|
||||
Executor executor = Executors.newFixedThreadPool(2);
|
||||
executor.execute(catcher);
|
||||
executor.execute(catcher);
|
||||
Thread.sleep(3000);
|
||||
latch1.await(3000, TimeUnit.MILLISECONDS);
|
||||
if (noMulticast) {
|
||||
return;
|
||||
}
|
||||
MulticastSendingMessageHandler handler =
|
||||
new MulticastSendingMessageHandler(multicastAddress, testPort, true,
|
||||
true, "localhost", ackPort, 500000);;
|
||||
true, "localhost", ackPort, 500000);
|
||||
handler.setMinAcksForSuccess(2);
|
||||
handler.handleMessage(MessageBuilder.withPayload(payload).build());
|
||||
assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
|
||||
assertTrue(latch2.await(10000, TimeUnit.MILLISECONDS));
|
||||
handler.shutDown();
|
||||
}
|
||||
|
||||
|
||||
@@ -58,10 +58,7 @@ public class MultiClientTests {
|
||||
adapter.setTaskScheduler(taskScheduler);
|
||||
adapter.start();
|
||||
final QueueChannel queueIn = new QueueChannel(1000);
|
||||
while (!adapter.isRunning()) {
|
||||
Thread.sleep(50); // wait for server to start listening
|
||||
}
|
||||
Thread.sleep(250); // wait for listener
|
||||
SocketUtils.waitListening(adapter);
|
||||
for (int i = 0; i < drivers; i++) {
|
||||
Thread t = new Thread( new Runnable() {
|
||||
public void run() {
|
||||
@@ -102,10 +99,7 @@ public class MultiClientTests {
|
||||
adapter.setTaskScheduler(taskScheduler);
|
||||
adapter.start();
|
||||
final QueueChannel queueIn = new QueueChannel(1000);
|
||||
while (!adapter.isRunning()) {
|
||||
Thread.sleep(50); // wait for server to start listening
|
||||
}
|
||||
Thread.sleep(250); // wait for listener
|
||||
SocketUtils.waitListening(adapter);
|
||||
for (int i = 0; i < drivers; i++) {
|
||||
final int j = i;
|
||||
Thread t = new Thread( new Runnable() {
|
||||
@@ -150,10 +144,7 @@ public class MultiClientTests {
|
||||
adapter.setTaskScheduler(taskScheduler);
|
||||
adapter.start();
|
||||
final QueueChannel queueIn = new QueueChannel(1000);
|
||||
while (!adapter.isRunning()) {
|
||||
Thread.sleep(50); // wait for server to start listening
|
||||
}
|
||||
Thread.sleep(250); // wait for listener
|
||||
SocketUtils.waitListening(adapter);
|
||||
for (int i = 0; i < drivers; i++) {
|
||||
final int j = i;
|
||||
Thread t = new Thread( new Runnable() {
|
||||
@@ -161,7 +152,7 @@ public class MultiClientTests {
|
||||
UnicastSendingMessageHandler sender = new UnicastSendingMessageHandler(
|
||||
"localhost", adapter.getPort(),
|
||||
true, true, "localhost",
|
||||
SocketUtils.findAvailableUdpSocket(adapter.getPort() + j + 1000),
|
||||
SocketUtils.findAvailableUdpSocket(adapter.getPort() + j + 1100),
|
||||
10000);
|
||||
while (true) {
|
||||
Message message = queueIn.receive();
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
package org.springframework.integration.ip.udp;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.ip.util.SocketUtils;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
|
||||
|
||||
public class UdpChannelAdapterTests {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testUnicastReceiver() throws Exception {
|
||||
QueueChannel channel = new QueueChannel(2);
|
||||
int port = SocketUtils.findAvailableUdpSocket();
|
||||
UnicastReceivingChannelAdapter adapter = new UnicastReceivingChannelAdapter(port);
|
||||
adapter.setOutputChannel(channel);
|
||||
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
|
||||
taskScheduler.initialize();
|
||||
adapter.setTaskScheduler(taskScheduler);
|
||||
SocketUtils.setLocalNicIfPossible(adapter);
|
||||
adapter.start();
|
||||
SocketUtils.waitListening(adapter);
|
||||
|
||||
Message<byte[]> message = MessageBuilder.withPayload("ABCD".getBytes()).build();
|
||||
DatagramPacketMessageMapper mapper = new DatagramPacketMessageMapper();
|
||||
DatagramPacket packet = mapper.fromMessage(message);
|
||||
packet.setSocketAddress(new InetSocketAddress("localhost", port));
|
||||
new DatagramSocket(SocketUtils.findAvailableUdpSocket()).send(packet);
|
||||
Message<byte[]> receivedMessage = (Message<byte[]>) channel.receive(2000);
|
||||
assertEquals(new String(message.getPayload()), new String(receivedMessage.getPayload()));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testUnicastSender() throws Exception {
|
||||
QueueChannel channel = new QueueChannel(2);
|
||||
int port = SocketUtils.findAvailableUdpSocket();
|
||||
UnicastReceivingChannelAdapter adapter = new UnicastReceivingChannelAdapter(port);
|
||||
adapter.setOutputChannel(channel);
|
||||
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
|
||||
taskScheduler.initialize();
|
||||
adapter.setTaskScheduler(taskScheduler);
|
||||
SocketUtils.setLocalNicIfPossible(adapter);
|
||||
adapter.start();
|
||||
SocketUtils.waitListening(adapter);
|
||||
|
||||
String whichNic = SocketUtils.chooseANic(false);
|
||||
UnicastSendingMessageHandler handler = new UnicastSendingMessageHandler(
|
||||
"localhost", port, false, true, whichNic,
|
||||
SocketUtils.findAvailableUdpSocket(), 500000);
|
||||
handler.setLocalAddress(whichNic);
|
||||
Message<byte[]> message = MessageBuilder.withPayload("ABCD".getBytes()).build();
|
||||
handler.handleMessage(message);
|
||||
Message<byte[]> receivedMessage = (Message<byte[]>) channel.receive(2000);
|
||||
assertEquals(new String(message.getPayload()), new String(receivedMessage.getPayload()));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testMulticastReceiver() throws Exception {
|
||||
QueueChannel channel = new QueueChannel(2);
|
||||
int port = SocketUtils.findAvailableUdpSocket();
|
||||
MulticastReceivingChannelAdapter adapter = new MulticastReceivingChannelAdapter("225.6.7.8", port);
|
||||
adapter.setOutputChannel(channel);
|
||||
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
|
||||
taskScheduler.initialize();
|
||||
adapter.setTaskScheduler(taskScheduler);
|
||||
String nic = SocketUtils.chooseANic(true);
|
||||
if (nic == null) { // no multicast support
|
||||
LogFactory.getLog(this.getClass()).error("No Multicast support");
|
||||
return;
|
||||
}
|
||||
adapter.setLocalAddress(nic);
|
||||
adapter.start();
|
||||
SocketUtils.waitListening(adapter);
|
||||
|
||||
Message<byte[]> message = MessageBuilder.withPayload("ABCD".getBytes()).build();
|
||||
DatagramPacketMessageMapper mapper = new DatagramPacketMessageMapper();
|
||||
DatagramPacket packet = mapper.fromMessage(message);
|
||||
packet.setSocketAddress(new InetSocketAddress("225.6.7.8", port));
|
||||
new DatagramSocket(0, Inet4Address.getByName(nic)).send(packet);
|
||||
|
||||
Message<byte[]> receivedMessage = (Message<byte[]>) channel.receive(2000);
|
||||
assertNotNull(receivedMessage);
|
||||
assertEquals(new String(message.getPayload()), new String(receivedMessage.getPayload()));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testMulticastSender() throws Exception {
|
||||
QueueChannel channel = new QueueChannel(2);
|
||||
int port = SocketUtils.findAvailableUdpSocket();
|
||||
UnicastReceivingChannelAdapter adapter = new MulticastReceivingChannelAdapter("225.6.7.9", port);
|
||||
adapter.setOutputChannel(channel);
|
||||
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
|
||||
taskScheduler.initialize();
|
||||
adapter.setTaskScheduler(taskScheduler);
|
||||
String nic = SocketUtils.chooseANic(true);
|
||||
if (nic == null) { // no multicast support
|
||||
LogFactory.getLog(this.getClass()).error("No Multicast support");
|
||||
return;
|
||||
}
|
||||
adapter.setLocalAddress(nic);
|
||||
adapter.start();
|
||||
SocketUtils.waitListening(adapter);
|
||||
|
||||
MulticastSendingMessageHandler handler = new MulticastSendingMessageHandler("225.6.7.9", port);
|
||||
handler.setLocalAddress(nic);
|
||||
Message<byte[]> message = MessageBuilder.withPayload("ABCD".getBytes()).build();
|
||||
handler.handleMessage(message);
|
||||
|
||||
Message<byte[]> receivedMessage = (Message<byte[]>) channel.receive(2000);
|
||||
assertNotNull(receivedMessage);
|
||||
assertEquals(new String(message.getPayload()), new String(receivedMessage.getPayload()));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -34,6 +34,7 @@ import org.springframework.integration.channel.ChannelResolver;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.core.MessageChannel;
|
||||
import org.springframework.integration.core.MessagingException;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
|
||||
/**
|
||||
@@ -97,6 +98,10 @@ public class UdpMulticastEndToEndTests implements Runnable {
|
||||
catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (MessagingException e) {
|
||||
// no multicast this host
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
finally {
|
||||
if (hangAroundFor == 0) {
|
||||
|
||||
@@ -19,15 +19,19 @@ import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Enumeration;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import javax.net.ServerSocketFactory;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.integration.ip.AbstractInternetProtocolReceivingChannelAdapter;
|
||||
|
||||
/**
|
||||
* TCP/IP Test utilities.
|
||||
@@ -329,4 +333,43 @@ public class SocketUtils {
|
||||
return findAvailableUdpSocket(9876);
|
||||
}
|
||||
|
||||
public static void setLocalNicIfPossible(
|
||||
AbstractInternetProtocolReceivingChannelAdapter adapter)
|
||||
throws UnknownHostException {
|
||||
InetAddress[] nics = InetAddress.getAllByName(null);
|
||||
if (nics.length > 0) {
|
||||
// just listen on the loopback interface
|
||||
String loopBack = nics[0].getHostAddress();
|
||||
adapter.setLocalAddress(loopBack);
|
||||
}
|
||||
}
|
||||
|
||||
public static String chooseANic(boolean multicast) throws Exception {
|
||||
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
||||
while (interfaces.hasMoreElements()) {
|
||||
NetworkInterface intface = interfaces.nextElement();
|
||||
if (intface.isLoopback() || (multicast && !intface.supportsMulticast()))
|
||||
continue;
|
||||
Enumeration<InetAddress> inet = intface.getInetAddresses();
|
||||
if (!inet.hasMoreElements())
|
||||
continue;
|
||||
String address = inet.nextElement().getHostAddress();
|
||||
return address;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void waitListening(AbstractInternetProtocolReceivingChannelAdapter adapter) throws Exception {
|
||||
int n = 0;
|
||||
while (!adapter.isListening()) {
|
||||
Thread.sleep(100);
|
||||
if (n++ > 100) {
|
||||
throw new Exception("Gateway failed to listen");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user