4343import java .util .Set ;
4444import java .util .concurrent .CountDownLatch ;
4545import java .util .concurrent .ExecutionException ;
46- import java .util .concurrent .Executors ;
4746import java .util .concurrent .Future ;
4847import java .util .concurrent .TimeUnit ;
4948import java .util .concurrent .atomic .AtomicBoolean ;
6160import org .springframework .beans .factory .BeanFactory ;
6261import org .springframework .core .serializer .DefaultDeserializer ;
6362import org .springframework .core .serializer .DefaultSerializer ;
63+ import org .springframework .core .task .AsyncTaskExecutor ;
64+ import org .springframework .core .task .SimpleAsyncTaskExecutor ;
6465import org .springframework .expression .EvaluationContext ;
6566import org .springframework .expression .Expression ;
6667import org .springframework .expression .spel .standard .SpelExpressionParser ;
@@ -90,6 +91,8 @@ public class TcpOutboundGatewayTests {
9091
9192 private static final Log logger = LogFactory .getLog (TcpOutboundGatewayTests .class );
9293
94+ private AsyncTaskExecutor executor = new SimpleAsyncTaskExecutor ();
95+
9396 @ ClassRule
9497 public static LongRunningIntegrationTest longTests = new LongRunningIntegrationTest ();
9598
@@ -101,13 +104,13 @@ public class TcpOutboundGatewayTests {
101104 public void testGoodNetSingle () throws Exception {
102105 final CountDownLatch latch = new CountDownLatch (1 );
103106 final AtomicBoolean done = new AtomicBoolean ();
104- final AtomicReference <ServerSocket > serverSocket = new AtomicReference <ServerSocket >();
105- Executors . newSingleThreadExecutor () .execute (() -> {
107+ final AtomicReference <ServerSocket > serverSocket = new AtomicReference <>();
108+ this . executor .execute (() -> {
106109 try {
107110 ServerSocket server = ServerSocketFactory .getDefault ().createServerSocket (0 , 100 );
108111 serverSocket .set (server );
109112 latch .countDown ();
110- List <Socket > sockets = new ArrayList <Socket >();
113+ List <Socket > sockets = new ArrayList <>();
111114 int i = 0 ;
112115 while (true ) {
113116 Socket socket = server .accept ();
@@ -158,15 +161,16 @@ public void testGoodNetSingle() throws Exception {
158161 assertTrue (replies .remove ("Reply" + i ));
159162 }
160163 done .set (true );
164+ ccf .stop ();
161165 serverSocket .get ().close ();
162166 }
163167
164168 @ Test
165169 public void testGoodNetMultiplex () throws Exception {
166170 final CountDownLatch latch = new CountDownLatch (1 );
167171 final AtomicBoolean done = new AtomicBoolean ();
168- final AtomicReference <ServerSocket > serverSocket = new AtomicReference <ServerSocket >();
169- Executors . newSingleThreadExecutor () .execute (() -> {
172+ final AtomicReference <ServerSocket > serverSocket = new AtomicReference <>();
173+ this . executor .execute (() -> {
170174 try {
171175 ServerSocket server = ServerSocketFactory .getDefault ().createServerSocket (0 , 10 );
172176 serverSocket .set (server );
@@ -213,15 +217,16 @@ public void testGoodNetMultiplex() throws Exception {
213217 }
214218 done .set (true );
215219 gateway .stop ();
220+ ccf .stop ();
216221 serverSocket .get ().close ();
217222 }
218223
219224 @ Test
220225 public void testGoodNetTimeout () throws Exception {
221226 final CountDownLatch latch = new CountDownLatch (1 );
222227 final AtomicBoolean done = new AtomicBoolean ();
223- final AtomicReference <ServerSocket > serverSocket = new AtomicReference <ServerSocket >();
224- Executors . newSingleThreadExecutor () .execute (() -> {
228+ final AtomicReference <ServerSocket > serverSocket = new AtomicReference <>();
229+ this . executor .execute (() -> {
225230 try {
226231 ServerSocket server = ServerSocketFactory .getDefault ().createServerSocket (0 );
227232 serverSocket .set (server );
@@ -260,12 +265,12 @@ public void testGoodNetTimeout() throws Exception {
260265 Future <Integer >[] results = (Future <Integer >[]) new Future <?>[2 ];
261266 for (int i = 0 ; i < 2 ; i ++) {
262267 final int j = i ;
263- results [j ] = (Executors . newSingleThreadExecutor () .submit (() -> {
268+ results [j ] = (this . executor .submit (() -> {
264269 gateway .handleMessage (MessageBuilder .withPayload ("Test" + j ).build ());
265270 return 0 ;
266271 }));
267272 }
268- Set <String > replies = new HashSet <String >();
273+ Set <String > replies = new HashSet <>();
269274 int timeouts = 0 ;
270275 for (int i = 0 ; i < 2 ; i ++) {
271276 try {
@@ -294,6 +299,7 @@ public void testGoodNetTimeout() throws Exception {
294299 }
295300 done .set (true );
296301 gateway .stop ();
302+ ccf .stop ();
297303 serverSocket .get ().close ();
298304 }
299305
@@ -344,7 +350,7 @@ private void testGoodNetGWTimeoutGuts(final int port, AbstractClientConnectionFa
344350 final AtomicReference <String > lastReceived = new AtomicReference <String >();
345351 final CountDownLatch serverLatch = new CountDownLatch (2 );
346352
347- Executors . newSingleThreadExecutor () .execute (() -> {
353+ this . executor .execute (() -> {
348354 try {
349355 latch .countDown ();
350356 int i = 0 ;
@@ -398,7 +404,7 @@ private void testGoodNetGWTimeoutGuts(final int port, AbstractClientConnectionFa
398404
399405 for (int i = 0 ; i < 2 ; i ++) {
400406 final int j = i ;
401- results [j ] = (Executors . newSingleThreadExecutor () .submit (() -> {
407+ results [j ] = (this . executor .submit (() -> {
402408 gateway .handleMessage (MessageBuilder .withPayload ("Test" + j ).build ());
403409 return j ;
404410 }));
@@ -433,6 +439,7 @@ private void testGoodNetGWTimeoutGuts(final int port, AbstractClientConnectionFa
433439 done .set (true );
434440 assertEquals (0 , TestUtils .getPropertyValue (gateway , "pendingReplies" , Map .class ).size ());
435441 gateway .stop ();
442+ ccf .stop ();
436443 }
437444
438445 @ Test
@@ -442,7 +449,7 @@ public void testCachingFailover() throws Exception {
442449 final AtomicBoolean done = new AtomicBoolean ();
443450 final CountDownLatch serverLatch = new CountDownLatch (1 );
444451
445- Executors . newSingleThreadExecutor () .execute (() -> {
452+ this . executor .execute (() -> {
446453 try {
447454 ServerSocket server = ServerSocketFactory .getDefault ().createServerSocket (0 );
448455 serverSocket .set (server );
@@ -512,17 +519,18 @@ public void testCachingFailover() throws Exception {
512519 done .set (true );
513520 gateway .stop ();
514521 verify (mockConn1 ).send (Mockito .any (Message .class ));
522+ factory2 .stop ();
515523 serverSocket .get ().close ();
516524 }
517525
518526 @ Test
519527 public void testFailoverCached () throws Exception {
520- final AtomicReference <ServerSocket > serverSocket = new AtomicReference <ServerSocket >();
528+ final AtomicReference <ServerSocket > serverSocket = new AtomicReference <>();
521529 final CountDownLatch latch = new CountDownLatch (1 );
522530 final AtomicBoolean done = new AtomicBoolean ();
523531 final CountDownLatch serverLatch = new CountDownLatch (1 );
524532
525- Executors . newSingleThreadExecutor () .execute (() -> {
533+ this . executor .execute (() -> {
526534 try {
527535 ServerSocket server = ServerSocketFactory .getDefault ().createServerSocket (0 );
528536 serverSocket .set (server );
@@ -595,6 +603,7 @@ public void testFailoverCached() throws Exception {
595603 done .set (true );
596604 gateway .stop ();
597605 verify (mockConn1 ).send (Mockito .any (Message .class ));
606+ factory2 .stop ();
598607 serverSocket .get ().close ();
599608 }
600609
@@ -667,11 +676,11 @@ private void testGWPropagatesSocketCloseGuts(final int port, AbstractClientConne
667676 final ServerSocket server ) throws Exception {
668677 final CountDownLatch latch = new CountDownLatch (1 );
669678 final AtomicBoolean done = new AtomicBoolean ();
670- final AtomicReference <String > lastReceived = new AtomicReference <String >();
679+ final AtomicReference <String > lastReceived = new AtomicReference <>();
671680 final CountDownLatch serverLatch = new CountDownLatch (1 );
672681
673- Executors . newSingleThreadExecutor () .execute (() -> {
674- List <Socket > sockets = new ArrayList <Socket >();
682+ this . executor .execute (() -> {
683+ List <Socket > sockets = new ArrayList <>();
675684 try {
676685 latch .countDown ();
677686 while (!done .get ()) {
@@ -793,8 +802,8 @@ private void testGWPropagatesSocketTimeoutGuts(final int port, AbstractClientCon
793802 final CountDownLatch latch = new CountDownLatch (1 );
794803 final AtomicBoolean done = new AtomicBoolean ();
795804
796- Executors . newSingleThreadExecutor () .execute (() -> {
797- List <Socket > sockets = new ArrayList <Socket >();
805+ this . executor .execute (() -> {
806+ List <Socket > sockets = new ArrayList <>();
798807 try {
799808 latch .countDown ();
800809 while (!done .get ()) {
0 commit comments