Search in sources :

Example 21 with Promise

use of org.osgi.util.promise.Promise in project aries by apache.

the class PromiseImpl method delay.

@Override
public Promise<T> delay(final long milliseconds) {
    final PromiseImpl<T> p = new PromiseImpl<T>();
    then(new Success<T, T>() {

        @Override
        public Promise<T> call(final Promise<T> resolved) throws Exception {
            ses.schedule(new Runnable() {

                @Override
                public void run() {
                    try {
                        p.resolve(resolved.getValue());
                    } catch (IllegalStateException ise) {
                    // Someone else resolved our promise?
                    } catch (Exception e) {
                        p.fail(e);
                    }
                }
            }, milliseconds, MILLISECONDS);
            return null;
        }
    }, new Failure() {

        @Override
        public void fail(final Promise<?> resolved) throws Exception {
            ses.schedule(new Runnable() {

                @Override
                public void run() {
                    try {
                        p.fail(resolved.getFailure());
                    } catch (Exception e) {
                        p.fail(e);
                    }
                }
            }, milliseconds, MILLISECONDS);
        }
    });
    return p;
}
Also used : Promise(org.osgi.util.promise.Promise) InvocationTargetException(java.lang.reflect.InvocationTargetException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) NoSuchElementException(java.util.NoSuchElementException) TimeoutException(org.osgi.util.promise.TimeoutException) Failure(org.osgi.util.promise.Failure)

Aggregations

Promise (org.osgi.util.promise.Promise)21 Failure (org.osgi.util.promise.Failure)11 ArrayList (java.util.ArrayList)9 TimeoutException (org.osgi.util.promise.TimeoutException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 List (java.util.List)8 NoSuchElementException (java.util.NoSuchElementException)8 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)8 File (java.io.File)4 URI (java.net.URI)4 Deferred (org.osgi.util.promise.Deferred)4 ResourcesRepository (aQute.bnd.osgi.repository.ResourcesRepository)3 Resource (org.osgi.resource.Resource)3 BridgeRepository (aQute.bnd.osgi.repository.BridgeRepository)2 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)2 DownloadListenerPromise (aQute.bnd.util.repository.DownloadListenerPromise)2 PromiseCollectors.toPromise (aQute.lib.promise.PromiseCollectors.toPromise)2 Reporter (aQute.service.reporter.Reporter)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2