Skip to content

Commit 7c16916

Browse files
Added code changes so that REMReM Publish should not die even (#248)
* Added code changes so that REMReM Publish should not die even if RabbitMQ is unavailable when starting up
1 parent 34d7f80 commit 7c16916

File tree

5 files changed

+66
-24
lines changed

5 files changed

+66
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 2.0.26
2+
- Added code changes so that REMReM Publish should not die if RabbitMQ is unavailable when starting up.
3+
14
## 2.0.25
25
- Implemented "publisher confirms " in REMReM so that this can be used to get confirmation about the messages sent to MB.
36
- Implemented configurable parameters for TCP connection timeout against LDAP and MB

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<version>2.0.8</version>
1010
</parent>
1111
<properties>
12-
<eiffel-remrem-publish.version>2.0.25</eiffel-remrem-publish.version>
12+
<eiffel-remrem-publish.version>2.0.26</eiffel-remrem-publish.version>
1313
<eiffel-remrem-semantics.version>2.2.3</eiffel-remrem-semantics.version>
1414
</properties>
1515
<artifactId>eiffel-remrem-publish</artifactId>

publish-common/src/main/java/com/ericsson/eiffel/remrem/publish/exception/RemRemPublishException.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,22 @@
1414
*/
1515
package com.ericsson.eiffel.remrem.publish.exception;
1616

17+
import com.ericsson.eiffel.remrem.publish.helper.RMQBeanConnectionFactory;
18+
1719
public class RemRemPublishException extends Exception {
1820

1921
private static final long serialVersionUID = 1L;
2022

2123
public RemRemPublishException(String message) {
2224
super(message);
2325
}
26+
27+
public RemRemPublishException(String message, Throwable cause) {
28+
super(message, cause);
29+
}
30+
31+
public RemRemPublishException(String message, RMQBeanConnectionFactory factory,
32+
Throwable cause) {
33+
super(message + factory.getHost() + ":" + factory.getPort(), cause);
34+
}
2435
}

publish-common/src/main/java/com/ericsson/eiffel/remrem/publish/helper/RabbitMqProperties.java

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class RabbitMqProperties {
5353
private Integer channelsCount;
5454
private boolean createExchangeIfNotExisting;
5555
private Integer tcpTimeOut;
56+
private boolean hasExchange = false;
5657
// built in tcp connection timeout value for MB in milliseconds.
5758
public static final Integer DEFAULT_TCP_TIMEOUT = 60000;
5859
private Long waitForConfirmsTimeOut;
@@ -88,7 +89,10 @@ public String getExchangeName() {
8889
}
8990

9091
public void setExchangeName(String exchangeName) {
91-
this.exchangeName = exchangeName;
92+
if (!exchangeName.equals(this.exchangeName)) {
93+
this.exchangeName = exchangeName;
94+
this.hasExchange = false;
95+
}
9296
}
9397

9498
public Integer getPort() {
@@ -183,7 +187,7 @@ public void setRabbitConnection(Connection rabbitConnection) {
183187
this.rabbitConnection = rabbitConnection;
184188
}
185189

186-
public void init() throws RemRemPublishException {
190+
public void init() {
187191
log.info("RabbitMqProperties init ...");
188192
if (Boolean.getBoolean(PropertiesConfig.CLI_MODE)) {
189193
initCli();
@@ -238,7 +242,13 @@ public void init() throws RemRemPublishException {
238242
} catch (NoSuchAlgorithmException e) {
239243
log.error(e.getMessage(), e);
240244
}
241-
checkAndCreateExchangeIfNeeded();
245+
try {
246+
//The exception can be safely handled here as there is a check for existence of exchange is done before each publish.
247+
checkAndCreateExchangeIfNeeded();
248+
} catch (RemRemPublishException e) {
249+
log.error("Error occured while setting up the RabbitMq Connection. "+e.getMessage());
250+
e.printStackTrace();
251+
}
242252
}
243253

244254
/**
@@ -264,7 +274,8 @@ public void createRabbitMqConnection() throws RemRemPublishException {
264274
}
265275
} catch (IOException | TimeoutException e) {
266276
log.error(e.getMessage(), e);
267-
throw new RemRemPublishException("Failed to create connection for Rabbitmq ::" + factory.getHost() + ":" + factory.getPort());
277+
throw new RemRemPublishException("Failed to create connection for Rabbitmq :: ", factory,
278+
e);
268279
}
269280
}
270281

@@ -364,19 +375,26 @@ public void checkAndCreateExchangeIfNeeded() throws RemRemPublishException {
364375
try {
365376
connection = factory.newConnection();
366377
} catch (final IOException | TimeoutException e) {
367-
throw new RemRemPublishException("Exception occurred while creating Rabbitmq connection ::" + factory.getHost() + ":" + factory.getPort() + e.getMessage());
378+
throw new RemRemPublishException(
379+
"Exception occurred while creating Rabbitmq connection :: ", factory, e);
368380
}
369381
Channel channel = null;
370382
try {
371383
channel = connection.createChannel();
372384
} catch (final IOException e) {
373-
throw new RemRemPublishException("Exception occurred while creating Channel with Rabbitmq connection ::" + factory.getHost() + ":" + factory.getPort() + e.getMessage());
385+
throw new RemRemPublishException(
386+
"Exception occurred while creating Channel with Rabbitmq connection ::",
387+
factory, e);
374388
}
375389
try {
376390
channel.exchangeDeclare(exchangeName, "topic", true);
391+
log.info("Exchange {} is created",exchangeName);
392+
hasExchange = true;
377393
} catch (final IOException e) {
378394
log.info(exchangeName + "failed to create an exchange");
379-
throw new RemRemPublishException("Unable to create Exchange with Rabbitmq connection " + exchangeName + factory.getHost() + ":" + factory.getPort() + e.getMessage());
395+
throw new RemRemPublishException(
396+
"Unable to create Exchange with Rabbitmq connection " + exchangeName,
397+
factory, e);
380398
} finally {
381399
if (channel == null || channel.isOpen()) {
382400
try {
@@ -405,25 +423,32 @@ public void checkAndCreateExchangeIfNeeded() throws RemRemPublishException {
405423
* @throws IOException
406424
*/
407425
private boolean hasExchange() throws RemRemPublishException {
408-
log.info("Exchange is: " + exchangeName);
426+
if(hasExchange) {
427+
log.info("Exchange is: {}", exchangeName);
428+
return true;
429+
}
430+
409431
Connection connection;
410432
try {
411433
connection = factory.newConnection();
412434
} catch (final IOException | TimeoutException e) {
413-
throw new RemRemPublishException("Exception occurred while creating Rabbitmq connection ::" + factory.getHost() + factory.getPort() + e.getMessage());
435+
throw new RemRemPublishException(
436+
"Exception occurred while creating Rabbitmq connection :: ", factory, e);
414437
}
415438
Channel channel = null;
416439
try {
417440
channel = connection.createChannel();
418441
} catch (final IOException e) {
419-
log.info("Exchange " + exchangeName + " does not Exist");
420-
throw new RemRemPublishException("Exception occurred while creating Channel with Rabbitmq connection ::" + factory.getHost() + factory.getPort() + e.getMessage());
442+
throw new RemRemPublishException(
443+
"Exception occurred while creating Channel with Rabbitmq connection :: ",
444+
factory, e);
421445
}
422446
try {
423447
channel.exchangeDeclarePassive(exchangeName);
424-
return true;
448+
hasExchange = true;
449+
return hasExchange;
425450
} catch (final IOException e) {
426-
log.info("Exchange " + exchangeName + " does not Exist");
451+
log.info("Exchange " + exchangeName + " was not created");
427452
return false;
428453
} finally {
429454
if (channel != null && channel.isOpen()) {
@@ -437,7 +462,8 @@ private boolean hasExchange() throws RemRemPublishException {
437462
}
438463
}
439464

440-
/**
465+
466+
/**
441467
* This method is used to publish the message to RabbitMQ
442468
* @param routingKey
443469
* @param msg is Eiffel Event
@@ -449,6 +475,7 @@ private boolean hasExchange() throws RemRemPublishException {
449475
public void send(String routingKey, String msg)
450476
throws IOException, NackException, TimeoutException, RemRemPublishException, IllegalArgumentException {
451477
Channel channel = giveMeRandomChannel();
478+
checkAndCreateExchangeIfNeeded();
452479
channel.addShutdownListener(new ShutdownListener() {
453480
public void shutdownCompleted(ShutdownSignalException cause) {
454481
// Beware that proper synchronization is needed here
@@ -483,13 +510,14 @@ public void shutdownCompleted(ShutdownSignalException cause) {
483510
throw new TimeoutException("Timeout waiting for ACK " + e.getMessage());
484511
} catch (IllegalArgumentException e) {
485512
log.error("Failed to publish message due to " + e.getMessage());
486-
throw new IllegalArgumentException("DomainId limit exceeded " + e.getMessage());
513+
throw new IllegalArgumentException("DomainId limit exceeded " + e.getMessage(), e);
487514
} catch (Exception e) {
488515
log.error(e.getMessage(), e);
489516
if(!channel.isOpen()&& rabbitConnection.isOpen()){
490-
throw new RemRemPublishException("Channel was closed for Rabbitmq connection ::" + factory.getHost() + factory.getPort());
517+
throw new RemRemPublishException("Channel was closed for Rabbitmq connection :: ",
518+
factory, e);
491519
}
492-
throw new IOException("Failed to publish message due to " + e.getMessage());
520+
throw new IOException("Failed to publish message due to " + e.getMessage(), e);
493521
}
494522
}
495523

publish-service/src/test/java/com/ericsson/eiffel/remrem/publish/service/MessageServiceRMQImplUnitTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,19 @@ public class MessageServiceRMQImplUnitTest {
8686
* exchange it will throw an Exception.
8787
*/
8888
@Test
89-
public void testCreateExchangeIfNotExistingEnable() throws RemRemPublishException {
90-
boolean ExceptionOccured = false;
89+
public void testCreateExchangeIfNotExistingEnable() {
90+
boolean exceptionOccured = false;
9191
rabbitmqProtocolProperties.setExchangeName("nonexistexchangename");
9292
rabbitmqProtocolProperties.setCreateExchangeIfNotExisting(createExchangeIfNotExisting);
9393
try {
94-
rabbitmqProtocolProperties.init();
94+
rabbitmqProtocolProperties.checkAndCreateExchangeIfNeeded();
9595
} catch (RemRemPublishException e) {
96-
ExceptionOccured = true;
96+
exceptionOccured = true;
9797
} finally {
9898
rabbitmqProtocolProperties.setExchangeName(exchangeName);
9999
rabbitmqProtocolProperties.init();
100100
}
101-
assertFalse("An exception occured while creating a exchange", ExceptionOccured);
101+
assertFalse("An exception occured while creating a exchange", exceptionOccured);
102102
}
103103

104104
/**
@@ -110,7 +110,7 @@ public void testCreateExchangeIfNotExistingEnable() throws RemRemPublishExceptio
110110
public void testCreateExchangeIfNotExistingDisable() throws RemRemPublishException {
111111
rabbitmqProtocolProperties.setExchangeName("test76888");
112112
rabbitmqProtocolProperties.setCreateExchangeIfNotExisting(false);
113-
rabbitmqProtocolProperties.init();
113+
rabbitmqProtocolProperties.checkAndCreateExchangeIfNeeded();
114114

115115
rabbitmqProtocolProperties.setExchangeName(exchangeName);
116116
rabbitmqProtocolProperties.init();

0 commit comments

Comments
 (0)