当前位置: 首页>>代码示例>>Java>>正文


Java Service.addListener方法代码示例

本文整理汇总了Java中com.google.common.util.concurrent.Service.addListener方法的典型用法代码示例。如果您正苦于以下问题:Java Service.addListener方法的具体用法?Java Service.addListener怎么用?Java Service.addListener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.common.util.concurrent.Service的用法示例。


在下文中一共展示了Service.addListener方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: onStop

import com.google.common.util.concurrent.Service; //导入方法依赖的package包/类
/**
 * Attaches the given callbacks which will be invoked when the given Service enters a TERMINATED or FAILED state.
 * The callbacks are optional and may be invoked synchronously if the Service is already in one of these states.
 *
 * @param service            The Service to attach to.
 * @param terminatedCallback (Optional) A Runnable that will be invoked if the Service enters a TERMINATED state.
 * @param failureCallback    (Optional) A Runnable that will be invoked if the Service enters a FAILED state.
 * @param executor           An Executor to use for callback invocations.
 */
public static void onStop(Service service, Runnable terminatedCallback, Consumer<Throwable> failureCallback, Executor executor) {
    ShutdownListener listener = new ShutdownListener(terminatedCallback, failureCallback);
    service.addListener(listener, executor);

    // addListener() will not invoke the callbacks if the service is already in a terminal state. As such, we need to
    // manually check for these states after registering the listener and invoke the appropriate callback. The
    // ShutdownListener will make sure they are not invoked multiple times.
    Service.State state = service.state();
    if (state == Service.State.FAILED) {
        // We don't care (or know) the state from which we came, so we just pass some random one.
        listener.failed(Service.State.FAILED, service.failureCause());
    } else if (state == Service.State.TERMINATED) {
        listener.terminated(Service.State.TERMINATED);
    }
}
 
开发者ID:pravega,项目名称:pravega,代码行数:25,代码来源:Services.java

示例2: 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();
  }
}
 
开发者ID:apache,项目名称:twill,代码行数:36,代码来源:ControllerTest.java

示例3: startAsync

import com.google.common.util.concurrent.Service; //导入方法依赖的package包/类
/**
 * Asynchronously starts a Service and returns a CompletableFuture that will indicate when it is running.
 *
 * @param service  The Service to start.
 * @param executor An Executor to use for callback invocations.
 * @return A CompletableFuture that will be completed when the service enters a RUNNING state, or completed
 * exceptionally if the service failed to start.
 */
public static CompletableFuture<Void> startAsync(Service service, Executor executor) {
    // Service.startAsync() will fail if the service is not in a NEW state. That is, if it is already RUNNING or
    // STARTED, then the method will fail synchronously, hence we are not in danger of not invoking our callbacks,
    // as long as we register the Listener before we attempt to start.
    // Nevertheless, do make a sanity check since once added, a Listener cannot be removed.
    Preconditions.checkState(service.state() == Service.State.NEW,
            "Service expected to be %s but was %s.", Service.State.NEW, service.state());
    Preconditions.checkNotNull(executor, "executor");
    CompletableFuture<Void> result = new CompletableFuture<>();
    service.addListener(new StartupListener(result), executor);
    service.startAsync();
    return result;
}
 
开发者ID:pravega,项目名称:pravega,代码行数:22,代码来源:Services.java

示例4: listenTo

import com.google.common.util.concurrent.Service; //导入方法依赖的package包/类
public static void listenTo(Service service, MetricRegistry metricRegistry) {
    service.addListener(new ServiceFailureListener(service, metricRegistry), MoreExecutors.sameThreadExecutor());
}
 
开发者ID:bazaarvoice,项目名称:emodb,代码行数:4,代码来源:ServiceFailureListener.java


注:本文中的com.google.common.util.concurrent.Service.addListener方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。