From 66e4ac61945542779c5c2136a5fca03b8555964e Mon Sep 17 00:00:00 2001 From: Arnold Galovics Date: Thu, 19 Jan 2023 12:08:38 +0100 Subject: [PATCH] FINERACT-1724: Removed accidental closing of JMS connection upon startup --- .../jms/JMSMultiExternalEventProducer.java | 29 ++++++++++++------- .../JMSMultiExternalEventProducerTest.java | 1 - 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/producer/jms/JMSMultiExternalEventProducer.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/producer/jms/JMSMultiExternalEventProducer.java index e063e44f4..794c45cc3 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/producer/jms/JMSMultiExternalEventProducer.java +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/producer/jms/JMSMultiExternalEventProducer.java @@ -39,6 +39,7 @@ import org.apache.fineract.infrastructure.core.messaging.jms.MessageFactory; import org.apache.fineract.infrastructure.core.service.HashingService; import org.apache.fineract.infrastructure.event.external.exception.AcknowledgementTimeoutException; import org.apache.fineract.infrastructure.event.external.producer.ExternalEventProducer; +import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -49,7 +50,7 @@ import org.springframework.stereotype.Component; @Slf4j @RequiredArgsConstructor @ConditionalOnProperty(value = "fineract.events.external.producer.jms.enabled", havingValue = "true") -public class JMSMultiExternalEventProducer implements ExternalEventProducer, InitializingBean { +public class JMSMultiExternalEventProducer implements ExternalEventProducer, InitializingBean, DisposableBean { @Qualifier("eventDestination") private final Destination destination; @@ -67,22 +68,30 @@ public class JMSMultiExternalEventProducer implements ExternalEventProducer, Ini private final List producers = new ArrayList<>(); + private Connection connection; + @Override public void afterPropertiesSet() throws Exception { int producerCount = getProducerCount(); - try (Connection connection = connectionFactory.createConnection()) { - for (int i = 0; i < producerCount; i++) { - // It's crucial to create the session within the loop, otherwise the producers won't be handled as - // parallel - // producers - Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - MessageProducer producer = session.createProducer(destination); - producers.add(producer); - } + connection = connectionFactory.createConnection(); + for (int i = 0; i < producerCount; i++) { + // It's crucial to create the session within the loop, otherwise the producers won't be handled as + // parallel + // producers + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer producer = session.createProducer(destination); + producers.add(producer); } log.info("Initialized JMS multi producer for external events with {} parallel producers", producerCount); } + @Override + public void destroy() throws Exception { + if (connection != null) { + connection.close(); + } + } + private int getProducerCount() { return fineractProperties.getEvents().getExternal().getProducer().getJms().getProducerCount(); } diff --git a/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/producer/jms/JMSMultiExternalEventProducerTest.java b/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/producer/jms/JMSMultiExternalEventProducerTest.java index 503717ed8..2f28dca2a 100644 --- a/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/producer/jms/JMSMultiExternalEventProducerTest.java +++ b/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/producer/jms/JMSMultiExternalEventProducerTest.java @@ -101,7 +101,6 @@ class JMSMultiExternalEventProducerTest { verify(session1).createProducer(destination); verify(session2).createProducer(destination); verify(session3).createProducer(destination); - verify(connection).close(); } @Test