com.google.api.services.pubsub.model.Topic Java Examples

The following examples show how to use com.google.api.services.pubsub.model.Topic. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GcloudPubsub.java    From cloud-pubsub-mqtt-proxy with Apache License 2.0 6 votes vote down vote up
private void createTopic(final String topic) throws IOException {
  try {
    pubsub.projects().topics().create(topic, new Topic()).execute();
  } catch (GoogleJsonResponseException e) {
    // two threads were trying to create topic at the same time
    // first thread created a topic, causing second thread to wait(this method is synchronized)
    // second thread causes an exception since it tries to create an existing topic
    if (e.getStatusCode() == RESOURCE_CONFLICT) {
      logger.info("Topic was created by another thread");
      return ;
    }
    // if it is not a topic conflict(or topic already exists) error,
    // it must be a low level error, and the client should send the PUBLISH packet again for retry
    // we throw the exception, so that we don't send a PUBACK to the client
    throw e;
  }
  logger.info("Google Cloud Pub/Sub Topic Created");
}
 
Example #2
Source File: TopicMethods.java    From cloud-pubsub-samples-java with Apache License 2.0 6 votes vote down vote up
/**
 * Lists existing topics in the project.
 *
 * @param client Cloud Pub/Sub client.
 * @param args Command line arguments.
 * @throws IOException when Cloud Pub/Sub API calls fail.
 */
public static void listTopics(final Pubsub client, final String[] args)
        throws IOException {
    String nextPageToken = null;
    boolean hasTopics = false;
    Pubsub.Projects.Topics.List listMethod =
            client.projects().topics().list("projects/" + args[0]);
    do {
        if (nextPageToken != null) {
            listMethod.setPageToken(nextPageToken);
        }
        ListTopicsResponse response = listMethod.execute();
        if (!response.isEmpty()) {
            for (Topic topic : response.getTopics()) {
                hasTopics = true;
                System.out.println(topic.getName());
            }
        }
        nextPageToken = response.getNextPageToken();
    } while (nextPageToken != null);
    if (!hasTopics) {
        System.out.println(String.format(
                "There are no topics in the project '%s'.", args[0]));
    }
}
 
Example #3
Source File: InitServlet.java    From cloud-pubsub-samples-java with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a Cloud Pub/Sub topic if it doesn't exist.
 *
 * @param client Pubsub client object.
 * @throws IOException when API calls to Cloud Pub/Sub fails.
 */
private void setupTopic(final Pubsub client) throws IOException {
    String fullName = String.format("projects/%s/topics/%s",
            PubsubUtils.getProjectId(),
            PubsubUtils.getAppTopicName());

    try {
        client.projects().topics().get(fullName).execute();
    } catch (GoogleJsonResponseException e) {
        if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) {
            // Create the topic if it doesn't exist
            client.projects().topics()
                    .create(fullName, new Topic())
                    .execute();
        } else {
            throw e;
        }
    }
}
 
Example #4
Source File: InjectorUtils.java    From deployment-examples with MIT License 5 votes vote down vote up
/** Create a topic if it doesn't exist. */
public static void createTopic(Pubsub client, String fullTopicName) throws IOException {
  System.out.println("fullTopicName " + fullTopicName);
  try {
    client.projects().topics().get(fullTopicName).execute();
  } catch (GoogleJsonResponseException e) {
    if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) {
      Topic topic = client.projects().topics().create(fullTopicName, new Topic()).execute();
      System.out.printf("Topic %s was created.%n", topic.getName());
    }
  }
}
 
Example #5
Source File: ExampleUtils.java    From deployment-examples with MIT License 5 votes vote down vote up
private void setupPubsubTopic(String topic) throws IOException {
  if (pubsubClient == null) {
    pubsubClient = newPubsubClient(options.as(PubsubOptions.class)).build();
  }
  if (executeNullIfNotFound(pubsubClient.projects().topics().get(topic)) == null) {
    pubsubClient.projects().topics().create(topic, new Topic().setName(topic)).execute();
  }
}
 
