1 package org.simantics.databoard.channel;
3 import java.io.IOException;
4 import java.util.concurrent.Executor;
5 import java.util.concurrent.TimeUnit;
6 import java.util.concurrent.TimeoutException;
8 import org.simantics.databoard.binding.Binding;
11 * Delayed response object. The result or error may be or become available to
12 * this object at any given time.
14 * This interface is intended to be used by clients that make service requests
15 * over command channel.
17 * @author Toni Kalajainen <toni.kalajainen@iki.fi>
19 public interface AsyncRequest {
22 * Get the current request status. <p>
24 * If Succeeded or Failed, then result or error is available and can be read
27 * Succeeded and Failed statuses are final. <p>
29 enum Status {Waiting, Succeed, Failed} Status getStatus();
32 * Get result if available.
35 * @return result or <tt>null</tt>
36 * @throws RequestException
38 Object getResult(Binding binding) throws RequestException;
41 * Get error if available.
43 * Cause is (typically) one of these:
44 * o Communication problems {@link IOException}
45 * o Timeout {@link TimeoutException}
46 * o Service(handler) problem {@link ServiceException}
48 * @return error or <tt>null</tt>
53 * Synchronous wait for result until default timeout.
54 * Default timeout is configured to the channel.
55 * If timeout occurs TimeoutException is thrown wrapped in RequestException.
57 * @param binding the format for the result
58 * @return the result object
59 * @throws RequestException
61 Object waitForResult(Binding binding) throws RequestException;
64 * Wait for result or break after until timeout.
65 * If timeout occurs TimeoutException is thrown wrapped in RequestException.
71 * @throws RequestException
73 Object waitForResult(Binding binding, long timeout, TimeUnit unit) throws RequestException;
76 * Set a listener. If the result is already available, the event
77 * schedueled immediately.
79 * @param listener (listener may not block) or null to remove listener
81 void setListener(RequestListener listener);
83 interface RequestListener {
86 * Get thread where the listening is to be processed
88 * @return thread to run listener events
93 * Request completed, the result is available
95 * @param result the result
97 void onCompleted(Object result);
100 * There was an error in processing the request
102 * @param error the error
104 void onError(ServiceException error);