Permalink
Browse files

Add support for asynchronous calls for ProcessManager #582

  • Loading branch information...
hekonsek committed Apr 25, 2016
1 parent 5b5eabf commit 6a4092a2a29a658f6e9ac982f0aca41e10d75dca
@@ -16,15 +16,14 @@
*/
package io.rhiot.utils.process;
import java.util.List;
import static java.util.Arrays.asList;
public class EchoMockProcessManager implements ProcessManager {
/**
* Mock process manager returning command input as an output.
*/
class EchoMockProcessManager extends ExecutorBasedProcessManager {
@Override
public List<String> executeAndJoinOutput(String... command) {
return asList(command);
List<String> executeAndJoinOutput(String... command) {
command.toList()
}
}
@@ -21,7 +21,7 @@
import java.io.IOException;
import java.util.List;
public class DefaultProcessManager implements ProcessManager {
public class DefaultProcessManager extends ExecutorBasedProcessManager {
@Override
public List<String> executeAndJoinOutput(String... command) {
@@ -25,7 +25,8 @@
/**
* Process manager based on commons-exec, optionally waits for timeout or kills the process. Default is to not timeout.
*/
public class ExecProcessManager implements ProcessManager {
public class ExecProcessManager extends ExecutorBasedProcessManager {
private static final Logger LOG = LoggerFactory.getLogger(ExecProcessManager.class);
private int timeout;
@@ -0,0 +1,38 @@
/**
* Licensed to the Rhiot under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.rhiot.utils.process;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import static java.util.concurrent.Executors.newCachedThreadPool;
public abstract class ExecutorBasedProcessManager implements ProcessManager {
private ExecutorService executor = newCachedThreadPool();
@Override
public Future<List<String>> asyncExecuteAndJoinOutput(String... command) {
return executor.submit(() -> executeAndJoinOutput(command));
}
public void close() {
executor.shutdown();
}
}
@@ -20,7 +20,7 @@
import static java.util.Arrays.asList;
public class FixedMockProcessManager implements ProcessManager {
public class FixedMockProcessManager extends ExecutorBasedProcessManager {
private final List<String> result;
@@ -17,6 +17,7 @@
package io.rhiot.utils.process;
import java.util.List;
import java.util.concurrent.Future;
/**
* Executes and manages system processes.
@@ -32,4 +33,6 @@
*/
List<String> executeAndJoinOutput(String... command);
Future<List<String>> asyncExecuteAndJoinOutput(String... command);
}

0 comments on commit 6a4092a

Please sign in to comment.