From 3e81bce213f051ee0864f4fc1dab550370e6d790 Mon Sep 17 00:00:00 2001 From: Matthias Ronge Date: Thu, 8 Oct 2020 10:43:32 +0200 Subject: [PATCH] Connect asynchronously to Active MQ server on startup Fixes #4029 --- .../interfaces/activemq/ActiveMQDirector.java | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/Kitodo/src/main/java/org/kitodo/production/interfaces/activemq/ActiveMQDirector.java b/Kitodo/src/main/java/org/kitodo/production/interfaces/activemq/ActiveMQDirector.java index 059878d1ece..1ad20c6aebe 100644 --- a/Kitodo/src/main/java/org/kitodo/production/interfaces/activemq/ActiveMQDirector.java +++ b/Kitodo/src/main/java/org/kitodo/production/interfaces/activemq/ActiveMQDirector.java @@ -49,7 +49,7 @@ * implementation as required for the connection. */ @WebListener -public class ActiveMQDirector implements ServletContextListener { +public class ActiveMQDirector implements Runnable, ServletContextListener { private static final Logger logger = LogManager.getLogger(ActiveMQDirector.class); // When implementing new services, add them to this list @@ -70,16 +70,25 @@ public class ActiveMQDirector implements ServletContextListener { */ @Override public void contextInitialized(ServletContextEvent initialisation) { - Optional activeMQHost = ConfigCore.getOptionalString(ParameterCore.ACTIVE_MQ_HOST_URL); - if (activeMQHost.isPresent()) { - session = connectToServer(activeMQHost.get()); - if (Objects.nonNull(session)) { - registerListeners(services); - Optional activeMQResultsTopic = ConfigCore - .getOptionalString(ParameterCore.ACTIVE_MQ_RESULTS_TOPIC); - activeMQResultsTopic.ifPresent(topic -> resultsTopic = setUpReportChannel(topic)); - } + if (ConfigCore.getOptionalString(ParameterCore.ACTIVE_MQ_HOST_URL).isPresent()) { + Thread connectAsynchronously = new Thread(new ActiveMQDirector()); + connectAsynchronously.setName(ActiveMQDirector.class.getSimpleName()); + connectAsynchronously.setDaemon(true); + connectAsynchronously.start(); + } + } + + @Override + public void run() { + String activeMQHost = ConfigCore.getParameter(ParameterCore.ACTIVE_MQ_HOST_URL); + logger.info("Connecting to Active MQ server: {}", activeMQHost); + session = connectToServer(activeMQHost); + if (Objects.nonNull(session)) { + registerListeners(services); + Optional activeMQResultsTopic = ConfigCore.getOptionalString(ParameterCore.ACTIVE_MQ_RESULTS_TOPIC); + activeMQResultsTopic.ifPresent(topic -> resultsTopic = setUpReportChannel(topic)); } + logger.info("Connection to Active MQ server established."); } /**