• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java Promise类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.osgi.util.promise.Promise的典型用法代码示例。如果您正苦于以下问题:Java Promise类的具体用法?Java Promise怎么用?Java Promise使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



Promise类属于org.osgi.util.promise包,在下文中一共展示了Promise类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: restart

import org.osgi.util.promise.Promise; //导入依赖的package包/类
@Override
public Promise<Void> restart() {

    Deferred<Void> future = new Deferred<>();
    Thread restartThread = new Thread("Restart-Server-Thread") {
        @Override
        public void run() {
            Server.inMaintenance = true;
            try {
                log.info("Restarting server.");
                boolean successStarted = startNewServer();
                if (!successStarted) {
                    throw new Exception("Fail to start new server process.");
                }
                log.info("Restart server (pid:" + ServerUtil.getCurrentPid() + ")");

                future.resolve(null);
            } catch (Exception ex) {
                future.fail(ex);
            }
        }
    };
    restartThread.start();
    return future.getPromise();
}
 
开发者ID:opensecuritycontroller,项目名称:osc-core,代码行数:26,代码来源:Server.java


示例2: putResolveJob

import org.osgi.util.promise.Promise; //导入依赖的package包/类
/**
 * Put a resolve job onto the queue. The resolve queue is a little
 * different: because there is no need to resolve the same file twice, we
 * check whether there's already a pending resolve job for the given file.
 * If so, just return the same promise.
 */
private Promise<InstallableUnitImpl> putResolveJob(File file) {
    Promise<InstallableUnitImpl> promise;

    Job<InstallableUnitImpl> newJob = new Job<>(() -> {
        InstallableUnitImpl unit = performResolve(file);
        replaceUnit(file, null, unit);
        return unit;
    });
    synchronized (this.processorThread) {
        Job<InstallableUnitImpl> existing = this.pendingResolves.putIfAbsent(file, newJob);
        if (existing != null) {
            // no need to unblock the processor thread because we haven't added anything
            debug("Not adding resolve job for %s as it is already on the queue", file);
            promise = existing.getPromise();
        } else {
            debug("Added resolve job for %s", file);
            promise = newJob.getPromise();
            this.processorThread.notifyAll();
        }
    }
    return promise;
}
 
开发者ID:opensecuritycontroller,项目名称:osc-core,代码行数:29,代码来源:DeploymentInstaller.java


示例3: unsponsorUnit

import org.osgi.util.promise.Promise; //导入依赖的package包/类
/**
 * Unsponsor the installable unit. Returns a promise which is resolved when the uninstall is completed.
 */
private Promise<List<Bundle>> unsponsorUnit(File file) {
    List<InstallableUnitEvent> events = new LinkedList<>();

    Promise<List<Bundle>> bundles = withLock(this.unitsLock.writeLock(), () -> {
        Promise<List<Bundle>> promise = Promises.resolved(Collections.emptyList());
        InstallableUnitImpl existing = this.units.remove(file);
        if (existing != null) {
            State origState = existing.getState();
            if (origState.equals(State.INSTALLED)) {
                promise = putUninstallJob(existing);
            }

            if (existing.setState(State.REMOVED)) {
                events.add(new InstallableUnitEvent(origState, State.REMOVED, existing));
            }
        }
        return promise;
    });

    notifyListeners(events);
    return bundles;
}
 
开发者ID:opensecuritycontroller,项目名称:osc-core,代码行数:26,代码来源:DeploymentInstaller.java


示例4: doService

import org.osgi.util.promise.Promise; //导入依赖的package包/类
@SuppressWarnings("unchecked")
protected void doService(SerializationStrategy serializationStrategy, ClassLoader loader, Method method, Object target, DataByteArrayInputStream requestStream, final DataByteArrayOutputStream responseStream, final Runnable onComplete) {

    final AsyncServiceResponse helper = new AsyncServiceResponse(loader, method, responseStream, onComplete, serializationStrategy);
    try {
        Class<?>[] types = method.getParameterTypes();
        final Object[] args = new Object[types.length];
        serializationStrategy.decodeRequest(loader, types, requestStream, args);
        final Promise<Object> promise = (Promise<Object>)method.invoke(target, args);
        promise.onResolve(() -> {
            try{
                helper.send(promise.getFailure(), promise.getFailure()==null ? promise.getValue() : null);
            }
            catch (Exception e){
                helper.send(e, null);
            }
        });

    } catch (Throwable t) {
        helper.send(t, null);
    }
}
 
