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(); }
subscriber.stopAsync();
} 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);
/** Receive messages over a subscription. */ public static void main(String... args) throws Exception { // set subscriber id, eg. my-sub String subscriptionId = args[0]; ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of( PROJECT_ID, subscriptionId); Subscriber subscriber = null; try { // create a subscriber bound to the asynchronous message receiver subscriber = Subscriber.newBuilder(subscriptionName, new MessageReceiverExample()).build(); subscriber.startAsync().awaitRunning(); // Continue to listen to messages while (true) { PubsubMessage message = messages.take(); System.out.println("Message Id: " + message.getMessageId()); System.out.println("Data: " + message.getData().toStringUtf8()); } } finally { if (subscriber != null) { subscriber.stopAsync(); } } } }
subscriber.stopAsync();
@Override protected void doStop() { if (this.subscriber != null) { this.subscriber.stopAsync(); } super.doStop(); }
@Override protected void doStop() { if (this.subscriber != null) { this.subscriber.stopAsync(); } super.doStop(); }
@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(); }
private void closeConnection(PubsubSubscriber subscriber) { log.info("Closing async connection to {}", subscriber.subscriptionName()); GooglePubsubSubscriber googleSubscriber = (GooglePubsubSubscriber) subscriber; googleSubscriber.getSubscriber().stopAsync(); }