com.google.common.util.concurrent.Service.State Java Examples
The following examples show how to use
com.google.common.util.concurrent.Service.State.
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: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
@GuardedBy("monitor") private void terminated(final State from) { switch (from) { case NEW: TERMINATED_FROM_NEW_CALLBACK.enqueueOn(listeners); break; case RUNNING: TERMINATED_FROM_RUNNING_CALLBACK.enqueueOn(listeners); break; case STOPPING: TERMINATED_FROM_STOPPING_CALLBACK.enqueueOn(listeners); break; case STARTING: case TERMINATED: case FAILED: default: throw new AssertionError(); } }
Example #2
Source File: UsersServiceImplTest.java From connector-sdk with Apache License 2.0 | 6 votes |
@Test public void testStartAndStop() throws GeneralSecurityException, IOException { BatchRequestService batchRequestService = new BatchRequestService.Builder(service) .setGoogleCredential(credentialFactory.getCredential(Collections.emptyList())) .build(); UsersServiceImpl usersService = new UsersServiceImpl.Builder() .setBatchRequestService(batchRequestService) .setCredentialFactory(credentialFactory) .setCustomer("c1") .setBatchPolicy(BatchPolicy.fromConfiguration()) .build(); usersService.startAsync().awaitRunning(); assertTrue(batchRequestService.isRunning()); usersService.stopAsync().awaitTerminated(); assertEquals(State.TERMINATED, batchRequestService.state()); }
Example #3
Source File: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
StateSnapshot( State internalState, boolean shutdownWhenStartupFinishes, @Nullable Throwable failure) { checkArgument( !shutdownWhenStartupFinishes || internalState == STARTING, "shudownWhenStartupFinishes can only be set if state is STARTING. Got %s instead.", internalState); checkArgument( !(failure != null ^ internalState == FAILED), "A failure cause should be set if and only if the state is failed. Got %s and %s " + "instead.", internalState, failure); this.state = internalState; this.shutdownWhenStartupFinishes = shutdownWhenStartupFinishes; this.failure = failure; }
Example #4
Source File: MetricExporterTest.java From java-monitoring-client-library with Apache License 2.0 | 6 votes |
@Test public void testRun_writesMetrics() throws Exception { Optional<ImmutableList<MetricPoint<?>>> threeBatch = Optional.of(ImmutableList.of(point, point, point)); exporter.startAsync(); insertAndAssert(threeBatch); // Insert another batch in order to block until the exporter has processed the last one insertAndAssert(threeBatch); // Force the exporter to finish so that the verify counts below are deterministic insertAndAssert(poisonPill); try { exporter.awaitTerminated(500, TimeUnit.MILLISECONDS); } catch (TimeoutException timeout) { fail("MetricExporter did not reach the TERMINATED state after receiving a poison pill"); } assertThat(exporter.state()).isNotEqualTo(State.FAILED); verify(writer, times(6)).write(point); verify(writer, times(2)).flush(); }
Example #5
Source File: ServiceManager.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
@Override public void failed(State from, Throwable failure) { ServiceManagerState state = this.state.get(); if (state != null) { // Log before the transition, so that if the process exits in response to server failure, // there is a higher likelihood that the cause will be in the logs. boolean log = !(service instanceof NoOpService); if (log) { logger.log( Level.SEVERE, "Service " + service + " has failed in the " + from + " state.", failure); } state.transitionService(service, from, FAILED); } }
Example #6
Source File: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
/** * Implementing classes should invoke this method once their service has stopped. It will cause * the service to transition from {@link State#STOPPING} to {@link State#TERMINATED}. * * @throws IllegalStateException if the service is neither {@link State#STOPPING} nor {@link * State#RUNNING}. */ protected final void notifyStopped() { monitor.enter(); try { // We check the internal state of the snapshot instead of state() directly so we don't allow // notifyStopped() to be called while STARTING, even if stop() has already been called. State previous = snapshot.state; if (previous != STOPPING && previous != RUNNING) { IllegalStateException failure = new IllegalStateException("Cannot notifyStopped() when the service is " + previous); notifyFailed(failure); throw failure; } snapshot = new StateSnapshot(TERMINATED); terminated(previous); } finally { monitor.leave(); executeListeners(); } }
Example #7
Source File: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
@GuardedBy("monitor") private void terminated(final State from) { switch (from) { case NEW: TERMINATED_FROM_NEW_CALLBACK.enqueueOn(listeners); break; case RUNNING: TERMINATED_FROM_RUNNING_CALLBACK.enqueueOn(listeners); break; case STOPPING: TERMINATED_FROM_STOPPING_CALLBACK.enqueueOn(listeners); break; case STARTING: case TERMINATED: case FAILED: default: throw new AssertionError(); } }
Example #8
Source File: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
/** * Implementing classes should invoke this method once their service has stopped. It will cause * the service to transition from {@link State#STOPPING} to {@link State#TERMINATED}. * * @throws IllegalStateException if the service is neither {@link State#STOPPING} nor {@link * State#RUNNING}. */ protected final void notifyStopped() { monitor.enter(); try { // We check the internal state of the snapshot instead of state() directly so we don't allow // notifyStopped() to be called while STARTING, even if stop() has already been called. State previous = snapshot.state; if (previous != STOPPING && previous != RUNNING) { IllegalStateException failure = new IllegalStateException("Cannot notifyStopped() when the service is " + previous); notifyFailed(failure); throw failure; } snapshot = new StateSnapshot(TERMINATED); terminated(previous); } finally { monitor.leave(); executeListeners(); } }
Example #9
Source File: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
@GuardedBy("monitor") private void terminated(final State from) { switch (from) { case NEW: TERMINATED_FROM_NEW_CALLBACK.enqueueOn(listeners); break; case RUNNING: TERMINATED_FROM_RUNNING_CALLBACK.enqueueOn(listeners); break; case STOPPING: TERMINATED_FROM_STOPPING_CALLBACK.enqueueOn(listeners); break; case STARTING: case TERMINATED: case FAILED: default: throw new AssertionError(); } }
Example #10
Source File: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
@GuardedBy("monitor") private void terminated(final State from) { switch (from) { case NEW: TERMINATED_FROM_NEW_CALLBACK.enqueueOn(listeners); break; case RUNNING: TERMINATED_FROM_RUNNING_CALLBACK.enqueueOn(listeners); break; case STOPPING: TERMINATED_FROM_STOPPING_CALLBACK.enqueueOn(listeners); break; case STARTING: case TERMINATED: case FAILED: default: throw new AssertionError(); } }
Example #11
Source File: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
/** @see Service#state() */ State externalState() { if (shutdownWhenStartupFinishes && state == STARTING) { return STOPPING; } else { return state; } }
Example #12
Source File: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
private static Callback<Listener> terminatedCallback(final State from) { return new Callback<Listener>("terminated({from = " + from + "})") { @Override void call(Listener listener) { listener.terminated(from); } }; }
Example #13
Source File: ServiceManager.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
@Override public void failed(State from, Throwable failure) { ServiceManagerState state = this.state.get(); if (state != null) { // Log before the transition, so that if the process exits in response to server failure, // there is a higher likelihood that the cause will be in the logs. boolean log = !(service instanceof NoOpService); if (log) { logger.log(Level.SEVERE, "Service " + service + " has failed in the " + from + " state.", failure); } state.transitionService(service, from, FAILED); } }
Example #14
Source File: ServiceManager.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
ImmutableMultimap<State, Service> servicesByState() { ImmutableSetMultimap.Builder<State, Service> builder = ImmutableSetMultimap.builder(); monitor.enter(); try { for (Entry<State, Service> entry : servicesByState.entries()) { if (!(entry.getValue() instanceof NoOpService)) { builder.put(entry); } } } finally { monitor.leave(); } return builder.build(); }
Example #15
Source File: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
private static Callback<Listener> stoppingCallback(final State from) { return new Callback<Listener>("stopping({from = " + from + "})") { @Override void call(Listener listener) { listener.stopping(from); } }; }
Example #16
Source File: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
@GuardedBy("monitor") private void stopping(final State from) { if (from == State.STARTING) { STOPPING_FROM_STARTING_CALLBACK.enqueueOn(listeners); } else if (from == State.RUNNING) { STOPPING_FROM_RUNNING_CALLBACK.enqueueOn(listeners); } else { throw new AssertionError(); } }
Example #17
Source File: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
@CanIgnoreReturnValue @Override public final Service stopAsync() { if (monitor.enterIf(isStoppable)) { try { State previous = state(); switch (previous) { case NEW: snapshot = new StateSnapshot(TERMINATED); terminated(NEW); break; case STARTING: snapshot = new StateSnapshot(STARTING, true, null); stopping(STARTING); break; case RUNNING: snapshot = new StateSnapshot(STOPPING); stopping(RUNNING); doStop(); break; case STOPPING: case TERMINATED: case FAILED: // These cases are impossible due to the if statement above. throw new AssertionError("isStoppable is incorrectly implemented, saw: " + previous); default: throw new AssertionError("Unexpected state: " + previous); } } catch (Throwable shutdownFailure) { notifyFailed(shutdownFailure); } finally { monitor.leave(); executeListeners(); } } return this; }
Example #18
Source File: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
@GuardedBy("monitor") private void stopping(final State from) { if (from == State.STARTING) { STOPPING_FROM_STARTING_CALLBACK.enqueueOn(listeners); } else if (from == State.RUNNING) { STOPPING_FROM_RUNNING_CALLBACK.enqueueOn(listeners); } else { throw new AssertionError(); } }
Example #19
Source File: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
private static Callback<Listener> terminatedCallback(final State from) { return new Callback<Listener>("terminated({from = " + from + "})") { @Override void call(Listener listener) { listener.terminated(from); } }; }
Example #20
Source File: ServicesTest.java From attic-aurora with Apache License 2.0 | 5 votes |
@Test public void testGetServices() throws Exception { Service newService = createMock(Service.class); expect(newService.state()).andReturn(State.NEW); Service failedService = createMock(Service.class); expect(failedService.state()).andReturn(State.FAILED); Exception failureCause = new Exception(FAILURE_CAUSE_REASON); expect(failedService.failureCause()).andReturn(failureCause); Service runningService = createMock(Service.class); expect(runningService.state()).andReturn(State.RUNNING); expect(startupServiceManager.servicesByState()).andReturn( ImmutableMultimap.of( State.RUNNING, runningService, State.FAILED, failedService)); expect(activeServicesManager.servicesByState()) .andReturn(ImmutableMultimap.of(State.NEW, newService)); control.replay(); assertEquals( ImmutableList.of( ImmutableMap.of( "name", newService.getClass().getSimpleName(), "state", State.NEW), ImmutableMap.of( "name", failedService.getClass().getSimpleName(), "state", State.RUNNING), ImmutableMap.of( "name", failedService.getClass().getSimpleName(), "state", State.FAILED, "failureCause", failureCause.toString())), servicesServlet.getServices().getEntity()); }
Example #21
Source File: ServiceManager.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
@Override public void failed(State from, Throwable failure) { ServiceManagerState state = this.state.get(); if (state != null) { // Log before the transition, so that if the process exits in response to server failure, // there is a higher likelihood that the cause will be in the logs. boolean log = !(service instanceof NoOpService); if (log) { logger.log(Level.SEVERE, "Service " + service + " has failed in the " + from + " state.", failure); } state.transitionService(service, from, FAILED); } }
Example #22
Source File: ServiceManager.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
@Override public void stopping(State from) { ServiceManagerState state = this.state.get(); if (state != null) { state.transitionService(service, from, STOPPING); } }
Example #23
Source File: ServiceManager.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
ImmutableMultimap<State, Service> servicesByState() { ImmutableSetMultimap.Builder<State, Service> builder = ImmutableSetMultimap.builder(); monitor.enter(); try { for (Entry<State, Service> entry : servicesByState.entries()) { if (!(entry.getValue() instanceof NoOpService)) { builder.put(entry); } } } finally { monitor.leave(); } return builder.build(); }
Example #24
Source File: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
private static Callback<Listener> stoppingCallback(final State from) { return new Callback<Listener>("stopping({from = " + from + "})") { @Override void call(Listener listener) { listener.stopping(from); } }; }
Example #25
Source File: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
/** Checks that the current state is equal to the expected state. */ @GuardedBy("monitor") private void checkCurrentState(State expected) { State actual = state(); if (actual != expected) { if (actual == FAILED) { // Handle this specially so that we can include the failureCause, if there is one. throw new IllegalStateException( "Expected the service " + this + " to be " + expected + ", but the service has FAILED", failureCause()); } throw new IllegalStateException( "Expected the service " + this + " to be " + expected + ", but was " + actual); } }
Example #26
Source File: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
@GuardedBy("monitor") private void failed( final State from, final Throwable cause) { // can't memoize this one due to the exception new Callback<Listener>("failed({from = " + from + ", cause = " + cause + "})") { @Override void call(Listener listener) { listener.failed(from, cause); } }.enqueueOn(listeners); }
Example #27
Source File: ApplicationTest.java From connector-sdk with Apache License 2.0 | 5 votes |
private static void validateStartError(Application subject, Class<? extends Exception> causeType) throws InterruptedException { try { subject.start(); } catch (IllegalStateException e) { validateStartFailure(e, causeType); return; } finally { assertEquals(State.FAILED, subject.state()); } // missed expected exception fail(causeType + " was expected"); }
Example #28
Source File: ServiceManager.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
@Override public void stopping(State from) { ServiceManagerState state = this.state.get(); if (state != null) { state.transitionService(service, from, STOPPING); } }
Example #29
Source File: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
/** Checks that the current state is equal to the expected state. */ @GuardedBy("monitor") private void checkCurrentState(State expected) { State actual = state(); if (actual != expected) { if (actual == FAILED) { // Handle this specially so that we can include the failureCause, if there is one. throw new IllegalStateException("Expected the service " + this + " to be " + expected + ", but the service has FAILED", failureCause()); } throw new IllegalStateException("Expected the service " + this + " to be " + expected + ", but was " + actual); } }
Example #30
Source File: AbstractService.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
/** * Invoke this method to transition the service to the {@link State#FAILED}. The service will * <b>not be stopped</b> if it is running. Invoke this method when a service has failed critically * or otherwise cannot be started nor stopped. */ protected final void notifyFailed(Throwable cause) { checkNotNull(cause); monitor.enter(); try { State previous = state(); switch (previous) { case NEW: case TERMINATED: throw new IllegalStateException("Failed while in state:" + previous, cause); case RUNNING: case STARTING: case STOPPING: snapshot = new StateSnapshot(FAILED, false, cause); failed(previous, cause); break; case FAILED: // Do nothing break; default: throw new AssertionError("Unexpected state: " + previous); } } finally { monitor.leave(); executeListeners(); } }