开发者ID:apache,项目名称:aries-rsa,代码行数:23,代码来源:AsyncPromiseInvocationStrategy.java


示例5: callAsyncPromise

import org.osgi.util.promise.Promise; //导入依赖的package包/类
@Override
public Promise<String> callAsyncPromise(final int delay) {
    final Deferred<String> deferred = new Deferred<String>();
    new Thread(new Runnable() {
        
        @Override
        public void run() {
            if (delay == -1) {
                deferred.fail(new ExpectedTestException());
                return;
            }
            sleep(delay);
            deferred.resolve("Finished");
        }
    }).start();
    
    return deferred.getPromise();
}
 
开发者ID:apache,项目名称:aries-rsa,代码行数:19,代码来源:MyServiceImpl.java


示例6: onCall

import org.osgi.util.promise.Promise; //导入依赖的package包/类
@Override
public <T, E extends Throwable> T onCall(CallContext callContext, InterceptionHandler<T> handler) throws E {
    Stopwatch started = Stopwatch.createStarted();
    String methodName = callContext.target.getName() + "::" + callContext.method.getName();

    boolean async = (callContext.method.getReturnType() == Promise.class);

    try {
        T result = handler.invoke();
        if (async) {
            Promise<?> p = (Promise<?>) result;
            p.onResolve(() -> System.out
                    .println("Async call to " + methodName + " took " + started.elapsed(TimeUnit.MICROSECONDS)
                            + " µs"));
        }
        return result;
    } finally {
        System.out.println(
                "Sync call to " + methodName + " took " + started.elapsed(TimeUnit.MICROSECONDS) + " µs");
    }
}
 
开发者ID:primeval-io,项目名称:aspecio,代码行数:22,代码来源:AllMetricInterceptorImpl.java


示例7: onCall

import org.osgi.util.promise.Promise; //导入依赖的package包/类
@Override
public <T, E extends Throwable> T onCall(Timed annotation, CallContext callContext,
        InterceptionHandler<T> handler) {

    Stopwatch started = Stopwatch.createStarted();
    String methodName = callContext.target.getName() + "::" + callContext.method.getName();

    boolean async = (callContext.method.getReturnType() == Promise.class);

    try {
        T result = handler.invoke();
        if (async) {
            Promise<?> p = (Promise<?>) result;
            p.onResolve(() -> System.out
                    .println("Async call to " + methodName + " took " + started.elapsed(TimeUnit.MICROSECONDS)
                            + " µs"));
        }
        return result;
    } finally {
        System.out.println(
                "Sync call to " + methodName + " took " + started.elapsed(TimeUnit.MICROSECONDS) + " µs");
    }
}
 
开发者ID:primeval-io,项目名称:aspecio,代码行数:24,代码来源:AnnotatedMetricInterceptorImpl.java


示例8: getLongResult

import org.osgi.util.promise.Promise; //导入依赖的package包/类
@Override
public Promise<Long> getLongResult() {
    Deferred<Long> d = new Deferred<>();

    Promise<Long> promise = superSlowService.compute();
    promise.onResolve(() -> {
        new Thread(new Runnable() {

            @Override
            public void run() {
                d.resolveWith(promise);
            }
        }).start();
    });
    Promise<Long> promise2 = d.getPromise();

    return promise2;
}
 
开发者ID:primeval-io,项目名称:aspecio,代码行数:19,代码来源:DemoConsumerImpl.java


示例9: forward

