private void stopConnections(List<? extends ApiService> connections) { ArrayList<ApiService> liveConnections; synchronized (connections) { liveConnections = new ArrayList<ApiService>(connections); connections.clear(); } for (ApiService subscriber : liveConnections) { subscriber.stopAsync(); } for (ApiService subscriber : liveConnections) { try { subscriber.awaitTerminated(); } catch (IllegalStateException e) { // If the service fails, awaitTerminated will throw an exception. // However, we could be stopping services because at least one // has already failed, so we just ignore this exception. } } }
public void startAndWait() throws Exception { Subscriber subscriber = Subscriber.newBuilder(subscriptionName, receiver).build(); ExecutorService pool = Executors.newCachedThreadPool(); subscriber.addListener( new Subscriber.Listener() { public void failed(Subscriber.State from, Throwable failure) { // Handle error. } }, pool); subscriber.startAsync(); // Wait for a stop signal. // In a server, this might be a signal to stop serving. // In this example, the signal is just a dummy Future. // // By default, Subscriber uses daemon threads (see // https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html). // Consequently, once other threads have terminated, Subscriber will not stop the JVM from // exiting. // If the Subscriber should simply run forever, either use the setExecutorProvider method in // Subscriber.Builder // to use non-daemon threads or run // for (;;) { // Thread.sleep(Long.MAX_VALUE); // } // at the end of main() to previent the main thread from exiting. done.get(); pool.shutdown(); subscriber.stopAsync().awaitTerminated(); }
} finally { if (subscriber != null) { subscriber.stopAsync().awaitTerminated();
@Test public void testOpenedChannels() throws Exception { int expectedChannelCount = 1; Subscriber subscriber = startSubscriber(getTestSubscriberBuilder(testReceiver)); assertEquals( expectedChannelCount, fakeSubscriberServiceImpl.waitForOpenedStreams(expectedChannelCount)); subscriber.stopAsync().awaitTerminated(); }
@Test public void testFailedChannel_recoverableError_channelReopened() throws Exception { int expectedChannelCount = 1; Subscriber subscriber = startSubscriber( getTestSubscriberBuilder(testReceiver) .setSystemExecutorProvider( InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(1).build())); // Recoverable error fakeSubscriberServiceImpl.sendError(new StatusException(Status.INTERNAL)); assertEquals(1, fakeSubscriberServiceImpl.waitForClosedStreams(1)); assertEquals( expectedChannelCount, fakeSubscriberServiceImpl.waitForOpenedStreams(expectedChannelCount)); subscriber.stopAsync().awaitTerminated(); }
redelivered.consumer().ack(); subscriber.stopAsync().awaitTerminated(); subscriptionAdminClient.deleteSubscription(subscriptionName); topicAdminClient.deleteTopic(topicName);
@Override public void shutdown() { Subscriber subscriber; synchronized (this) { if (shuttingDown) { throw new IllegalStateException("the task is already shutting down"); } shuttingDown = true; subscriber = this.subscriber; } // We must stop out of the lock. Stopping waits for all messages to be processed, // and processing the messages needs to lock. subscriber.stopAsync().awaitTerminated(); } }
public void stop() { subscriber.stopAsync().awaitTerminated(); }