02d913de4eadfa30196da07196fff89928b8405c
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.testsuites.integration.uservice.adapt.jms;
22
23 import static org.awaitility.Awaitility.await;
24
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;
30
31 public class JmsServerRunner implements Runnable {
32     private static final Logger LOGGER = LoggerFactory.getLogger(JmsServerRunner.class);
33
34     // Embedded JMS server
35     private EmbeddedActiveMQ embedded;
36
37     // Thread to run the JMS server in
38     private Thread jmsServerRunnerThread;
39
40     // Config fields
41     private final String serverName;
42     private final String serverUri;
43
44     /**
45      * Create the JMS Server.
46      *
47      * @param serverName Name of the server
48      * @param serverUri URI for the server
49      * @throws Exception on errors
50      */
51     public JmsServerRunner(String serverName, String serverUri) throws Exception {
52         this.serverName = serverName;
53         this.serverUri = serverUri;
54
55         ConfigurationImpl config = new ConfigurationImpl();
56
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");
63
64         embedded = new EmbeddedActiveMQ();
65         embedded.setConfiguration(config);
66
67         LOGGER.debug("starting JMS Server " + serverName + " on URI " + serverUri + " . . .");
68
69         jmsServerRunnerThread = new Thread(this);
70         jmsServerRunnerThread.start();
71
72         LOGGER.debug("requested start on JMS Server " + serverName + " on URI " + serverUri);
73     }
74
75     @Override
76     public void run() {
77         try {
78             LOGGER.debug("starting JMS Server thread " + serverName + " on URI " + serverUri + " . . .");
79             embedded.start();
80
81             await().atMost(30, TimeUnit.SECONDS).until(() -> embedded.getActiveMQServer().isActive());
82
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);
86         }
87     }
88
89     /**
90      * Stop the JMS server.
91      *
92      * @throws Exception on stop errors
93      */
94     public void stop() throws Exception {
95         LOGGER.debug("stopping JMS Server " + serverName + " on URI " + serverUri + " . . .");
96
97         if (!embedded.getActiveMQServer().isActive()) {
98             LOGGER.debug("JMS Server " + serverName + " already stopped on URI " + serverUri + " . . .");
99             return;
100         }
101
102         embedded.stop();
103
104         LOGGER.debug("waiting on JMS Server " + serverName + " to stop on URI " + serverUri + " . . .");
105
106         await().atMost(30, TimeUnit.SECONDS)
107                 .until(() -> !embedded.getActiveMQServer().isActive() && !jmsServerRunnerThread.isAlive());
108
109         LOGGER.debug("stopping JMS Server " + serverName + " on URI " + serverUri + " . . .");
110     }
111 }