private Subscriber startSubscriber(Builder testSubscriberBuilder) throws Exception { Subscriber subscriber = testSubscriberBuilder.build(); subscriber.startAsync().awaitRunning(); return subscriber; }
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. } } }
private void startConnections( List<? extends ApiService> connections, final ApiService.Listener connectionsListener) { for (ApiService subscriber : connections) { subscriber.addListener(connectionsListener, executor); subscriber.startAsync(); } for (ApiService subscriber : connections) { subscriber.awaitRunning(); } }
subscriber.startAsync().awaitRunning(); } finally { if (subscriber != null) { subscriber.stopAsync().awaitTerminated();
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(); }
@Test public void testOpenedChannels() throws Exception { int expectedChannelCount = 1; Subscriber subscriber = startSubscriber(getTestSubscriberBuilder(testReceiver)); assertEquals( expectedChannelCount, fakeSubscriberServiceImpl.waitForOpenedStreams(expectedChannelCount)); subscriber.stopAsync().awaitTerminated(); }
/** 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(); } } } }
@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(); }
subscriber.startAsync().awaitRunning();
redelivered.consumer().ack(); subscriber.stopAsync().awaitTerminated(); subscriptionAdminClient.deleteSubscription(subscriptionName); topicAdminClient.deleteTopic(topicName);
@Override public ListenableFuture<RunResult> doRun() { synchronized (this) { if (subscriber.isRunning()) { return Futures.immediateFuture(RunResult.empty()); } if (shuttingDown) { return Futures.immediateFailedFuture( new IllegalStateException("the task is shutting down")); } try { subscriber.startAsync().awaitRunning(); } catch (Exception e) { log.error("Fatal error from subscriber.", e); subscriber = Subscriber.defaultBuilder(this.subscription, this).build(); return Futures.immediateFailedFuture(e); } return Futures.immediateFuture(RunResult.empty()); } }
@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(); } }
synchronized public void start() { this.subscriber = Subscriber .newBuilder(ProjectSubscriptionName.of(project, subscriptionName), messageReceiver) .setCredentialsProvider(FixedCredentialsProvider.create(credentials)) .build(); subscriber.addListener(new GooglePubsubFailureHandler(this, formatSubscriptionName(project, subscriptionName)), MoreExecutors.directExecutor()); subscriber.startAsync().awaitRunning(); log.info("Google Pubsub subscriber started for {}", formatSubscriptionName(project, subscriptionName)); }
public void stop() { subscriber.stopAsync().awaitTerminated(); }