Java Code Examples for com.google.common.util.concurrent.Service#startAsync()

The following examples show how to use com.google.common.util.concurrent.Service#startAsync() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Services.java    From pravega with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: LeaderServiceTest.java    From curator-extensions with Apache License 2.0 5 votes vote down vote up
/** Verify that leadership is re-acquired after the connection to ZooKeeper is lost. */
@Test
public void testLostZooKeeperConnection() throws Exception {
    int reacquireDelayMillis = 1500;
    ServiceTriggers triggers1 = new ServiceTriggers();
    ServiceTriggers triggers2 = new ServiceTriggers();
    ServiceTimer timer1 = new ServiceTimer();
    ServiceTimer timer2 = new ServiceTimer();
    List<Event> events = Collections.synchronizedList(new ArrayList<>());
    Service leader = newLeaderService(reacquireDelayMillis, TimeUnit.MILLISECONDS, supply(
            triggers1.listenTo(timer1.listenTo(trackEvents("1", events, new NopService()))),
            triggers2.listenTo(timer2.listenTo(trackEvents("2", events, new NopService())))));

    leader.startAsync();
    assertTrue(triggers1.getRunning().firedWithin(1, TimeUnit.MINUTES));

    killSession(_curator);
    assertTrue(triggers1.getTerminated().firedWithin(1, TimeUnit.MINUTES));
    assertTrue(triggers2.getRunning().firedWithin(1, TimeUnit.MINUTES));

    leader.stopAsync();
    assertTrue(triggers2.getTerminated().firedWithin(1, TimeUnit.MINUTES));

    // Verify sequence of events, no overlap between service instances.
    assertEquals(ImmutableList.of(
            new Event("1", Service.State.STARTING),
            new Event("1", Service.State.RUNNING),
            new Event("1", Service.State.STOPPING),
            new Event("1", Service.State.TERMINATED),
            new Event("2", Service.State.STARTING),
            new Event("2", Service.State.RUNNING),
            new Event("2", Service.State.STOPPING),
            new Event("2", Service.State.TERMINATED)
    ), events);

    // Verify that the re-acquire delay was observed
    long actualDelayMillis = timer2.getStartedAt() - timer1.getStoppedAt();
    assertTrue("Re-acquire delay was not observed: " + actualDelayMillis, actualDelayMillis >= reacquireDelayMillis);
}
 
Example 3
Source File: LeaderServiceTest.java    From curator-extensions with Apache License 2.0 5 votes vote down vote up
/** Verify that leadership is re-acquired after the the delegate service throws an exception at startup. */
@Test
public void testStartupFailed() throws Exception {
    int reacquireDelayMillis = 1500;
    ServiceTriggers triggers1 = new ServiceTriggers();
    ServiceTriggers triggers2 = new ServiceTriggers();
    ServiceTimer timer1 = new ServiceTimer();
    ServiceTimer timer2 = new ServiceTimer();
    List<Event> events = Collections.synchronizedList(new ArrayList<>());
    Service leader = newLeaderService(reacquireDelayMillis, TimeUnit.MILLISECONDS, supply(
            triggers1.listenTo(timer1.listenTo(trackEvents("1", events, new NopService() {
                @Override
                protected void startUp() throws Exception {
                    throw new Exception("Startup failed");
                }
            }))),
            triggers2.listenTo(timer2.listenTo(trackEvents("2", events, new NopService())))));

    leader.startAsync();
    assertTrue(triggers1.getFailed().firedWithin(1, TimeUnit.MINUTES));
    assertTrue(triggers2.getRunning().firedWithin(1, TimeUnit.MINUTES));

    // Verify sequence of events, no overlap between service instances.
    assertEquals(ImmutableList.of(
            new Event("1", Service.State.STARTING),
            new Event("1", Service.State.FAILED),
            new Event("2", Service.State.STARTING),
            new Event("2", Service.State.RUNNING)
    ), events);

    // Verify that the re-acquire delay was observed
    long actualDelayMillis = timer2.getStartedAt() - timer1.getStoppedAt();
    assertTrue("Re-acquire delay was not observed: " + actualDelayMillis, actualDelayMillis >= reacquireDelayMillis);
}
 
Example 4
Source File: LeaderServiceTest.java    From curator-extensions with Apache License 2.0 5 votes vote down vote up
/** Verify that leadership is re-acquired after the the delegate service throws an exception at shutdown. */
@Test
public void testShutdownFailed() throws Exception {
    int reacquireDelayMillis = 1500;
    ServiceTriggers triggers1 = new ServiceTriggers();
    ServiceTriggers triggers2 = new ServiceTriggers();
    ServiceTimer timer1 = new ServiceTimer();
    ServiceTimer timer2 = new ServiceTimer();
    List<Event> events = Collections.synchronizedList(new ArrayList<>());
    Service leader = newLeaderService(reacquireDelayMillis, TimeUnit.MILLISECONDS, supply(
            triggers1.listenTo(timer1.listenTo(trackEvents("1", events, new NopService() {
                @Override
                protected void shutDown() throws Exception {
                    throw new Exception("ShutDown failed");
                }
            }))),
            triggers2.listenTo(timer2.listenTo(trackEvents("2", events, new NopService())))));

    leader.startAsync();
    assertTrue(triggers1.getRunning().firedWithin(1, TimeUnit.MINUTES));

    killSession(_curator);
    assertTrue(triggers1.getFailed().firedWithin(1, TimeUnit.MINUTES));
    assertTrue(triggers2.getRunning().firedWithin(1, TimeUnit.MINUTES));

    // Verify sequence of events, no overlap between service instances.
    assertEquals(ImmutableList.of(
            new Event("1", Service.State.STARTING),
            new Event("1", Service.State.RUNNING),
            new Event("1", Service.State.STOPPING),
            new Event("1", Service.State.FAILED),
            new Event("2", Service.State.STARTING),
            new Event("2", Service.State.RUNNING)
    ), events);

    // Verify that the re-acquire delay was observed
    long actualDelayMillis = timer2.getStartedAt() - timer1.getStoppedAt();
    assertTrue("Re-acquire delay was not observed: " + actualDelayMillis, actualDelayMillis >= reacquireDelayMillis);
}