2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.testsuites.integration.uservice.adapt.jms;
23 import static org.awaitility.Awaitility.await;
25 import java.util.concurrent.TimeUnit;
26 import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl;
27 import org.apache.activemq.artemis.core.server.embedded.EmbeddedActiveMQ;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class JmsServerRunner implements Runnable {
32 private static final Logger LOGGER = LoggerFactory.getLogger(JmsServerRunner.class);
34 // Embedded JMS server
35 private EmbeddedActiveMQ embedded;
37 // Thread to run the JMS server in
38 private Thread jmsServerRunnerThread;
41 private final String serverName;
42 private final String serverUri;
45 * Create the JMS Server.
47 * @param serverName Name of the server
48 * @param serverUri URI for the server
49 * @throws Exception on errors
51 public JmsServerRunner(String serverName, String serverUri) throws Exception {
52 this.serverName = serverName;
53 this.serverUri = serverUri;
55 ConfigurationImpl config = new ConfigurationImpl();
57 config.addAcceptorConfiguration(serverName, serverUri);
58 config.setSecurityEnabled(false);
59 config.setJournalDirectory("target/artemisActiveMq/data/journal");
60 config.setBindingsDirectory("target/artemisActiveMq/data/bindings");
61 config.setLargeMessagesDirectory("target/artemisActiveMq/data/largemessages");
62 config.setPagingDirectory("target/artemisActiveMq/data/paging");
64 embedded = new EmbeddedActiveMQ();
65 embedded.setConfiguration(config);
67 LOGGER.debug("starting JMS Server " + serverName + " on URI " + serverUri + " . . .");
69 jmsServerRunnerThread = new Thread(this);
70 jmsServerRunnerThread.start();
72 LOGGER.debug("requested start on JMS Server " + serverName + " on URI " + serverUri);
78 LOGGER.debug("starting JMS Server thread " + serverName + " on URI " + serverUri + " . . .");
81 await().atMost(30, TimeUnit.SECONDS).until(() -> embedded.getActiveMQServer().isActive());
83 LOGGER.debug("started JMS Server thread " + serverName + " on URI " + serverUri);
84 } catch (Exception e) {
85 LOGGER.warn("failed to start JMS Server thread " + serverName + " on URI " + serverUri, e);
90 * Stop the JMS server.
92 * @throws Exception on stop errors
94 public void stop() throws Exception {
95 LOGGER.debug("stopping JMS Server " + serverName + " on URI " + serverUri + " . . .");
97 if (!embedded.getActiveMQServer().isActive()) {
98 LOGGER.debug("JMS Server " + serverName + " already stopped on URI " + serverUri + " . . .");
104 LOGGER.debug("waiting on JMS Server " + serverName + " to stop on URI " + serverUri + " . . .");
106 await().atMost(30, TimeUnit.SECONDS)
107 .until(() -> !embedded.getActiveMQServer().isActive() && !jmsServerRunnerThread.isAlive());
109 LOGGER.debug("stopping JMS Server " + serverName + " on URI " + serverUri + " . . .");