Skip to content

Commit 9b27fbe

Browse files
committed
Remove SocketUtils usage
* Remove usage of non-stable `org.springframework.util.SocketUtils` * Replace it with `0` for those tests where it is possible to select OS port * Remove the mentioning of the `SocketUtils` from the `testing.adoc` * Use `TransportConstants.DEFAULT_STOMP_PORT` for `StompServerIntegrationTests`. We may disable this test in the future for CI if it is not going to be stable * Introduce `Supplier<String> connectUrl` variants for `ZeroMqMessageHandler` to let it defer connection evaluation until subscription to the socket `Mono` in the `ZeroMqMessageHandler`. * Move connection logic in the `ZeroMqMessageHandler` to `Lifecycle.start()` Related to spring-projects/spring-framework#28054 **Cherry-pick to `5.5.x`** # Conflicts: # spring-integration-stomp/src/test/java/org/springframework/integration/stomp/client/StompServerIntegrationTests.java
1 parent f8c22dd commit 9b27fbe

File tree

12 files changed

+187
-148
lines changed

12 files changed

+187
-148
lines changed

spring-integration-ip/src/test/java/org/springframework/integration/ip/udp/MulticastRule.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2020 the original author or authors.
2+
* Copyright 2015-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,7 +29,6 @@
2929
import org.springframework.integration.ip.util.SocketTestUtils;
3030
import org.springframework.lang.Nullable;
3131
import org.springframework.util.Assert;
32-
import org.springframework.util.SocketUtils;
3332

