Search in sources :

Example 1 with AsyncDelegate

use of org.osgi.service.async.delegate.AsyncDelegate in project aries by apache.

the class MethodCall method fireAndForget.

public Promise<Void> fireAndForget(Bundle clientBundle, ExecutorService executor, ScheduledExecutorService ses) {
    PromiseImpl<Void> started = new PromiseImpl<Void>(executor, ses);
    Object svc;
    try {
        svc = getService();
    } catch (Exception e) {
        logError("Unable to obtain the service object", e);
        started.fail(e);
        return started;
    }
    if (svc instanceof AsyncDelegate) {
        try {
            if (((AsyncDelegate) svc).execute(method, arguments)) {
                releaseService();
                started.resolve(null);
                return started;
            }
        } catch (Exception e) {
            releaseService();
            logError("The AsyncDelegate rejected the fire-and-forget invocation with an exception", e);
            started.fail(e);
            return started;
        }
    }
    //If we get here then svc is either not an async delegate, or it rejected the call
    PromiseImpl<Void> cleanup = new PromiseImpl<Void>();
    try {
        executor.execute(new FireAndForgetWork(this, cleanup, started));
        cleanup.onResolve(new Runnable() {

            public void run() {
                releaseService();
            }
        }).then(null, new Failure() {

            public void fail(Promise<?> resolved) throws Exception {
                logError("The fire-and-forget invocation failed", resolved.getFailure());
            }
        });
    } catch (RejectedExecutionException ree) {
        logError("The Async Service threadpool rejected the fire-and-forget invocation", ree);
        started.fail(new ServiceException("Unable to enqueue the fire-and forget task", 7, ree));
    }
    return started;
}
Also used : AsyncDelegate(org.osgi.service.async.delegate.AsyncDelegate) ServiceException(org.osgi.framework.ServiceException) PromiseImpl(org.apache.aries.async.promise.PromiseImpl) ServiceException(org.osgi.framework.ServiceException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Failure(org.osgi.util.promise.Failure) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 PromiseImpl (org.apache.aries.async.promise.PromiseImpl)1 ServiceException (org.osgi.framework.ServiceException)1 AsyncDelegate (org.osgi.service.async.delegate.AsyncDelegate)1 Failure (org.osgi.util.promise.Failure)1