30
30
import org .slf4j .LoggerFactory ;
31
31
32
32
import com .ericsson .eiffel .remrem .publish .config .PropertiesConfig ;
33
- import com .ericsson .eiffel .remrem .publish .exception .RemRemPublishException ;
34
33
import com .ericsson .eiffel .remrem .publish .exception .NackException ;
34
+ import com .ericsson .eiffel .remrem .publish .exception .RemRemPublishException ;
35
35
import com .rabbitmq .client .AMQP .BasicProperties ;
36
+ import com .rabbitmq .client .AlreadyClosedException ;
37
+ import com .rabbitmq .client .BlockedListener ;
36
38
import com .rabbitmq .client .Channel ;
37
39
import com .rabbitmq .client .Connection ;
38
40
import com .rabbitmq .client .MessageProperties ;
@@ -296,15 +298,36 @@ public void createRabbitMqConnection() throws RemRemPublishException {
296
298
}
297
299
factory .setConnectionTimeout (tcpTimeOut );
298
300
rabbitConnection = factory .newConnection ();
301
+ rabbitConnection .addShutdownListener (new ShutdownListener () {
302
+ @ Override
303
+ public void shutdownCompleted (ShutdownSignalException cause ) {
304
+ log .debug ("Connection Shutdown completed " + cause .getMessage ());
305
+ try {
306
+ rabbitConnection .close ();
307
+ } catch (AlreadyClosedException | IOException e ) {
308
+ // This is intentionally added, if we do not call the close function, connection is not closed properly
309
+ // and the connections count is getting increased..
310
+ }
311
+ }
312
+ });
313
+
314
+ rabbitConnection .addBlockedListener (new BlockedListener () {
315
+ public void handleBlocked (String reason ) throws IOException {
316
+ // Connection is now blocked
317
+ log .debug ("Connection is blocked " + reason );
318
+ }
319
+
320
+ public void handleUnblocked () throws IOException {
321
+ // Connection is now unblocked
322
+ }
323
+ });
299
324
log .info ("Connected to RabbitMQ." );
300
325
rabbitChannels = new ArrayList <>();
301
326
if (channelsCount == null || channelsCount == 0 ) {
302
327
channelsCount = DEFAULT_CHANNEL_COUNT ;
303
328
}
304
329
for (int i = 0 ; i < channelsCount ; i ++) {
305
- Channel channel = rabbitConnection .createChannel ();
306
- channel .confirmSelect ();
307
- rabbitChannels .add (channel );
330
+ createNewChannel ();
308
331
}
309
332
} catch (IOException | TimeoutException e ) {
310
333
log .error (e .getMessage (), e );
@@ -313,6 +336,32 @@ public void createRabbitMqConnection() throws RemRemPublishException {
313
336
}
314
337
}
315
338
339
+ /**
340
+ * This method is used to create Rabbitmq channels
341
+ * @throws IOException
342
+ */
343
+ private Channel createNewChannel () throws IOException {
344
+ Channel channel = rabbitConnection .createChannel ();
345
+ channel .addShutdownListener (new ShutdownListener () {
346
+ public void shutdownCompleted (ShutdownSignalException cause ) {
347
+ // Beware that proper synchronization is needed here
348
+ if (cause .isInitiatedByApplication ()) {
349
+ log .debug ("Shutdown is initiated by application. Ignoring it." );
350
+ } else {
351
+ log .error ("Shutdown is NOT initiated by application." );
352
+ log .error (cause .getMessage ());
353
+ boolean cliMode = Boolean .getBoolean (PropertiesConfig .CLI_MODE );
354
+ if (cliMode ) {
355
+ System .exit (-3 );
356
+ }
357
+ }
358
+ }
359
+ });
360
+ channel .confirmSelect ();
361
+ rabbitChannels .add (channel );
362
+ return channel ;
363
+ }
364
+
316
365
private void initCli () {
317
366
setValues ();
318
367
}
@@ -517,21 +566,7 @@ public void send(String routingKey, String msg, String eventId)
517
566
throws IOException , NackException , TimeoutException , RemRemPublishException , IllegalArgumentException {
518
567
Channel channel = giveMeRandomChannel ();
519
568
checkAndCreateExchangeIfNeeded ();
520
- channel .addShutdownListener (new ShutdownListener () {
521
- public void shutdownCompleted (ShutdownSignalException cause ) {
522
- // Beware that proper synchronization is needed here
523
- if (cause .isInitiatedByApplication ()) {
524
- log .debug ("Shutdown is initiated by application. Ignoring it." );
525
- } else {
526
- log .error ("Shutdown is NOT initiated by application." );
527
- log .error (cause .getMessage ());
528
- boolean cliMode = Boolean .getBoolean (PropertiesConfig .CLI_MODE );
529
- if (cliMode ) {
530
- System .exit (-3 );
531
- }
532
- }
533
- }
534
- });
569
+
535
570
BasicProperties msgProps = usePersitance ? PERSISTENT_BASIC_APPLICATION_JSON
536
571
: MessageProperties .BASIC ;
537
572
@@ -577,9 +612,7 @@ private Channel giveMeRandomChannel() throws RemRemPublishException {
577
612
}
578
613
}
579
614
try {
580
- Channel channel = rabbitConnection .createChannel ();
581
- channel .confirmSelect ();
582
- rabbitChannels .add (channel );
615
+ Channel channel = createNewChannel ();
583
616
return channel ;
584
617
} catch (IOException e ) {
585
618
log .error (e .getMessage (), e );
0 commit comments