1
1
/*
2
- * Copyright 2002-2024 the original author or authors.
2
+ * Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
25
25
import java .nio .channels .SocketChannel ;
26
26
import java .util .ArrayList ;
27
27
import java .util .List ;
28
+ import java .util .concurrent .CompletableFuture ;
28
29
import java .util .concurrent .CountDownLatch ;
29
30
import java .util .concurrent .Executor ;
30
31
import java .util .concurrent .TimeUnit ;
39
40
import org .mockito .Mockito ;
40
41
41
42
import org .springframework .beans .factory .BeanFactory ;
42
- import org .springframework .context .ApplicationEvent ;
43
- import org .springframework .context .ApplicationEventPublisher ;
44
43
import org .springframework .core .task .SimpleAsyncTaskExecutor ;
45
44
import org .springframework .integration .channel .DirectChannel ;
46
45
import org .springframework .integration .channel .QueueChannel ;
79
78
*/
80
79
public class FailoverClientConnectionFactoryTests {
81
80
82
- private static final ApplicationEventPublisher NULL_PUBLISHER = new ApplicationEventPublisher () {
83
-
84
- @ Override
85
- public void publishEvent (ApplicationEvent event ) {
86
- }
87
-
88
- @ Override
89
- public void publishEvent (Object event ) {
90
-
91
- }
92
-
93
- };
94
-
95
81
@ Test
96
82
public void testFailoverGood () throws Exception {
97
83
TcpConnectionSupport conn1 = makeMockConnection ();
98
84
TcpConnectionSupport conn2 = makeMockConnection ();
99
85
AbstractClientConnectionFactory factory1 = createFactoryWithMockConnection (conn1 );
100
86
AbstractClientConnectionFactory factory2 = createFactoryWithMockConnection (conn2 );
101
- List <AbstractClientConnectionFactory > factories = new ArrayList <AbstractClientConnectionFactory >();
87
+ List <AbstractClientConnectionFactory > factories = new ArrayList <>();
102
88
factories .add (factory1 );
103
89
factories .add (factory2 );
104
90
doThrow (new UncheckedIOException (new IOException ("fail" )))
105
91
.when (conn1 ).send (Mockito .any (Message .class ));
106
92
doAnswer (invocation -> null ).when (conn2 ).send (Mockito .any (Message .class ));
107
93
FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory (factories );
108
94
failoverFactory .start ();
109
- GenericMessage <String > message = new GenericMessage <String >("foo" );
95
+ GenericMessage <String > message = new GenericMessage <>("foo" );
110
96
failoverFactory .getConnection ().send (message );
111
97
Mockito .verify (conn2 ).send (message );
112
98
}
@@ -129,7 +115,7 @@ public void testRefreshSharedInfinite() throws Exception {
129
115
private void testRefreshShared (boolean closeOnRefresh , long interval ) throws Exception {
130
116
AbstractClientConnectionFactory factory1 = mock (AbstractClientConnectionFactory .class );
131
117
AbstractClientConnectionFactory factory2 = mock (AbstractClientConnectionFactory .class );
132
- List <AbstractClientConnectionFactory > factories = new ArrayList <AbstractClientConnectionFactory >();
118
+ List <AbstractClientConnectionFactory > factories = new ArrayList <>();
133
119
factories .add (factory1 );
134
120
factories .add (factory2 );
135
121
TcpConnectionSupport conn1 = makeMockConnection ();
@@ -182,7 +168,7 @@ public void testFailoverAllDead() throws Exception {
182
168
TcpConnectionSupport conn2 = makeMockConnection ();
183
169
AbstractClientConnectionFactory factory1 = createFactoryWithMockConnection (conn1 );
184
170
AbstractClientConnectionFactory factory2 = createFactoryWithMockConnection (conn2 );
185
- List <AbstractClientConnectionFactory > factories = new ArrayList <AbstractClientConnectionFactory >();
171
+ List <AbstractClientConnectionFactory > factories = new ArrayList <>();
186
172
factories .add (factory1 );
187
173
factories .add (factory2 );
188
174
doThrow (new UncheckedIOException (new IOException ("fail" )))
@@ -191,7 +177,7 @@ public void testFailoverAllDead() throws Exception {
191
177
.when (conn2 ).send (Mockito .any (Message .class ));
192
178
FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory (factories );
193
179
failoverFactory .start ();
194
- GenericMessage <String > message = new GenericMessage <String >("foo" );
180
+ GenericMessage <String > message = new GenericMessage <>("foo" );
195
181
assertThatExceptionOfType (UncheckedIOException .class ).isThrownBy (() ->
196
182
failoverFactory .getConnection ().send (message ));
197
183
Mockito .verify (conn2 ).send (message );
@@ -214,7 +200,7 @@ void failoverAllDeadAfterSuccess() throws Exception {
214
200
TcpNetClientConnectionFactory cf1 = new TcpNetClientConnectionFactory ("localhost" , ss1 .getLocalPort ());
215
201
AbstractClientConnectionFactory cf2 = mock (AbstractClientConnectionFactory .class );
216
202
doThrow (new UncheckedIOException (new IOException ("fail" ))).when (cf2 ).getConnection ();
217
- CountDownLatch latch = new CountDownLatch (2 );
203
+ CountDownLatch latch = new CountDownLatch (1 );
218
204
cf1 .setApplicationEventPublisher (event -> {
219
205
if (event instanceof TcpConnectionCloseEvent ) {
220
206
latch .countDown ();
@@ -223,12 +209,16 @@ void failoverAllDeadAfterSuccess() throws Exception {
223
209
cf2 .setApplicationEventPublisher (event -> {
224
210
});
225
211
FailoverClientConnectionFactory fccf = new FailoverClientConnectionFactory (List .of (cf1 , cf2 ));
226
- fccf . registerListener ( msf -> {
227
- latch . countDown ();
228
- return false ;
229
- });
212
+
213
+ CompletableFuture < Message <?>> messageCompletableFuture = new CompletableFuture <> ();
214
+ fccf . registerListener ( messageCompletableFuture :: complete ) ;
215
+
230
216
fccf .start ();
231
217
fccf .getConnection ().send (new GenericMessage <>("test" ));
218
+ assertThat (messageCompletableFuture )
219
+ .succeedsWithin (10 , TimeUnit .SECONDS )
220
+ .extracting (Message ::getPayload )
221
+ .isEqualTo ("ok" .getBytes ());
232
222
assertThat (latch .await (10 , TimeUnit .SECONDS )).isTrue ();
233
223
assertThatExceptionOfType (UncheckedIOException .class ).isThrownBy (() ->
234
224
fccf .getConnection ().send (new GenericMessage <>("test" )));
@@ -240,7 +230,7 @@ public void testFailoverAllDeadButOriginalOkAgain() throws Exception {
240
230
TcpConnectionSupport conn2 = makeMockConnection ();
241
231
AbstractClientConnectionFactory factory1 = createFactoryWithMockConnection (conn1 );
242
232
AbstractClientConnectionFactory factory2 = createFactoryWithMockConnection (conn2 );
243
- List <AbstractClientConnectionFactory > factories = new ArrayList <AbstractClientConnectionFactory >();
233
+ List <AbstractClientConnectionFactory > factories = new ArrayList <>();
244
234
factories .add (factory1 );
245
235
factories .add (factory2 );
246
236
final AtomicBoolean failedOnce = new AtomicBoolean ();
@@ -255,7 +245,7 @@ public void testFailoverAllDeadButOriginalOkAgain() throws Exception {
255
245
.when (conn2 ).send (Mockito .any (Message .class ));
256
246
FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory (factories );
257
247
failoverFactory .start ();
258
- GenericMessage <String > message = new GenericMessage <String >("foo" );
248
+ GenericMessage <String > message = new GenericMessage <>("foo" );
259
249
failoverFactory .getConnection ().send (message );
260
250
Mockito .verify (conn2 ).send (message );
261
251
Mockito .verify (conn1 , times (2 )).send (message );
@@ -265,7 +255,7 @@ public void testFailoverAllDeadButOriginalOkAgain() throws Exception {
265
255
public void testFailoverConnectNone () throws Exception {
266
256
AbstractClientConnectionFactory factory1 = mock (AbstractClientConnectionFactory .class );
267
257
AbstractClientConnectionFactory factory2 = mock (AbstractClientConnectionFactory .class );
268
- List <AbstractClientConnectionFactory > factories = new ArrayList <AbstractClientConnectionFactory >();
258
+ List <AbstractClientConnectionFactory > factories = new ArrayList <>();
269
259
factories .add (factory1 );
270
260
factories .add (factory2 );
271
261
when (factory1 .getConnection ()).thenThrow (new UncheckedIOException (new IOException ("fail" )));
@@ -274,7 +264,7 @@ public void testFailoverConnectNone() throws Exception {
274
264
when (factory2 .isActive ()).thenReturn (true );
275
265
FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory (factories );
276
266
failoverFactory .start ();
277
- GenericMessage <String > message = new GenericMessage <String >("foo" );
267
+ GenericMessage <String > message = new GenericMessage <>("foo" );
278
268
assertThatExceptionOfType (UncheckedIOException .class ).isThrownBy (() ->
279
269
failoverFactory .getConnection ().send (message ));
280
270
}
@@ -283,7 +273,7 @@ public void testFailoverConnectNone() throws Exception {
283
273
public void testFailoverConnectToFirstAfterTriedAll () throws Exception {
284
274
AbstractClientConnectionFactory factory1 = mock (AbstractClientConnectionFactory .class );
285
275
AbstractClientConnectionFactory factory2 = mock (AbstractClientConnectionFactory .class );
286
- List <AbstractClientConnectionFactory > factories = new ArrayList <AbstractClientConnectionFactory >();
276
+ List <AbstractClientConnectionFactory > factories = new ArrayList <>();
287
277
factories .add (factory1 );
288
278
factories .add (factory2 );
289
279
TcpConnectionSupport conn1 = makeMockConnection ();
@@ -308,7 +298,7 @@ public void testOkAgainAfterCompleteFailure() throws Exception {
308
298
TcpConnectionSupport conn2 = makeMockConnection ();
309
299
AbstractClientConnectionFactory factory1 = createFactoryWithMockConnection (conn1 );
310
300
AbstractClientConnectionFactory factory2 = createFactoryWithMockConnection (conn2 );
311
- List <AbstractClientConnectionFactory > factories = new ArrayList <AbstractClientConnectionFactory >();
301
+ List <AbstractClientConnectionFactory > factories = new ArrayList <>();
312
302
factories .add (factory1 );
313
303
factories .add (factory2 );
314
304
final AtomicInteger failCount = new AtomicInteger ();
@@ -322,7 +312,7 @@ public void testOkAgainAfterCompleteFailure() throws Exception {
322
312
.when (conn2 ).send (Mockito .any (Message .class ));
323
313
FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory (factories );
324
314
failoverFactory .start ();
325
- GenericMessage <String > message = new GenericMessage <String >("foo" );
315
+ GenericMessage <String > message = new GenericMessage <>("foo" );
326
316
assertThatExceptionOfType (UncheckedIOException .class )
327
317
.isThrownBy (() -> failoverFactory .getConnection ().send (message ));
328
318
failoverFactory .getConnection ().send (message );
@@ -426,27 +416,27 @@ public void testFailoverCachedRealClose() throws Exception {
426
416
cachingFactory2 .setBeanName ("cache2" );
427
417
428
418
// Failover
429
- List <AbstractClientConnectionFactory > factories = new ArrayList <AbstractClientConnectionFactory >();
419
+ List <AbstractClientConnectionFactory > factories = new ArrayList <>();
430
420
factories .add (cachingFactory1 );
431
421
factories .add (cachingFactory2 );
432
422
FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory (factories );
433
423
434
424
failoverFactory .start ();
435
425
TcpConnection conn1 = failoverFactory .getConnection ();
436
- conn1 .send (new GenericMessage <String >("foo1" ));
426
+ conn1 .send (new GenericMessage <>("foo1" ));
437
427
conn1 .close ();
438
428
TcpConnection conn2 = failoverFactory .getConnection ();
439
429
assertThat ((TestUtils .getPropertyValue (conn2 , "delegate" , TcpConnectionInterceptorSupport .class ))
440
430
.getTheConnection ())
441
431
.isSameAs ((TestUtils .getPropertyValue (conn1 , "delegate" , TcpConnectionInterceptorSupport .class ))
442
432
.getTheConnection ());
443
- conn2 .send (new GenericMessage <String >("foo2" ));
433
+ conn2 .send (new GenericMessage <>("foo2" ));
444
434
conn1 = failoverFactory .getConnection ();
445
435
assertThat ((TestUtils .getPropertyValue (conn2 , "delegate" , TcpConnectionInterceptorSupport .class ))
446
436
.getTheConnection ())
447
437
.isNotSameAs ((TestUtils .getPropertyValue (conn1 , "delegate" , TcpConnectionInterceptorSupport .class ))
448
438
.getTheConnection ());
449
- conn1 .send (new GenericMessage <String >("foo3" ));
439
+ conn1 .send (new GenericMessage <>("foo3" ));
450
440
conn1 .close ();
451
441
conn2 .close ();
452
442
assertThat (latch1 .await (10 , TimeUnit .SECONDS )).isTrue ();
@@ -455,8 +445,8 @@ public void testFailoverCachedRealClose() throws Exception {
455
445
TestingUtilities .waitUntilFactoryHasThisNumberOfConnections (factory1 , 0 );
456
446
conn1 = failoverFactory .getConnection ();
457
447
conn2 = failoverFactory .getConnection ();
458
- conn1 .send (new GenericMessage <String >("foo4" ));
459
- conn2 .send (new GenericMessage <String >("foo5" ));
448
+ conn1 .send (new GenericMessage <>("foo4" ));
449
+ conn2 .send (new GenericMessage <>("foo5" ));
460
450
conn1 .close ();
461
451
conn2 .close ();
462
452
assertThat (latch2 .await (10 , TimeUnit .SECONDS )).isTrue ();
@@ -467,7 +457,7 @@ public void testFailoverCachedRealClose() throws Exception {
467
457
468
458
@ SuppressWarnings ("unchecked" )
469
459
@ Test
470
- public void testFailoverCachedWithGateway () throws Exception {
460
+ public void testFailoverCachedWithGateway () {
471
461
final TcpNetServerConnectionFactory server = new TcpNetServerConnectionFactory (0 );
472
462
server .setBeanName ("server" );
473
463
server .afterPropertiesSet ();
@@ -490,7 +480,7 @@ public void testFailoverCachedWithGateway() throws Exception {
490
480
cachingClient .afterPropertiesSet ();
491
481
492
482
// Failover
493
- List <AbstractClientConnectionFactory > clientFactories = new ArrayList <AbstractClientConnectionFactory >();
483
+ List <AbstractClientConnectionFactory > clientFactories = new ArrayList <>();
494
484
clientFactories .add (cachingClient );
495
485
FailoverClientConnectionFactory failoverClient = new FailoverClientConnectionFactory (clientFactories );
496
486
failoverClient .setSingleUse (true );
@@ -505,13 +495,13 @@ public void testFailoverCachedWithGateway() throws Exception {
505
495
outbound .afterPropertiesSet ();
506
496
outbound .start ();
507
497
508
- outbound .handleMessage (new GenericMessage <String >("foo" ));
498
+ outbound .handleMessage (new GenericMessage <>("foo" ));
509
499
Message <byte []> result = (Message <byte []>) replyChannel .receive (10000 );
510
500
assertThat (result ).isNotNull ();
511
501
assertThat (new String (result .getPayload ())).isEqualTo ("foo" );
512
502
513
503
// INT-4024 - second reply had bad connection id
514
- outbound .handleMessage (new GenericMessage <String >("foo" ));
504
+ outbound .handleMessage (new GenericMessage <>("foo" ));
515
505
result = (Message <byte []>) replyChannel .receive (10000 );
516
506
assertThat (result ).isNotNull ();
517
507
assertThat (new String (result .getPayload ())).isEqualTo ("foo" );
@@ -557,13 +547,13 @@ public void testFailoverCachedRealBadHost() throws Exception {
557
547
cachingFactory2 .setBeanName ("cache2" );
558
548
559
549
// Failover
560
- List <AbstractClientConnectionFactory > factories = new ArrayList <AbstractClientConnectionFactory >();
550
+ List <AbstractClientConnectionFactory > factories = new ArrayList <>();
561
551
factories .add (cachingFactory1 );
562
552
factories .add (cachingFactory2 );
563
553
FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory (factories );
564
554
failoverFactory .start ();
565
555
TcpConnection conn1 = failoverFactory .getConnection ();
566
- GenericMessage <String > message = new GenericMessage <String >("foo" );
556
+ GenericMessage <String > message = new GenericMessage <>("foo" );
567
557
conn1 .send (message );
568
558
conn1 .close ();
569
559
TcpConnection conn2 = failoverFactory .getConnection ();
@@ -595,9 +585,11 @@ private void testRealGuts(AbstractClientConnectionFactory client1, AbstractClien
595
585
client2 .setTaskExecutor (holder .exec );
596
586
client1 .setBeanName ("client1" );
597
587
client2 .setBeanName ("client2" );
598
- client1 .setApplicationEventPublisher (NULL_PUBLISHER );
599
- client2 .setApplicationEventPublisher (NULL_PUBLISHER );
600
- List <AbstractClientConnectionFactory > factories = new ArrayList <AbstractClientConnectionFactory >();
588
+ client1 .setApplicationEventPublisher (event -> {
589
+ });
590
+ client2 .setApplicationEventPublisher (event -> {
591
+ });
592
+ List <AbstractClientConnectionFactory > factories = new ArrayList <>();
601
593
factories .add (client1 );
602
594
factories .add (client2 );
603
595
FailoverClientConnectionFactory failFactory = new FailoverClientConnectionFactory (factories );
@@ -610,10 +602,10 @@ private void testRealGuts(AbstractClientConnectionFactory client1, AbstractClien
610
602
outGateway .start ();
611
603
QueueChannel replyChannel = new QueueChannel ();
612
604
outGateway .setReplyChannel (replyChannel );
613
- Message <String > message = new GenericMessage <String >("foo" );
605
+ Message <String > message = new GenericMessage <>("foo" );
614
606
outGateway .setRemoteTimeout (120000 );
615
607
outGateway .handleMessage (message );
616
- Socket socket = null ;
608
+ Socket socket ;
617
609
if (!singleUse ) {
618
610
socket = getSocket (client1 );
619
611
port1 = socket .getLocalPort ();
@@ -644,12 +636,14 @@ private Holder setupAndStartServers(AbstractServerConnectionFactory server1,
644
636
server2 .setTaskExecutor (exec );
645
637
server1 .setBeanName ("server1" );
646
638
server2 .setBeanName ("server2" );
647
- server1 .setApplicationEventPublisher (NULL_PUBLISHER );
648
- server2 .setApplicationEventPublisher (NULL_PUBLISHER );
639
+ server1 .setApplicationEventPublisher (event -> {
640
+ });
641
+ server2 .setApplicationEventPublisher (event -> {
642
+ });
649
643
TcpInboundGateway gateway1 = new TcpInboundGateway ();
650
644
gateway1 .setConnectionFactory (server1 );
651
645
SubscribableChannel channel = new DirectChannel ();
652
- final AtomicReference <String > connectionId = new AtomicReference <String >();
646
+ final AtomicReference <String > connectionId = new AtomicReference <>();
653
647
channel .subscribe (message -> {
654
648
connectionId .set ((String ) message .getHeaders ().get (IpHeaders .CONNECTION_ID ));
655
649
((MessageChannel ) message .getHeaders ().getReplyChannel ()).send (message );
@@ -695,7 +689,9 @@ private static class Holder {
695
689
696
690
}
697
691
698
- private static AbstractClientConnectionFactory createFactoryWithMockConnection (TcpConnectionSupport mockConn ) throws Exception {
692
+ private static AbstractClientConnectionFactory createFactoryWithMockConnection (TcpConnectionSupport mockConn )
693
+ throws Exception {
694
+
699
695
AbstractClientConnectionFactory factory = mock (AbstractClientConnectionFactory .class );
700
696
when (factory .getConnection ()).thenReturn (mockConn );
701
697
when (factory .isActive ()).thenReturn (true );
0 commit comments