3433
/**
3534
* @author Artem Bilan
@@ -76,7 +75,7 @@ private NetworkInterface checkMulticast() throws Exception {
7675
}
7776
try {
7877
MulticastSocket socket = new MulticastSocket();
79-
socket.joinGroup(new InetSocketAddress(this.group, SocketUtils.findAvailableUdpPort()), nic);
78+
socket.joinGroup(new InetSocketAddress(this.group, 0), nic);
8079
socket.close();
8180
}
8281
catch (Exception e) {

spring-integration-ip/src/test/java/org/springframework/integration/ip/udp/UdpChannelAdapterTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,7 +50,6 @@
5050
import org.springframework.integration.test.rule.Log4j2LevelAdjuster;
5151
import org.springframework.messaging.Message;
5252
import org.springframework.messaging.SubscribableChannel;
53-
import org.springframework.util.SocketUtils;
5453

5554
/**
5655
*
@@ -284,8 +283,7 @@ public void testMulticastReceiver() throws Exception {
284283
}
285284

286285
}
287-
DatagramSocket datagramSocket =
288-
new DatagramSocket(SocketUtils.findAvailableUdpPort(), inetAddress);
286+
DatagramSocket datagramSocket = new DatagramSocket(0, inetAddress);
289287
datagramSocket.send(packet);
290288
datagramSocket.close();
291289

spring-integration-stomp/src/test/java/org/springframework/integration/stomp/client/StompServerIntegrationTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2021 the original author or authors.
2+
* Copyright 2015-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,7 +54,6 @@
5454
import org.springframework.messaging.simp.stomp.StompCommand;
5555
import org.springframework.messaging.support.GenericMessage;
5656
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
57-
import org.springframework.util.SocketUtils;
5857

5958
/**
6059
* @author Artem Bilan
@@ -70,17 +69,16 @@ public class StompServerIntegrationTests {
7069

7170
@BeforeAll
7271
public static void setup() throws Exception {
73-
int port = SocketUtils.findAvailableTcpPort(61613);
7472
activeMQBroker = new BrokerService();
75-
activeMQBroker.addConnector("stomp://127.0.0.1:" + port);
73+
activeMQBroker.addConnector("stomp://127.0.0.1:" + BrokerService.DEFAULT_PORT);
7674
activeMQBroker.setPersistent(false);
7775
activeMQBroker.setUseJmx(false);
7876
activeMQBroker.setEnableStatistics(false);
7977
activeMQBroker.getSystemUsage().getMemoryUsage().setLimit(1024 * 1024 * 5);
8078
activeMQBroker.getSystemUsage().getTempUsage().setLimit(1024 * 1024 * 5);
8179
activeMQBroker.start();
8280

83-
stompClient = new ReactorNettyTcpStompClient("127.0.0.1", port);
81+
stompClient = new ReactorNettyTcpStompClient("127.0.0.1", Integer.parseInt(BrokerService.DEFAULT_PORT));
8482
stompClient.setMessageConverter(new PassThruMessageConverter());
8583
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
8684
taskScheduler.afterPropertiesSet();

spring-integration-syslog/src/test/java/org/springframework/integration/syslog/config/SyslogReceivingChannelAdapterParserTests-context.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
1010
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
1111

12-
<bean id="socketUtils" class="org.springframework.util.SocketUtils" />
13-
14-
<int-syslog:inbound-channel-adapter id="foo" port="#{socketUtils.findAvailableUdpPort(1514)}" />
12+
<int-syslog:inbound-channel-adapter id="foo" port="0" />
1513

1614
<int-syslog:inbound-channel-adapter id="foobar" channel="foo" port="1514" auto-startup="false" />
1715

@@ -42,7 +40,7 @@
4240
<bean id="converter"
4341
class="org.springframework.integration.syslog.config.SyslogReceivingChannelAdapterParserTests$PassThruConverter" />
4442

45-
<int-syslog:inbound-channel-adapter id="bar" protocol="tcp" port="#{socketUtils.findAvailableTcpPort(1514)}" />
43+
<int-syslog:inbound-channel-adapter id="bar" protocol="tcp" port="0" />
4644

4745
<int:channel id="bar">
4846
<int:queue/>

spring-integration-syslog/src/test/java/org/springframework/integration/syslog/config/SyslogReceivingChannelAdapterParserTests.java

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,17 +17,18 @@
1717
package org.springframework.integration.syslog.config;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.fail;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2121

22+
import java.lang.reflect.Method;
2223
import java.net.DatagramPacket;
2324
import java.net.DatagramSocket;
2425
import java.net.InetSocketAddress;
2526
import java.net.Socket;
27+
import java.nio.charset.StandardCharsets;
2628

2729
import javax.net.SocketFactory;
2830

29-
import org.junit.Test;
30-
import org.junit.runner.RunWith;
31+
import org.junit.jupiter.api.Test;
3132

3233
import org.springframework.beans.factory.BeanCreationException;
3334
import org.springframework.beans.factory.annotation.Autowired;
@@ -42,19 +43,21 @@
4243
import org.springframework.integration.test.util.TestUtils;
4344
import org.springframework.messaging.Message;
4445
import org.springframework.messaging.PollableChannel;
45-
import org.springframework.test.context.ContextConfiguration;
46-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
46+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
47+
import org.springframework.util.ReflectionUtils;
4748

4849
/**
4950
* @author Gary Russell
51+
* @author Artem Bilan
52+
*
5053
* @since 3.0
5154
*
5255
*/
53-
@ContextConfiguration
54-
@RunWith(SpringJUnit4ClassRunner.class)
56+
@SpringJUnitConfig
5557
public class SyslogReceivingChannelAdapterParserTests {
5658

57-
@Autowired @Qualifier("foo.adapter")
59+
@Autowired
60+
@Qualifier("foo.adapter")
5861
private UdpSyslogReceivingChannelAdapter adapter1;
5962

6063
@Autowired
@@ -63,7 +66,8 @@ public class SyslogReceivingChannelAdapterParserTests {
6366
@Autowired
6467
private PollableChannel foo;
6568

66-
@Autowired @Qualifier("explicitUdp.adapter")
69+
@Autowired
70+
@Qualifier("explicitUdp.adapter")
6771
private UdpSyslogReceivingChannelAdapter explicitUdpAdapter;
6872

6973
@Autowired
@@ -81,7 +85,8 @@ public class SyslogReceivingChannelAdapterParserTests {
8185
@Autowired
8286
private RFC5424MessageConverter rfc5424;
8387

84-
@Autowired @Qualifier("bar.adapter")
88+
@Autowired
89+
@Qualifier("bar.adapter")
8590
private TcpSyslogReceivingChannelAdapter adapter2;
8691

8792
@Autowired
@@ -95,8 +100,10 @@ public class SyslogReceivingChannelAdapterParserTests {
95100

96101
@Test
97102
public void testSimplestUdp() throws Exception {
98-
int port = TestUtils.getPropertyValue(adapter1, "udpAdapter.port", Integer.class);
99-
byte[] buf = "<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE".getBytes("UTF-8");
103+
Method getPort = ReflectionUtils.findMethod(UdpSyslogReceivingChannelAdapter.class, "getPort");
104+
ReflectionUtils.makeAccessible(getPort);
105+
int port = (int) ReflectionUtils.invokeMethod(getPort, this.adapter1);
106+
byte[] buf = "<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE".getBytes(StandardCharsets.UTF_8);
100107
DatagramPacket packet = new DatagramPacket(buf, buf.length, new InetSocketAddress("localhost", port));
101108
DatagramSocket socket = new DatagramSocket();
102109
Thread.sleep(1000);
@@ -108,13 +115,13 @@ public void testSimplestUdp() throws Exception {
108115
}
109116

110117
@Test
111-
public void testExplicitChannelUdp() throws Exception {
118+
public void testExplicitChannelUdp() {
112119
assertThat(TestUtils.getPropertyValue(foobar, "udpAdapter.port")).isEqualTo(1514);
113120
assertThat(TestUtils.getPropertyValue(foobar, "outputChannel")).isSameAs(foo);
114121
}
115122

116123
@Test
117-
public void testExplicitUdp() throws Exception {
124+
public void testExplicitUdp() {
118125
assertThat(TestUtils.getPropertyValue(explicitUdpAdapter, "outputChannel")).isSameAs(explicitUdp);
119126
}
120127

@@ -133,9 +140,9 @@ public void testFullBoatUdp() {
133140
public void testSimplestTcp() throws Exception {
134141
AbstractServerConnectionFactory connectionFactory = TestUtils.getPropertyValue(adapter2, "connectionFactory",
135142
AbstractServerConnectionFactory.class);
136-
int port = connectionFactory.getPort();
137143
waitListening(connectionFactory, 10000L);
138-
byte[] buf = "<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE\n".getBytes("UTF-8");
144+
byte[] buf = "<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE\n".getBytes(StandardCharsets.UTF_8);
145+
int port = connectionFactory.getPort();
139146
Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
140147
Thread.sleep(1000);
141148
socket.getOutputStream().write(buf);
@@ -159,57 +166,40 @@ public void testFullBoatTcp() {
159166

160167
@Test
161168
public void testPortOnUdpChild() {
162-
try {
163-
new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-fail1-context.xml", this.getClass())
164-
.close();
165-
fail("Expected exception");
166-
}
167-
catch (BeanDefinitionParsingException e) {
168-
assertThat(e.getMessage().startsWith(
169-
"Configuration problem: When child element 'udp-attributes' is present, 'port' must be defined " +
170-
"there"))
171-
.isTrue();
172-
}
169+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
170+
.isThrownBy(() ->
171+
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-fail1-context.xml",
172+
getClass()))
173+
.withMessageStartingWith(
174+
"Configuration problem: " +
175+
"When child element 'udp-attributes' is present, 'port' must be defined there");
173176
}
174177

175178
@Test
176179
public void testPortWithTCPFactory() {
177-
try {
178-
new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-fail2-context.xml", this.getClass())
179-
.close();
180-
fail("Expected exception");
181-
}
182-
catch (BeanCreationException e) {
183-
assertThat(e.getCause().getMessage()).isEqualTo("Cannot specify both 'port' and 'connectionFactory'");
184-
}
180+
assertThatExceptionOfType(BeanCreationException.class)
181+
.isThrownBy(() ->
182+
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-fail2-context.xml",
183+
getClass()))
184+
.withStackTraceContaining("Cannot specify both 'port' and 'connectionFactory'");
185185
}
186186

187187
@Test
188188
public void testUdpChildWithTcp() {
189-
try {
190-
new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-fail3-context.xml", this.getClass())
191-
.close();
192-
fail("Expected exception");
193-
}
194-
catch (BeanCreationException e) {
195-
e.printStackTrace();
196-
197-
assertThat(e.getCause().getMessage())
198-
.isEqualTo("Cannot specify 'udp-attributes' when the protocol is 'tcp'");
199-
}
189+
assertThatExceptionOfType(BeanCreationException.class)
190+
.isThrownBy(() ->
191+
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-fail3-context.xml",
192+
getClass()))
193+
.withStackTraceContaining("Cannot specify 'udp-attributes' when the protocol is 'tcp'");
200194
}
201195

202196
@Test
203197
public void testUDPWithTCPFactory() {
204-
try {
205-
new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-fail4-context.xml", this.getClass())
206-
.close();
207-
fail("Expected exception");
208-
}
209-
catch (BeanCreationException e) {
210-
assertThat(e.getCause().getMessage())
211-
.isEqualTo("Cannot specify 'connection-factory' unless the protocol is 'tcp'");
212-
}
198+
assertThatExceptionOfType(BeanCreationException.class)
199+
.isThrownBy(() ->
200+
new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-fail4-context.xml",
201+
getClass()))
202+
.withStackTraceContaining("Cannot specify 'connection-factory' unless the protocol is 'tcp'");
213203
}
214204

215205
public static class PassThruConverter implements MessageConverter {
@@ -229,7 +219,7 @@ public Message<?> fromSyslog(Message<?> syslog) {
229219
* @throws IllegalStateException
230220
*/
231221
private void waitListening(AbstractServerConnectionFactory serverConnectionFactory, Long delay)
232-
throws IllegalStateException {
222+
throws IllegalStateException {
233223
if (delay == null) {
234224
delay = 100L;
235225
}

spring-integration-zeromq/src/main/java/org/springframework/integration/zeromq/ZeroMqProxy.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -212,7 +212,7 @@ public int getBackendPort() {
212212

213213
/**
214214
* Return the address an {@code inproc} control socket is bound or null if this proxy has not been started yet.
215-
* @return the the address for control socket or null
215+
* @return the address for control socket or null
216216
*/
217217
@Nullable
218218
public String getControlAddress() {
@@ -222,7 +222,7 @@ public String getControlAddress() {
222222
/**
223223
* Return the address an {@code inproc} capture socket is bound or null if this proxy has not been started yet
224224
* or {@link #captureAddress} is false.
225-
* @return the the address for capture socket or null
225+
* @return the address for capture socket or null
226226
*/
227227
@Nullable
228228
public String getCaptureAddress() {

spring-integration-zeromq/src/main/java/org/springframework/integration/zeromq/dsl/ZeroMq.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 the original author or authors.
2+
* Copyright 2020-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.integration.zeromq.dsl;
1818

19+
import java.util.function.Supplier;
20+
1921
import org.zeromq.SocketType;
2022
import org.zeromq.ZContext;
2123

@@ -53,6 +55,18 @@ public static ZeroMqChannelSpec pubSubZeroMqChannel(ZContext context) {
5355
* @return the spec.
5456
*/
5557
public static ZeroMqMessageHandlerSpec outboundChannelAdapter(ZContext context, String connectUrl) {
58+
return outboundChannelAdapter(context, () -> connectUrl);
59+
}
60+
61+
/**
62+
* Create an instance of {@link ZeroMqMessageHandlerSpec} for the provided {@link ZContext}
63+
* and connection URL supplier.
64+
* @param context the {@link ZContext} to use.
65+
* @param connectUrl the supplier for URL to connect a ZeroMq socket to.
66+
* @return the spec.
67+
* @since 5.5.9
68+
*/
69+
public static ZeroMqMessageHandlerSpec outboundChannelAdapter(ZContext context, Supplier<String> connectUrl) {
5670
return new ZeroMqMessageHandlerSpec(context, connectUrl);
5771
}
5872

@@ -70,6 +84,21 @@ public static ZeroMqMessageHandlerSpec outboundChannelAdapter(ZContext context,
7084
return new ZeroMqMessageHandlerSpec(context, connectUrl, socketType);
7185
}
7286

87+
/**
88+
* Create an instance of {@link ZeroMqMessageHandlerSpec} for the provided {@link ZContext},
89+
* connection URL supplier and {@link SocketType}.
90+
* @param context the {@link ZContext} to use.
91+
* @param connectUrl the supplier for URL to connect a ZeroMq socket to.
92+
* @param socketType the {@link SocketType} for ZeroMq socket.
93+
* @return the spec.
94+
* @since 5.5.9
95+
*/
96+
public static ZeroMqMessageHandlerSpec outboundChannelAdapter(ZContext context, Supplier<String> connectUrl,
97+
SocketType socketType) {
98+
99+
return new ZeroMqMessageHandlerSpec(context, connectUrl, socketType);
100+
}
101+
73102
/**
74103
* Create an instance of {@link ZeroMqMessageProducerSpec} for the provided {@link ZContext}.
75104
* @param context the {@link ZContext} to use.

0 commit comments

Comments
 (0)