com.google.pubsub.v1.ListTopicsResponse Java Examples

The following examples show how to use com.google.pubsub.v1.ListTopicsResponse. 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: PublisherServiceTest.java    From kafka-pubsub-emulator with Apache License 2.0 6 votes vote down vote up
@Test
public void listTopics() {
  ListTopicsRequest request =
      ListTopicsRequest.newBuilder().setProject("projects/project-1").build();
  ListTopicsResponse response = blockingStub.listTopics(request);

  assertThat(
      response.getTopicsList(),
      Matchers.contains(
          com.google.pubsub.v1.Topic.newBuilder()
              .setName(TestHelpers.PROJECT1_TOPIC1)
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .build(),
          com.google.pubsub.v1.Topic.newBuilder()
              .setName(TestHelpers.PROJECT1_TOPIC2)
              .putLabels(KAFKA_TOPIC, "kafka-topic-2")
              .build()));
}
 
Example #2
Source File: PubsubGrpcClient.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public List<TopicPath> listTopics(ProjectPath project) throws IOException {
  ListTopicsRequest.Builder request =
      ListTopicsRequest.newBuilder().setProject(project.getPath()).setPageSize(LIST_BATCH_SIZE);
  ListTopicsResponse response = publisherStub().listTopics(request.build());
  if (response.getTopicsCount() == 0) {
    return ImmutableList.of();
  }
  List<TopicPath> topics = new ArrayList<>(response.getTopicsCount());
  while (true) {
    for (Topic topic : response.getTopicsList()) {
      topics.add(topicPathFromPath(topic.getName()));
    }
    if (response.getNextPageToken().isEmpty()) {
      break;
    }
    request.setPageToken(response.getNextPageToken());
    response = publisherStub().listTopics(request.build());
  }
  return topics;
}
 
Example #3
Source File: GoogleAuthClient.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
/**
 * Get topics (max 10) for our project ID: the topic list is logged to the logger.
 *
 * @param projectID  the GCP project ID to get the pubsub topics for. This is a string like
 *                   "projects/balmy-cirrus-225307" where "balmy-cirrus-225307" is
 *                   the project ID for the project you created.
 */
public void getTopics(String projectID) {
  logger.log(Level.INFO, "Will try to get topics for project {0} ...", projectID);

  ListTopicsRequest request = ListTopicsRequest.newBuilder()
          .setPageSize(10)        // get max 10 topics
          .setProject(projectID)  // for our projectID
          .build();

  ListTopicsResponse response;
  try {
    response = blockingStub.listTopics(request);
  } catch (StatusRuntimeException e) {
    logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
    return;
  }
  logger.log(Level.INFO, "Topics list:\n {0}", response.getTopicsList());
}
 
Example #4
Source File: PublisherService.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
@Override
public void listTopics(
    ListTopicsRequest request, StreamObserver<ListTopicsResponse> responseObserver) {
  logger.atFine().log("Listing Topics for %s", request);
  PaginationManager<Topic> paginationManager =
      new PaginationManager<>(
          configurationManager.getTopics(request.getProject()), Topic::getName);
  ListTopicsResponse response =
      ListTopicsResponse.newBuilder()
          .addAllTopics(paginationManager.paginate(request.getPageSize(), request.getPageToken()))
          .setNextPageToken(paginationManager.getNextToken(Topic::getName))
          .build();
  responseObserver.onNext(response);
  responseObserver.onCompleted();
}
 
Example #5
Source File: PublisherServiceTest.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
@Test
public void listTopics_withPagination() {
  ListTopicsRequest request =
      ListTopicsRequest.newBuilder().setProject("projects/project-1").setPageSize(1).build();
  ListTopicsResponse response = blockingStub.listTopics(request);

  assertThat(response.getTopicsList(), Matchers.hasSize(1));
  assertThat(
      response.getTopicsList(),
      Matchers.contains(
          Topic.newBuilder()
              .setName(TestHelpers.PROJECT1_TOPIC1)
              .putLabels(KAFKA_TOPIC, "kafka-topic-1")
              .build()));
  assertThat(response.getNextPageToken(), Matchers.not(Matchers.isEmptyOrNullString()));

  request = request.toBuilder().setPageToken(response.getNextPageToken()).setPageSize(0).build();
  response = blockingStub.listTopics(request);
  assertThat(response.getTopicsList(), Matchers.hasSize(1));
  assertThat(
      response.getTopicsList(),
      Matchers.contains(
          Topic.newBuilder()
              .setName("projects/project-1/topics/topic-2")
              .putLabels(KAFKA_TOPIC, "kafka-topic-2")
              .build()));
  assertThat(response.getNextPageToken(), Matchers.isEmptyOrNullString());
}
 
Example #6
Source File: Main.java    From cloud-pubsub-samples-java with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {

        if (args.length == 0) {
            System.err.println("Please specify your project name.");
            System.exit(1);
        }
        final String project = args[0];
        ManagedChannelImpl channelImpl = NettyChannelBuilder
            .forAddress("pubsub.googleapis.com", 443)
            .negotiationType(NegotiationType.TLS)
            .build();
        GoogleCredentials creds = GoogleCredentials.getApplicationDefault();
        // Down-scope the credential to just the scopes required by the service
        creds = creds.createScoped(Arrays.asList("https://www.googleapis.com/auth/pubsub"));
        // Intercept the channel to bind the credential
        ExecutorService executor = Executors.newSingleThreadExecutor();
        ClientAuthInterceptor interceptor = new ClientAuthInterceptor(creds, executor);
        Channel channel = ClientInterceptors.intercept(channelImpl, interceptor);
        // Create a stub using the channel that has the bound credential
        PublisherGrpc.PublisherBlockingStub publisherStub = PublisherGrpc.newBlockingStub(channel);
        ListTopicsRequest request = ListTopicsRequest.newBuilder()
                .setPageSize(10)
                .setProject("projects/" + project)
                .build();
        ListTopicsResponse resp = publisherStub.listTopics(request);
        System.out.println("Found " + resp.getTopicsCount() + " topics.");
        for (Topic topic : resp.getTopicsList()) {
            System.out.println(topic.getName());
        }
    }