Example #6
Source File: InjectorUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Create a topic if it doesn't exist. */
public static void createTopic(Pubsub client, String fullTopicName) throws IOException {
  System.out.println("fullTopicName " + fullTopicName);
  try {
    client.projects().topics().get(fullTopicName).execute();
  } catch (GoogleJsonResponseException e) {
    if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) {
      Topic topic = client.projects().topics().create(fullTopicName, new Topic()).execute();
      System.out.printf("Topic %s was created.%n", topic.getName());
    }
  }
}
 
Example #7
Source File: ExampleUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
private void setupPubsubTopic(String topic) throws IOException {
  if (pubsubClient == null) {
    pubsubClient = newPubsubClient(options.as(PubsubOptions.class)).build();
  }
  if (executeNullIfNotFound(pubsubClient.projects().topics().get(topic)) == null) {
    pubsubClient.projects().topics().create(topic, new Topic().setName(topic)).execute();
  }
}
 
Example #8
Source File: PubsubJsonClient.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public void createTopic(TopicPath topic) throws IOException {
  pubsub
      .projects()
      .topics()
      .create(topic.getPath(), new Topic())
      .execute(); // ignore Topic result.
}
 
Example #9
Source File: TopicMethods.java    From cloud-pubsub-samples-java with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new topic with a given name.
 *
 * @param client Cloud Pub/Sub client.
 * @param args Command line arguments.
 * @throws IOException when Cloud Pub/Sub API calls fail.
 */
public static void createTopic(final Pubsub client, final String[] args)
        throws IOException {
    Main.checkArgsLength(args, 3);
    String topicName = PubsubUtils.getFullyQualifiedResourceName(
            PubsubUtils.ResourceType.TOPIC, args[0], args[2]);
    Topic topic = client.projects().topics()
            .create(topicName, new Topic())
            .execute();
    System.out.printf("Topic %s was created.\n", topic.getName());
}
 
Example #10
Source File: PubsubJsonClientTest.java    From beam with Apache License 2.0 4 votes vote down vote up
private static Topic buildTopic(int i) {
  Topic topic = new Topic();
  topic.setName(PubsubClient.topicPathFromName(PROJECT.getId(), "Topic" + i).getPath());
  return topic;
}
 
Example #11
Source File: TestPublisher.java    From play-work with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args)
    throws IOException, GeneralSecurityException {

  Pubsub pubsubClient = ServiceAccountConfiguration.createPubsubClient(
      Settings.getSettings().getServiceAccountEmail(),
      Settings.getSettings().getServiceAccountP12KeyPath());
  String topicName = Settings.getSettings().getTopicName();

  try {
    Topic topic = pubsubClient
        .projects()
        .topics()
        .get(topicName)
        .execute();

    LOG.info("The topic " + topicName + " exists: " + topic.toPrettyString());
  } catch (HttpResponseException e) {
    if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) {
      // The topic doesn't exist
      LOG.info("The topic " + topicName + " doesn't exist, creating it");
      
      // TODO(kirillov): add explicit error handling here
      pubsubClient
          .projects()
          .topics()
          .create(topicName, new Topic())
          .execute();
      LOG.info("The topic " + topicName + " created");
    }
  }

  ImmutableList.Builder<PubsubMessage> listBuilder = ImmutableList.builder();

  EmmPubsub.MdmPushNotification mdmPushNotification = EmmPubsub.MdmPushNotification.newBuilder()
      .setEnterpriseId("12321321")
      .setEventNotificationSentTimestampMillis(System.currentTimeMillis())
      .addProductApprovalEvent(EmmPubsub.ProductApprovalEvent.newBuilder()
          .setApproved(EmmPubsub.ProductApprovalEvent.ApprovalStatus.UNAPPROVED)
          .setProductId("app:com.android.chrome"))
      .build();

  PublishRequest publishRequest = new PublishRequest()
      .setMessages(ImmutableList.of(new PubsubMessage()
          .encodeData(mdmPushNotification.toByteArray())));

  LOG.info("Publishing a request: " + publishRequest.toPrettyString());

  pubsubClient
      .projects()
      .topics()
      .publish(topicName, publishRequest)
      .execute();
}
 
Example #12
Source File: PubSubClient.java    From components with Apache License 2.0 4 votes vote down vote up
public void createTopic(String topic) throws IOException {
    client.projects().topics().create(getTopicPath(topic), new Topic()).execute();
}