GH-3410: Add UDP SocketCustomizer

Resolves https://github.com/spring-projects/spring-integration/issues/3410

**cherry-pick to 5.3.x**

* Doc polishing.
This commit is contained in:
Gary Russell
2020-10-22 13:32:56 -04:00
committed by GitHub
parent b94c20a0ce
commit a79be71ed2
15 changed files with 192 additions and 45 deletions

View File

@@ -19,6 +19,8 @@
<task:executor id="externalTE" pool-size="10"/>
<bean id="udpCust" class = "org.springframework.integration.ip.config.ParserUnitTests$SocketCust"/>
<ip:udp-inbound-channel-adapter id="testInUdp"
channel="udpChannel"
check-length="true"
@@ -35,6 +37,7 @@
lookup-host="false"
auto-startup="false"
phase="1234"
socket-customizer="udpCust"
/>
<ip:udp-inbound-channel-adapter id="testInUdpMulticast"
@@ -141,6 +144,7 @@
local-address="127.0.0.1"
task-executor="externalTE"
order="23"
socket-customizer="udpCust"
/>
<ip:udp-outbound-channel-adapter id="testOutUdpiMulticast"

View File

@@ -20,6 +20,8 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.time.Duration;
import java.util.Iterator;
import java.util.Set;
@@ -66,6 +68,7 @@ import org.springframework.integration.ip.tcp.connection.TcpSocketSupport;
import org.springframework.integration.ip.udp.DatagramPacketMessageMapper;
import org.springframework.integration.ip.udp.MulticastReceivingChannelAdapter;
import org.springframework.integration.ip.udp.MulticastSendingMessageHandler;
import org.springframework.integration.ip.udp.SocketCustomizer;
import org.springframework.integration.ip.udp.UnicastReceivingChannelAdapter;
import org.springframework.integration.ip.udp.UnicastSendingMessageHandler;
import org.springframework.integration.test.util.TestUtils;
@@ -273,6 +276,9 @@ public class ParserUnitTests {
@Autowired
TcpMessageMapper mapper;
@Autowired
SocketCust socketCustomizer;
private static CountDownLatch adviceCalled = new CountDownLatch(1);
@Test
@@ -293,6 +299,7 @@ public class ParserUnitTests {
assertThat((Boolean) mapperAccessor.getPropertyValue("lookupHost")).isFalse();
assertThat(TestUtils.getPropertyValue(udpIn, "autoStartup", Boolean.class)).isFalse();
assertThat(dfa.getPropertyValue("phase")).isEqualTo(1234);
assertThat(dfa.getPropertyValue("socketCustomizer")).isSameAs(this.socketCustomizer);
}
@Test
@@ -365,6 +372,7 @@ public class ParserUnitTests {
assertThat(dfa.getPropertyValue("order")).isEqualTo(23);
assertThat(udpOut.getComponentName()).isEqualTo("testOutUdp");
assertThat(udpOut.getComponentType()).isEqualTo("ip:udp-outbound-channel-adapter");
assertThat(dfa.getPropertyValue("socketCustomizer")).isSameAs(this.socketCustomizer);
}
@Test
@@ -697,4 +705,12 @@ public class ParserUnitTests {
}
public static class SocketCust implements SocketCustomizer {
@Override
public void configure(DatagramSocket socket) throws SocketException {
}
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.integration.ip.dsl;
import static org.assertj.core.api.Assertions.assertThat;
import java.net.DatagramSocket;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -56,6 +57,7 @@ import org.springframework.integration.ip.tcp.serializer.TcpCodecs;
import org.springframework.integration.ip.udp.MulticastSendingMessageHandler;
import org.springframework.integration.ip.udp.UdpServerListeningEvent;
import org.springframework.integration.ip.udp.UnicastReceivingChannelAdapter;
import org.springframework.integration.ip.udp.UnicastSendingMessageHandler;
import org.springframework.integration.ip.util.TestingUtilities;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.util.TestUtils;
@@ -100,6 +102,9 @@ public class IpIntegrationTests {
@Autowired
private UnicastReceivingChannelAdapter udpInbound;
@Autowired
private UnicastSendingMessageHandler udpOutbound;
@Autowired
private QueueChannel udpIn;
@@ -176,6 +181,8 @@ public class IpIntegrationTests {
Message<?> received = this.udpIn.receive(10000);
assertThat(received).isNotNull();
assertThat(Transformers.objectToString().transform(received).getPayload()).isEqualTo("foo");
assertThat(TestUtils.getPropertyValue(this.udpOutbound, "socket", DatagramSocket.class).getTrafficClass())
.isEqualTo(0x10);
}
@Test
@@ -303,7 +310,8 @@ public class IpIntegrationTests {
@Bean
public IntegrationFlow outUdpAdapter() {
return f -> f.handle(Udp.outboundAdapter(m -> m.getHeaders().get("udp_dest")));
return f -> f.handle(Udp.outboundAdapter(m -> m.getHeaders().get("udp_dest"))
.configureSocket(socket -> socket.setTrafficClass(0x10)));
}
@Bean