import org.osgi.util.promise.Promise; //导入依赖的package包/类
public Promise<NeuralNetworkSequenceResult> forward(UUID[] inputIds, UUID[] outputIds, List<Tensor>[] inputs, String... tags){
	if(!valid)
		throw new RuntimeException("This neural network object is no longer valid");
	
	// store inputs
	if(inputIds == null){
		sequenceInputs.put(null, inputs[0]);
	} else {
		for(int i=0;i<inputIds.length;i++){
			sequenceInputs.put(inputIds[i], inputs[i]);
		}
	}

	// TODO what if other sequence already executing?!
	return forward(0, inputIds, outputIds, inputs, tags);
}
 
开发者ID:ibcn-cloudlet,项目名称:dianne,代码行数:17,代码来源:NeuralNetworkWrapper.java


示例10: getNeuralNetwork

import org.osgi.util.promise.Promise; //导入依赖的package包/类
@Override
public Promise<NeuralNetwork> getNeuralNetwork(NeuralNetworkInstanceDTO nni) {
	Deferred<NeuralNetwork> result = new Deferred<>();
	
	NeuralNetwork nn = nnServices.get(nni.id);
	if(nn!=null){
		result.resolve(nn);
		return result.getPromise();
	}
	
	ToWatch w = new ToWatch();
	w.nni = nni;
	w.deferred = result; 
	toWatchFor.put(nni.id, w);
	
	createNeuralNetwork(nni.id);
	
	return result.getPromise();
}
 
开发者ID:ibcn-cloudlet,项目名称:dianne,代码行数:20,代码来源:DianneImpl.java


示例11: eval

import org.osgi.util.promise.Promise; //导入依赖的package包/类
@Override
public Promise<EvaluationResult> eval(String dataset, Map<String, String> config, NeuralNetworkDTO... nns) {
	if(nns != null){
		try {
			for(NeuralNetworkDTO nn : nns)
				repository.storeNeuralNetwork(nn);
		} catch(Exception e){
			// NN could be locked but still evaluation should be possible
		}
	}
	
	EvaluationJob job = new EvaluationJob(this, dataset, config, nns);
	queueEval.add(job);
	
	sendNotification(job.jobId, Level.INFO, "Evaluation job \""+job.name+"\" submitted.");
	
	schedule(Type.EVALUATE);
	
	return job.getPromise();
}
 
开发者ID:ibcn-cloudlet,项目名称:dianne,代码行数:21,代码来源:DianneCoordinatorImpl.java


示例12: act

import org.osgi.util.promise.Promise; //导入依赖的package包/类
@Override
public Promise<AgentResult> act(String dataset, Map<String, String> config, NeuralNetworkDTO... nns) {
	if(nns !=null){
		try {
			for(NeuralNetworkDTO nn : nns)
				repository.storeNeuralNetwork(nn);
		} catch(Exception e){
			// NN could be locked but still evaluation should be possible
		}
	}
	
	ActJob job = new ActJob(this, dataset, config, nns);
	queueAct.add(job);
	
	sendNotification(job.jobId, Level.INFO, "Act job \""+job.name+"\" submitted.");
	
	schedule(Type.ACT);
	
	return job.getPromise();
}
 
开发者ID:ibcn-cloudlet,项目名称:dianne,代码行数:21,代码来源:DianneCoordinatorImpl.java


示例13: install

import org.osgi.util.promise.Promise; //导入依赖的package包/类
@Override
public Promise<List<Bundle>> install() {
    if (!this.state.equals(State.RESOLVED)) {
        throw new IllegalStateException(String.format("Cannot install unit %s: not in the %s state", this.symbolicName, State.RESOLVED));
    }
    return this.installer.putInstallJob(this);
}
 
开发者ID:opensecuritycontroller,项目名称:osc-core,代码行数:8,代码来源:InstallableUnitImpl.java


示例14: putInstallJob

import org.osgi.util.promise.Promise; //导入依赖的package包/类
/**
 * Put an install job onto the queue
 */
Promise<List<Bundle>> putInstallJob(InstallableUnitImpl unit) {
    Job<List<Bundle>> job = new Job<>(() -> installArtifacts(unit));
    synchronized (this.processorThread) {
        this.pendingInstalls.add(job);
        this.processorThread.notifyAll();
    }
    return job.getPromise();
}
 
开发者ID:opensecuritycontroller,项目名称:osc-core,代码行数:12,代码来源:DeploymentInstaller.java


示例15: putUninstallJob

import org.osgi.util.promise.Promise; //导入依赖的package包/类
/**
 * Put an uninstall job onto the queue
 */
Promise<List<Bundle>> putUninstallJob(InstallableUnitImpl unit) {
    Job<List<Bundle>> job = new Job<>(() -> uninstallArtifacts(unit));
    synchronized (this.processorThread) {
        this.pendingInstalls.add(job);
        this.processorThread.notifyAll();
    }
    return job.getPromise();
}
 
开发者ID:opensecuritycontroller,项目名称:osc-core,代码行数:12,代码来源:DeploymentInstaller.java


示例16: testInvokeFutureExceptionally

import org.osgi.util.promise.Promise; //导入依赖的package包/类
@Test
public void testInvokeFutureExceptionally() throws Exception {

    Promise<String> promise = testService.exceptionPromise();
    try{
        promise.getValue();
        fail("Must throw an exception");
    } catch (InvocationTargetException e) {
        assertTrue(e.getTargetException() instanceof IOException);
        assertEquals("test", e.getCause().getMessage());
        assertTrue(promise.getFailure() instanceof IOException);
    }
}
 
开发者ID:apache,项目名称:aries-rsa,代码行数:14,代码来源:PromiseInvocationTest.java


示例17: exceptionPromise

import org.osgi.util.promise.Promise; //导入依赖的package包/类
@Override
public Promise<String> exceptionPromise() throws IOException {
    final Deferred<String> deferred = new Deferred<String>();
    new Thread(() -> {
        sleep(500);
        deferred.fail(new IOException("test"));
    }).start();
     return deferred.getPromise();
}
 
开发者ID:apache,项目名称:aries-rsa,代码行数:10,代码来源:PromiseInvocationTest.java


示例18: invoke

import org.osgi.util.promise.Promise; //导入依赖的package包/类
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (Future.class.isAssignableFrom(method.getReturnType()) ||
        CompletionStage.class.isAssignableFrom(method.getReturnType())) {
        return createFutureResult(method, args);
    } else if (Promise.class.isAssignableFrom(method.getReturnType())) {
        return createPromiseResult(method, args);
    } else {
        return handleSyncCall(method, args);
    }
}
 
开发者ID:apache,项目名称:aries-rsa,代码行数:12,代码来源:TcpInvocationHandler.java


示例19: testAsyncPromiseException

import org.osgi.util.promise.Promise; //导入依赖的package包/类
@Test(expected = ExpectedTestException.class)
public void testAsyncPromiseException() throws Throwable {
    Promise<String> result = myServiceProxy.callAsyncPromise(-1);
    try {
        result.getValue();
    } catch (InvocationTargetException e) {
        throw e.getCause();
    }
}
 
开发者ID:apache,项目名称:aries-rsa,代码行数:10,代码来源:TcpProviderTest.java


示例20: compute

import org.osgi.util.promise.Promise; //导入依赖的package包/类
@Override
@Timed
public Promise<Long> compute() {
    Deferred<Long> deferred = new Deferred<>();
    executor.submit(() -> {
        Uninterruptibles.sleepUninterruptibly(3, TimeUnit.SECONDS);
        deferred.resolve(42L);
    });
    return deferred.getPromise();
}
 
开发者ID:primeval-io,项目名称:aspecio,代码行数:11,代码来源:SuperSlowServiceImpl.java



注:本文中的org.osgi.util.promise.Promise类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java PolicyMapKey类代码示例发布时间:2022-05-23
下一篇:
Java OSSException类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap