title: Google Pub/Sub Java examples
body: <p>I'm not able to find a way to read messages from pub/sub using java.</p>
  
  <p>I'm using this maven dependency in my pom</p>
  
  <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;com.google.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;google-cloud-pubsub&lt;/artifactId&gt;
    &lt;version&gt;0.17.2-alpha&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>I implemented this main method to create a new topic:</p>
  
  <pre><code>public static void main(String... args) throws Exception {
  
          // Your Google Cloud Platform project ID
          String projectId = ServiceOptions.getDefaultProjectId();
  
          // Your topic ID
          String topicId = "my-new-topic-1";
          // Create a new topic
          TopicName topic = TopicName.create(projectId, topicId);
          try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
              topicAdminClient.createTopic(topic); 
          }
  }
  </code></pre>
  
  <p>The above code works well and, indeed, I can see the new topic I created using the google cloud console.</p>
  
  <p>I implemented the following main method to write a message to my topic:</p>
  
  <pre><code>public static void main(String a[]) throws InterruptedException, ExecutionException{
          String projectId = ServiceOptions.getDefaultProjectId(); 
          String topicId = "my-new-topic-1";
  
          String payload = "Hellooooo!!!";
          PubsubMessage pubsubMessage =
                    PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(payload)).build();
  
          TopicName topic = TopicName.create(projectId, topicId);
  
          Publisher publisher;
          try {
              publisher = Publisher.defaultBuilder(
                      topic)
                      .build();
              publisher.publish(pubsubMessage);
  
              System.out.println("Sent!");
          } catch (IOException e) {
              System.out.println("Not Sended!");
              e.printStackTrace();
          }
  }
  </code></pre>
  
  <p>Now I'm not able to verify if this message was really sent.
  I would like to implement a message reader using a subscription to my topic.
  Could someone show me a correct and working java example about reading messages from a topic?</p>
  
  <p>Anyone can help me?
  Thanks in advance!</p>