/** * Wait until the operation is somehow finished */ public final DeferredResult join() throws InterruptedException { join(0); return this; }
/** * Immediately start specified task (e.g. block returning something and throwing some exception on failure) in a * separate thread. Use {@link DeferredResult} to get the results. Meant to start operations that have to be done in * parallel. * * @param task to perform * * @return connected and deferred result of the operation already started. */ static public DeferredResult inParallel(final Task task) { final DeferredResult deferredResult = new DeferredResult(); threadPool.execute(() -> { try { deferredResult.sendSuccess(task.perform()); } catch (Exception e) { deferredResult.sendFailure(e); } }); return deferredResult; }
@Test public void success() throws Exception { DeferredResult dr = new DeferredResult(); dr.success( (text-> assertEquals("hello", text))); dr.sendSuccess("hello"); }
/** * waits until the operation is finished and return it's result on success, or throws an error on failure. * * @throws Error or {@link Failure} if operation fails */ public <T> T waitSuccess() throws Error, InterruptedException { join(); if (isSuccess()) return (T) result; if (result instanceof Exception) throw new Error((Exception) result); throw new Failure(result); }
a.send("ping").success((Object data) -> lastObject = data).join(); assertEquals("pong", lastObject); a.send("bad", null, null).failure((Object data) -> lastObject = data).join(); assertTrue(lastObject instanceof Farcall.RemoteException); }).failure((err) -> { fail(err.toString()); }).join(); HashMap<String, Object> res = (HashMap<String, Object>) lastObject; assertEquals(2, res.size());
/** * Adds failure callback. The parameter is what is passed to the {@link #sendFailure(Object)} call. Multiple * callbacks allowed; calling in order of registration. * <p> * This handler is executed before {@link #done(Handler)}. * <p> * If called when {@link #isDone()} and !{@link #isSuccess()} the callback is invoked immediately. * * @param handler RemoteError instance. callback */ public DeferredResult failure(Handler handler) { synchronized (errorHandlers) { errorHandlers.add(handler); } if (done && !success) invokeError(); return this; }
/** * Adds the success callback. The callback parameter will get the value passed to the {@link #sendSuccess(Object)} * call. Multiple callbacks allowed; calling in order of registration. * <p> * This handler is executed before {@link #done(Handler)}. * <p> * If called when {@link #isDone()} and {@link #isSuccess()} the callback is invoked immediately. * * @param handler callback */ public DeferredResult success(Handler handler) { synchronized (successHandlers) { successHandlers.add(handler); } if (done && success) invokeSuccess(); return this; }
/** * Trigger the failure event and pass argument to all {@link #failure(Handler)} and to {@link #done(Handler)} * callbacks. Can be called only once if {@link #sendSuccess(Object)} was not called before. Further calls to this * method and to {@link #sendSuccess(Object)} are silently ignored. * * @param data to pass to callbacks */ public void sendFailure(Object data) { synchronized (access) { if (!done) { success = false; done = true; result = data; invokeError(); access.notifyAll(); } } }
/** * Trigger the success event and pass argument to all {@link #success(Handler)} and then {@link #done(Handler)} * callbacks. Can be called only once if {@link #sendFailure(Object)} was not called before. Further calls to this * method and to {@link #sendFailure(Object)} are silently ignored. * * @param data to pass to callbacks */ public void sendSuccess(Object data) { synchronized (access) { if (!done) { success = true; done = true; result = data; invokeSuccess(); access.notifyAll(); } } }
public <T> T await() throws InterruptedException { join(); return (T) result; }
public <T> T await(long millis) throws InterruptedException { return join(millis) ? (T) result : null; }
/** * Adds the callback that will be called when the operation is done, after success() or failure() respecive * invocation. The operation result can be distinguished by checking {@link #isSuccess()} value. Multiple callbacks * allowed; calling in order of registration. * <p> * If called when {@link #isDone()} == true the callback is invoked immediately. * * @param handler callback */ public DeferredResult done(Handler handler) { synchronized (doneHandlers) { doneHandlers.add(handler); } if (done) invoke(doneHandlers); return this; }