Merge "Issue #1570 add ability for synchronous operations in PythonJobCoordinator" into development

Former-commit-id: 1859c39278 [formerly e2a5618795] [formerly 4393e42be9] [formerly 1859c39278 [formerly e2a5618795] [formerly 4393e42be9] [formerly 17645df404 [formerly 4393e42be9 [formerly bd8da9437128570e5dbecaca81a1c97953a32742]]]]
Former-commit-id: 17645df404
Former-commit-id: 4dc46476f3 [formerly b60f7b3c69] [formerly b32697d94bf1f2217d76c1ce21618ab1b3879772 [formerly 5e5522f21b]]
Former-commit-id: 253fc505f0182df09608546066834f117141e9d8 [formerly 6ffd4f8f7c]
Former-commit-id: 2452732785
This commit is contained in:
Nate Jensen 2013-02-12 16:37:33 -06:00 committed by Gerrit Code Review
commit 72dea7d093
2 changed files with 41 additions and 8 deletions

View file

@ -54,13 +54,17 @@ public class PythonJob<P extends PythonInterpreter, R extends Object>
private IPythonExecutor<P, R> executor;
public PythonJob(IPythonExecutor<P, R> executor,
IPythonJobListener<R> listener, ThreadLocal<P> threadLocal,
Object... args) {
IPythonJobListener<R> listener, ThreadLocal<P> threadLocal) {
this.listener = listener;
this.threadPython = threadLocal;
this.executor = executor;
}
public PythonJob(IPythonExecutor<P, R> executor, ThreadLocal<P> threadLocal) {
this.threadPython = threadLocal;
this.executor = executor;
}
@Override
public R call() {
P script = threadPython.get();
@ -68,12 +72,16 @@ public class PythonJob<P extends PythonInterpreter, R extends Object>
try {
result = executor.execute(script);
} catch (Throwable t) {
listener.jobFailed(t);
if (listener != null) {
listener.jobFailed(t);
}
return null;
}
// fire listener to alert the original caller that we are done
listener.jobFinished(result);
if (listener != null) {
listener.jobFinished(result);
}
return result;
}
}

View file

@ -21,8 +21,10 @@ package com.raytheon.uf.common.python.concurrent;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import com.raytheon.uf.common.python.PythonInterpreter;
@ -121,20 +123,43 @@ public class PythonJobCoordinator<P extends PythonInterpreter> {
}
/**
* Submits a job to the {@link ExecutorService}.
* Submits a job to the {@link ExecutorService}. Fires a listener back after
* it is done. This should be used for asynchronous operations.
*
* @param callable
* @return
* @throws Exception
*/
public <R> void submitJob(IPythonExecutor<P, R> executor,
IPythonJobListener<R> listener, Object... args) throws Exception {
public <R> void submitAsyncJob(IPythonExecutor<P, R> executor,
IPythonJobListener<R> listener) throws Exception {
// fail if the listener is null, bad things happen then
if (listener == null) {
throw new IllegalArgumentException("Listener cannot be null");
}
// submit job
PythonJob<P, R> job = new PythonJob<P, R>(executor, listener,
threadLocal, args);
threadLocal);
execService.submit(job);
}
/**
* Submits a job to the {@link ExecutorService}. Waits on the result before
* returning back. This should be used for synchronous operations.
*
* @param executor
* @return
* @throws InterruptedException
* @throws ExecutionException
*/
public <R> R submitSyncJob(IPythonExecutor<P, R> executor)
throws InterruptedException, ExecutionException {
// submit job
PythonJob<P, R> job = new PythonJob<P, R>(executor, threadLocal);
Future<R> future = execService.submit(job);
// wait for return object
return future.get();
}
/**
* This function should take the {@link PythonInterpreter} on each thread in
* the thread pool and dispose of it and then shutdown the