提交 183e0e53 编写于 作者: K kohsuke

implemented another channel for doing computation locally.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@1379 71c3de6d-444a-0410-be80-ed276b4c234a
上级 60c98440
......@@ -66,7 +66,7 @@ import java.util.logging.Logger;
*
* @author Kohsuke Kawaguchi
*/
public class Channel {
public class Channel implements VirtualChannel {
private final ObjectInputStream ois;
private final ObjectOutputStream oos;
/*package*/ final Executor executor;
......@@ -202,15 +202,7 @@ public class Channel {
}
/**
* Makes a remote procedure call.
*
* <p>
* Sends {@link Callable} to the remote system, executes it, and returns its result.
*
* @throws InterruptedException
* If the current thread is interrupted while waiting for the completion.
* @throws IOException
* If there's any error in the communication between {@link Channel}s.
* {@inheritDoc}
*/
public <V extends Serializable,T extends Throwable>
V call(Callable<V,T> callable) throws IOException, T, InterruptedException {
......@@ -226,16 +218,7 @@ public class Channel {
}
/**
* Makes an asynchronous remote procedure call.
*
* <p>
* Similar to {@link #call(Callable)} but returns immediately.
* The result of the {@link Callable} can be obtained through the {@link Future} object.
*
* @return
* The {@link Future} object that can be used to wait for the completion.
* @throws IOException
* If there's an error during the communication.
* {@inheritDoc}
*/
public <V extends Serializable,T extends Throwable>
Future<V> callAsync(final Callable<V,T> callable) throws IOException {
......@@ -299,7 +282,7 @@ public class Channel {
}
/**
* Performs an orderly shut down of this channel (and the remote peer.)
* {@inheritDoc}
*/
public synchronized void close() throws IOException {
// make sure no other commands get executed in between.
......
......@@ -7,6 +7,7 @@ import java.util.concurrent.Executors;
/**
* Entry point for running a {@link Channel} that uses stdin/stdout.
*
* <p>
* This can be used as the main class for launching a channel on
* a separate JVM.
*
......
package hudson.remoting;
import java.util.concurrent.*;
import java.io.Serializable;
import java.io.IOException;
/**
* {@link VirtualChannel} that performs computation on the local JVM.
*
* @author Kohsuke Kawaguchi
*/
public class LocalChannel implements VirtualChannel {
private final ExecutorService executor;
public LocalChannel(ExecutorService executor) {
this.executor = executor;
}
public <V extends Serializable, T extends Throwable> V call(Callable<V, T> callable) throws T {
return callable.call();
}
public <V extends Serializable, T extends Throwable> Future<V> callAsync(final Callable<V,T> callable) throws IOException {
final java.util.concurrent.Future<V> f = executor.submit(new java.util.concurrent.Callable<V>() {
public V call() throws Exception {
try {
return callable.call();
} catch (Exception t) {
throw t;
} catch (Error t) {
throw t;
} catch (Throwable t) {
throw new ExecutionException(t);
}
}
});
return new Future<V>() {
public boolean cancel(boolean mayInterruptIfRunning) {
return f.cancel(mayInterruptIfRunning);
}
public boolean isCancelled() {
return f.isCancelled();
}
public boolean isDone() {
return f.isDone();
}
public V get() throws InterruptedException, ExecutionException {
return f.get();
}
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return f.get(timeout,unit);
}
};
}
public void close() {
// noop
}
}
package hudson.remoting;
import java.io.Serializable;
import java.io.IOException;
/**
* Virtualized {@link Channel} that allows different implementations.
*
* @author Kohsuke Kawaguchi
*/
public interface VirtualChannel {
/**
* Makes a remote procedure call.
*
* <p>
* Sends {@link Callable} to the remote system, executes it, and returns its result.
*
* @throws InterruptedException
* If the current thread is interrupted while waiting for the completion.
* @throws IOException
* If there's any error in the communication between {@link Channel}s.
*/
<V extends Serializable,T extends Throwable>
V call(Callable<V,T> callable) throws IOException, T, InterruptedException;
/**
* Makes an asynchronous remote procedure call.
*
* <p>
* Similar to {@link #call(Callable)} but returns immediately.
* The result of the {@link Callable} can be obtained through the {@link Future} object.
*
* @return
* The {@link Future} object that can be used to wait for the completion.
* @throws IOException
* If there's an error during the communication.
*/
<V extends Serializable,T extends Throwable>
Future<V> callAsync(final Callable<V,T> callable) throws IOException;
/**
* Performs an orderly shut down of this channel (and the remote peer.)
*/
void close() throws IOException;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册