/*
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.pubsub;

// [START pubsub_quickstart_subscriber]

import com.google.cloud.ServiceOptions;
import com.google.cloud.pubsub.v1.AckReplyConsumer;
import com.google.cloud.pubsub.v1.MessageReceiver;
import com.google.cloud.pubsub.v1.Subscriber;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.PubsubMessage;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

public class SubscriberExample {

  // use the default project id
  private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId();

  private static final BlockingQueue<PubsubMessage> messages = new LinkedBlockingDeque<>();

  static class MessageReceiverExample implements MessageReceiver {

    @Override
    public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
      messages.offer(message);
      consumer.ack();
    }
  }

  /** 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();
      }
    }
  }
}
// [END pubsub_quickstart_subscriber]

@@ -943,100 +943,8 @@
 age;
-%0Aimport java.util.concurrent.BlockingQueue;%0Aimport java.util.concurrent.LinkedBlockingDeque;
 %0A%0Apu
@@ -974,17 +974,16 @@
 ample %7B%0A
-%0A
   // use
@@ -1092,101 +1092,8 @@
 );%0A%0A
-  private static final BlockingQueue%3CPubsubMessage%3E messages = new LinkedBlockingDeque%3C%3E();%0A%0A
   st
@@ -1263,32 +1263,188 @@
     
-messages.offer(message);
+System.out.println(%0A          %22Message Id: %22 + message.getMessageId() + %22 Data: %22 + message.getData().toStringUtf8());%0A      // Ack only after all work for the message is complete.
 %0A   
@@ -2050,346 +2050,229 @@
  // 
-Continue to listen to messages%0A      while (true) %7B%0A        PubsubMessage message = messages.take();%0A        System.out.println(%22Message Id: %22 + message.getMessageId());%0A        System.out.println(%22Data: %22 + message.getData().toStringUtf8());%0A      %7D%0A    %7D finally %7B%0A      if (subscriber != null) %7B%0A        subscriber.stopAsync();%0A      %7D
+Allow the subscriber to run indefinitely unless an unrecoverable error occurs.%0A      subscriber.awaitTerminated();%0A    %7D catch (IllegalStateException e) %7B%0A      System.out.println(%22Subscriber unexpectedly stopped: %22 + e);
 %0A   

Make Pub/Sub subscriber example simpler and more idiomatic (#1534)