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.InputStream;
26 import java.io.InputStreamReader;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Map.Entry;
31 import org.onap.policy.apex.context.test.utils.ConfigrationProvider;
32 import org.onap.policy.apex.model.basicmodel.service.AbstractParameters;
33 import org.onap.policy.apex.model.basicmodel.service.ParameterService;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
37 import com.google.gson.Gson;
40 * The Class TestConcurrentContextThread tests concurrent use of context.
42 * @author Liam Fallon (liam.fallon@ericsson.com)
44 public class ConcurrentContextJVMThread implements Runnable, Closeable {
45 // Logger for this class
46 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextJVMThread.class);
48 private final int jvm;
49 private final ConfigrationProvider configrationProvider;
50 private Process process = null;
52 public ConcurrentContextJVMThread(final int jvm, final ConfigrationProvider configrationProvider) {
54 this.configrationProvider = configrationProvider;
60 * @see java.lang.Runnable#run()
64 final List<String> commandList = new ArrayList<>();
65 commandList.add(System.getProperty("java.home") + System.getProperty("file.separator") + "bin"
66 + System.getProperty("file.separator") + "java");
67 commandList.add("-Xms512m");
68 commandList.add("-Xmx512m");
69 commandList.add("-cp");
70 commandList.add(System.getProperty("java.class.path"));
71 commandList.add(ConcurrentContextJVM.class.getCanonicalName());
72 commandList.add(configrationProvider.getTestName());
73 commandList.add(new Integer(jvm).toString());
74 commandList.add(new Integer(configrationProvider.getThreadCount()).toString());
75 commandList.add(new Integer(configrationProvider.getLoopSize()).toString());
76 commandList.add(new Integer(configrationProvider.getAlbumSize()).toString());
77 commandList.add(new Integer(configrationProvider.getLockType().getValue()).toString());
78 commandList.add(System.getProperty("hazelcast.config", ""));
80 for (final Entry<Class<?>, AbstractParameters> parameterServiceEntry : ParameterService.getAll()) {
81 commandList.add(parameterServiceEntry.getKey().getCanonicalName());
82 commandList.add(new Gson().toJson(parameterServiceEntry.getValue()));
85 LOGGER.info("starting JVM " + jvm);
88 final ProcessBuilder processBuilder = new ProcessBuilder(commandList);
89 processBuilder.redirectErrorStream(true);
92 process = processBuilder.start();
94 final InputStream is = process.getInputStream();
95 final InputStreamReader isr = new InputStreamReader(is);
96 final BufferedReader br = new BufferedReader(isr);
98 LOGGER.info("JVM Output for command " + commandList + "\n");
99 while ((line = br.readLine()) != null) {
103 // Wait to get exit value
105 final int exitValue = process.waitFor();
106 LOGGER.info("\n\nJVM " + jvm + " finished, exit value is " + exitValue);
107 } catch (final InterruptedException e) {
108 LOGGER.warn("Thread was interrupted");
109 Thread.currentThread().interrupt();
111 } catch (final Exception ioException) {
112 LOGGER.error("Error occured while writing JVM Output for command ", ioException);
118 public void close() {
119 LOGGER.info("Shutting down {} thread ...", Thread.currentThread().getName());
120 if (process != null) {
121 LOGGER.info("Destroying process ...");