Skip to content

Commit

Permalink
Connect asynchronously to Active MQ server on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Ronge committed Oct 22, 2020
1 parent faeecab commit 25adb1f
Showing 1 changed file with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -70,16 +70,25 @@ public class ActiveMQDirector implements ServletContextListener {
*/
@Override
public void contextInitialized(ServletContextEvent initialisation) {
Optional<String> activeMQHost = ConfigCore.getOptionalString(ParameterCore.ACTIVE_MQ_HOST_URL);
if (activeMQHost.isPresent()) {
session = connectToServer(activeMQHost.get());
if (Objects.nonNull(session)) {
registerListeners(services);
Optional<String> 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<String> activeMQResultsTopic = ConfigCore.getOptionalString(ParameterCore.ACTIVE_MQ_RESULTS_TOPIC);
activeMQResultsTopic.ifPresent(topic -> resultsTopic = setUpReportChannel(topic));
}
logger.info("Connection to Active MQ server established.");
}

/**
Expand Down

0 comments on commit 25adb1f

Please sign in to comment.