|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.asyncobjects.Using<S,T>
S
- Service typeT
- Result typepublic abstract class Using<S extends ACloseable,T>
This is an utility class that executes some action and than closes resource
that implements the interface ACloseable
after activity in the body
finishes.
The sample below gets output stream of the socket, writes 1,2,3 to it and closes it. The promise returned from method resolves only after output is closed.
private Promise<Void> write123(ASocket socket) {
return new Using<AByteOutput,Void>(socket.getOutput()) {
protected Promise<Void> run(AByteOutput out) {
// send data, result promise will resolve when data finish writing.
// The close method on output will be called only after it rather
// then right after return of the method.
return out.write(BinaryData.fromBytes(new byte[]{(byte)1,byte)2,byte)3}));
}
}.promise();
}
Constructor Summary | |
---|---|
Using(Promise<S> servicePromise)
A constructor |
|
Using(S service)
A constructor |
Method Summary | |
---|---|
protected Promise<? extends T> |
openFailed(Throwable problem)
This method is called in separate turn in case when open operation failed. |
Promise<T> |
promise()
|
protected abstract Promise<? extends T> |
run(S service)
This method should be implemented by subclasses. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Using(Promise<S> servicePromise)
servicePromise
- a promise for servicepublic Using(S service)
service
- a service proxyMethod Detail |
---|
protected abstract Promise<? extends T> run(S service)
service
- a service that was argument of constructor
protected Promise<? extends T> openFailed(Throwable problem) throws Throwable
problem
- a problem
Throwable
- a problempublic final Promise<T> promise()
run(ACloseable)
.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |