本文整理汇总了Java中com.google.common.util.concurrent.Service.startAndWait方法的典型用法代码示例。如果您正苦于以下问题:Java Service.startAndWait方法的具体用法?Java Service.startAndWait怎么用?Java Service.startAndWait使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.util.concurrent.Service
的用法示例。
在下文中一共展示了Service.startAndWait方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: startUp
import com.google.common.util.concurrent.Service; //导入方法依赖的package包/类
@Override
protected void startUp() throws Exception {
Throwable failureCause = null;
for (Service service : services) {
try {
service.startAndWait();
} catch (UncheckedExecutionException e) {
failureCause = e.getCause();
break;
}
}
if (failureCause != null) {
// Stop all running services and then throw the failure exception
try {
stopAll();
} catch (Throwable t) {
// Ignore the stop error. Just log.
LOG.warn("Failed when stopping all services on start failure", t);
}
Throwables.propagateIfPossible(failureCause, Exception.class);
throw new RuntimeException(failureCause);
}
}
示例2: testCompletion
import com.google.common.util.concurrent.Service; //导入方法依赖的package包/类
@Test
public void testCompletion() throws ExecutionException, InterruptedException {
Service service = new DummyService("s1", new AtomicBoolean());
ListenableFuture<Service.State> completion = Services.getCompletionFuture(service);
service.start();
service.stop();
completion.get();
AtomicBoolean transiting = new AtomicBoolean();
service = new DummyService("s2", transiting);
completion = Services.getCompletionFuture(service);
service.startAndWait();
transiting.set(true);
service.stop();
try {
completion.get();
Assert.assertTrue(false);
} catch (ExecutionException e) {
// Expected
}
}
示例3: testController
import com.google.common.util.concurrent.Service; //导入方法依赖的package包/类
@Test
public void testController() throws ExecutionException, InterruptedException, TimeoutException {
InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
zkServer.startAndWait();
LOG.info("ZKServer: " + zkServer.getConnectionStr());
try {
RunId runId = RunIds.generate();
ZKClientService zkClientService = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
zkClientService.startAndWait();
Service service = createService(zkClientService, runId);
service.startAndWait();
TwillController controller = getController(zkClientService, "testController", runId);
controller.sendCommand(Command.Builder.of("test").build()).get(2, TimeUnit.SECONDS);
controller.terminate().get(2, TimeUnit.SECONDS);
final CountDownLatch terminateLatch = new CountDownLatch(1);
service.addListener(new ServiceListenerAdapter() {
@Override
public void terminated(Service.State from) {
terminateLatch.countDown();
}
}, Threads.SAME_THREAD_EXECUTOR);
Assert.assertTrue(service.state() == Service.State.TERMINATED || terminateLatch.await(2, TimeUnit.SECONDS));
zkClientService.stopAndWait();
} finally {
zkServer.stopAndWait();
}
}
示例4: testControllerListener
import com.google.common.util.concurrent.Service; //导入方法依赖的package包/类
@Test
public void testControllerListener() throws InterruptedException {
InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
zkServer.startAndWait();
LOG.info("ZKServer: " + zkServer.getConnectionStr());
try {
RunId runId = RunIds.generate();
ZKClientService zkClientService = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
zkClientService.startAndWait();
Service service = createService(zkClientService, runId);
service.startAndWait();
final CountDownLatch runLatch = new CountDownLatch(1);
TwillController controller = getController(zkClientService, "testControllerListener", runId);
controller.onRunning(new Runnable() {
@Override
public void run() {
runLatch.countDown();
}
}, Threads.SAME_THREAD_EXECUTOR);
Assert.assertTrue(runLatch.await(2, TimeUnit.SECONDS));
service.stopAndWait();
zkClientService.stopAndWait();
} finally {
zkServer.stopAndWait();
}
}