2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
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.context.test.locking;
23 import java.io.BufferedReader;
24 import java.io.Closeable;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map.Entry;
32 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
33 import org.onap.policy.apex.model.basicmodel.service.AbstractParameters;
34 import org.onap.policy.apex.model.basicmodel.service.ParameterService;
35 import org.slf4j.ext.XLogger;
36 import org.slf4j.ext.XLoggerFactory;
38 import com.google.gson.Gson;
41 * The Class TestConcurrentContextThread tests concurrent use of context.
43 * @author Liam Fallon (liam.fallon@ericsson.com)
45 public class ConcurrentContextJVMThread implements Runnable, Closeable {
46 // Logger for this class
47 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextJVMThread.class);
49 private final String testType;
50 private final int jvm;
51 private final int threadCount;
52 private final int target;
53 private Process process = null;
58 * @param testType the test type
60 * @param threadCount the thread count
61 * @param target the target
62 * @throws ApexException the apex exception
64 public ConcurrentContextJVMThread(final String testType, final int jvm, final int threadCount, final int target)
65 throws ApexException {
66 this.testType = testType;
68 this.threadCount = threadCount;
75 * @see java.lang.Runnable#run()
79 final List<String> commandList = new ArrayList<>();
80 commandList.add(System.getProperty("java.home") + System.getProperty("file.separator") + "bin"
81 + System.getProperty("file.separator") + "java");
82 commandList.add("-Xms512m");
83 commandList.add("-Xmx512m");
84 commandList.add("-cp");
85 commandList.add(System.getProperty("java.class.path"));
86 commandList.add(ConcurrentContextJVM.class.getCanonicalName());
87 commandList.add(testType);
88 commandList.add(new Integer(jvm).toString());
89 commandList.add(new Integer(threadCount).toString());
90 commandList.add(new Integer(target).toString());
91 commandList.add(System.getProperty("hazelcast.config"));
93 for (final Entry<Class<?>, AbstractParameters> parameterServiceEntry : ParameterService.getAll()) {
94 commandList.add(parameterServiceEntry.getKey().getCanonicalName());
95 commandList.add(new Gson().toJson(parameterServiceEntry.getValue()));
98 LOGGER.info("starting JVM " + jvm);
101 final ProcessBuilder processBuilder = new ProcessBuilder(commandList);
102 processBuilder.redirectErrorStream(true);
106 process = processBuilder.start();
108 final InputStream is = process.getInputStream();
109 final InputStreamReader isr = new InputStreamReader(is);
110 final BufferedReader br = new BufferedReader(isr);
112 LOGGER.info("JVM Output for command " + commandList + "\n");
113 while ((line = br.readLine()) != null) {
117 // Wait to get exit value
119 final int exitValue = process.waitFor();
120 LOGGER.info("\n\nJVM " + jvm + " finished, exit value is " + exitValue);
121 } catch (final InterruptedException e) {
122 LOGGER.warn("Thread was interrupted");
123 Thread.currentThread().interrupt();
125 } catch (final IOException ioException) {
126 LOGGER.error("Error occured while writing JVM Output for command ", ioException);
132 public void close() {
133 LOGGER.info("Shutting down {} thread ...", Thread.currentThread().getName());
134 if (process != null) {
135 LOGGER.info("Destroying process ...");