com.google.pubsub.v1.Topic Java Examples

The following examples show how to use com.google.pubsub.v1.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: GoogleCloudPubSubSinkConfiguration.java    From divolte-collector with Apache License 2.0 6 votes vote down vote up
private static void createTopic(final String hostPort,
                                final TransportChannelProvider channelProvider,
                                final ProjectTopicName topic) {
    final TopicAdminClient topicClient;
    try {
        final TopicAdminSettings topicAdminSettings = TopicAdminSettings.newBuilder()
            .setTransportChannelProvider(channelProvider)
            .setCredentialsProvider(NoCredentialsProvider.create())
            .build();
        topicClient = TopicAdminClient.create(topicAdminSettings);
    } catch (final IOException e) {
        throw new UncheckedIOException(String.format("Error creating topic %s for pub/sub emulator %s",
                                                     topic, hostPort), e);
    }
    final ProjectName project = ProjectName.of(topic.getProject());
    if (Streams.stream(topicClient.listTopics(project).iterateAll())
               .map(Topic::getName)
               .map(ProjectTopicName::parse)
               .noneMatch(topic::equals)) {
        logger.info("Initializing Pub/Sub emulator topic: {}", topic);
        topicClient.createTopic(topic);
    }
}
 
Example #2
Source File: DeviceRegistryExample.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/** Creates a topic and grants the IoT service account access. */
protected static Topic createIotTopic(String projectId, String topicId) throws Exception {
  // Create a new topic
  final ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);

  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    final Topic topic = topicAdminClient.createTopic(topicName);
    final String topicString = topicName.toString();
    // add role -> members binding
    // create updated policy
    topicAdminClient.setIamPolicy(
        topicString,
        com.google.iam.v1.Policy.newBuilder(topicAdminClient.getIamPolicy(topicString))
            .addBindings(
                Binding.newBuilder()
                    .addMembers("serviceAccount:[email protected]")
                    .setRole(Role.owner().toString())
                    .build())
            .build());

    System.out.println("Setup topic / policy for: " + topic.getName());
    return topic;
  }
}
 
Example #3
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 #4
Source File: PubSubChannelProvisionerTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
	when(this.pubSubAdminMock.getSubscription(any())).thenReturn(null);
	doAnswer((invocation) ->
		Subscription.newBuilder()
				.setName("projects/test-project/subscriptions/" + invocation.getArgument(0))
				.setTopic("projects/test-project/topics/" + invocation.getArgument(1)).build()
	).when(this.pubSubAdminMock).createSubscription(any(), any());
	doAnswer((invocation) ->
			Topic.newBuilder().setName("projects/test-project/topics/" + invocation.getArgument(0)).build()
	).when(this.pubSubAdminMock).getTopic(any());
	when(this.properties.getExtension()).thenReturn(this.pubSubConsumerProperties);
	when(this.pubSubConsumerProperties.isAutoCreateResources()).thenReturn(true);

	this.pubSubChannelProvisioner = new PubSubChannelProvisioner(this.pubSubAdminMock);
}
 
Example #5
Source File: PubSubChannelProvisioner.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
private Topic makeSureTopicExists(String topicName, boolean autoCreate) {
	Topic topic = this.pubSubAdmin.getTopic(topicName);
	if (topic == null) {
		if (autoCreate) {
			try {
				topic = this.pubSubAdmin.createTopic(topicName);
			}
			catch (AlreadyExistsException alreadyExistsException) {
				// Ignore concurrent topic creation - we're good as long as topic was created and exists
				LOGGER.info("Failed to auto-create topic '" + topicName + "' because it already exists.");
			}
		}
		else {
			throw new ProvisioningException("Non-existing '" + topicName + "' topic.");
		}
	}

	return topic;
}
 
Example #6
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 #7
Source File: PubSubChannelProvisioner.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Override
public ConsumerDestination provisionConsumerDestination(String topicName, String group,
		ExtendedConsumerProperties<PubSubConsumerProperties> properties)
		throws ProvisioningException {

	Topic topic = makeSureTopicExists(topicName, properties.getExtension().isAutoCreateResources());

	String subscriptionName;
	Subscription subscription;
	if (StringUtils.hasText(group)) {
		// Use <topicName>.<group> as subscription name
		subscriptionName = topicName + "." + group;
		subscription = this.pubSubAdmin.getSubscription(subscriptionName);
	}
	else {
		// Generate anonymous random group since one wasn't provided
		subscriptionName = "anonymous." + topicName + "." + UUID.randomUUID().toString();
		subscription = this.pubSubAdmin.createSubscription(subscriptionName, topicName);
		this.anonymousGroupSubscriptionNames.add(subscriptionName);
	}

	// make sure subscription exists
	if (subscription == null) {
		if (properties.getExtension().isAutoCreateResources()) {
			this.pubSubAdmin.createSubscription(subscriptionName, topicName);
		}
		else {
			throw new ProvisioningException("Non-existing '" + subscriptionName + "' subscription.");
		}
	}
	else if (!subscription.getTopic().equals(topic.getName())) {
		throw new ProvisioningException(
				"Existing '" + subscriptionName + "' subscription is for a different topic '"
						+ subscription.getTopic() + "'.");
	}
	return new PubSubConsumerDestination(subscriptionName);
}
 
Example #8
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());
        }
    }
 
Example #9
Source File: PubsubHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
public Topic createTopic(String project, String topic) throws IOException {
	deleteTopic(project, topic);
	ProjectTopicName topicName = ProjectTopicName.of(project, topic);
	TopicAdminClient adminClient = getTopicAdminClient();
	LOG.info("CreateTopic {}", topicName);
	return adminClient.createTopic(topicName);
}
 
Example #10
Source File: PubSubAdmin.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
/**
 * Return every topic in a project.
 * <p>If there are multiple pages, they will all be merged into the same result.
 * @return a list of topics
 */
public List<Topic> listTopics() {
	TopicAdminClient.ListTopicsPagedResponse topicListPage =
			this.topicAdminClient.listTopics(ProjectName.of(this.projectId));

	List<Topic> topics = new ArrayList<>();
	topicListPage.iterateAll().forEach(topics::add);
	return Collections.unmodifiableList(topics);
}
 
Example #11
Source File: PubSubAdmin.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
/**
 * Get the configuration of a Google Cloud Pub/Sub topic.
 *
 * @param topicName canonical topic name, e.g., "topicName", or the fully-qualified topic name in the
 * {@code projects/<project_name>/topics/<topic_name>} format
 * @return topic configuration or {@code null} if topic doesn't exist
 */
public Topic getTopic(String topicName) {
	Assert.hasText(topicName, "No topic name was specified.");

	try {
		return this.topicAdminClient.getTopic(PubSubTopicUtils.toProjectTopicName(topicName, this.projectId));
	}
	catch (ApiException aex) {
		if (aex.getStatusCode().getCode() == StatusCode.Code.NOT_FOUND) {
			return null;
		}

		throw aex;
	}
}
 
Example #12
Source File: PubSubExtendedBindingsPropertiesTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Bean
public PubSubAdmin pubSubAdmin() {
	PubSubAdmin pubSubAdminMock = Mockito.mock(PubSubAdmin.class);
	when(pubSubAdminMock.createSubscription(anyString(), anyString())).thenReturn(
			Subscription.getDefaultInstance());
	when(pubSubAdminMock.getSubscription(anyString())).thenReturn(
			Subscription.getDefaultInstance());
	when(pubSubAdminMock.getTopic(anyString())).thenReturn(
			Topic.getDefaultInstance());
	return pubSubAdminMock;
}
 
Example #13
Source File: PubsubHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
public Topic createTopic(String project, String topic) throws IOException {
	deleteTopic(project, topic);
	ProjectTopicName topicName = ProjectTopicName.of(project, topic);
	TopicAdminClient adminClient = getTopicAdminClient();
	LOG.info("CreateTopic {}", topicName);
	return adminClient.createTopic(topicName);
}
 
Example #14
Source File: PublisherService.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
@Override
public void createTopic(Topic request, StreamObserver<Topic> responseObserver) {
  try {
    logger.atFine().log("Creating Topic %s", request);
    Topic topic = configurationManager.createTopic(request);
    responseObserver.onNext(topic);
    responseObserver.onCompleted();
  } catch (ConfigurationAlreadyExistsException e) {
    logger.atWarning().withCause(e).log("Topic already exists");
    responseObserver.onError(Status.ALREADY_EXISTS.withCause(e).asException());
  }
}
 
Example #15
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 #16
Source File: PublisherServiceTest.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
@Test
public void getTopic() {
  String topicName = "projects/project-1/topics/topic-1";

  GetTopicRequest request = GetTopicRequest.newBuilder().setTopic(topicName).build();
  Topic response = blockingStub.getTopic(request);
  assertThat(response.getName(), Matchers.equalTo(topicName));
}
 
Example #17
Source File: PublisherServiceTest.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
@Test
public void createTopic_topicExists() {
  expectedException.expect(StatusRuntimeException.class);
  expectedException.expectMessage(Status.ALREADY_EXISTS.getCode().toString());

  Topic request = Topic.newBuilder().setName("projects/project-1/topics/topic-1").build();

  blockingStub.createTopic(request);
}
 
Example #18
Source File: PublisherServiceTest.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
@Test
public void createTopic() {
  Topic request = Topic.newBuilder().setName("projects/project-1/topics/new-topic").build();
  Topic expected = request.toBuilder().putLabels(KAFKA_TOPIC,
          "project-1" + KAFKA_TOPIC_SEPARATOR + "new-topic").build();

  assertThat(blockingStub.createTopic(request), Matchers.equalTo(expected));
}
 
Example #19
Source File: PublisherService.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
@Override
public void getTopic(GetTopicRequest request, StreamObserver<Topic> responseObserver) {
  logger.atFine().log("Getting Topic %s", request);
  Optional<Topic> topic = configurationManager.getTopicByName(request.getTopic());
  if (!topic.isPresent()) {
    String message = request.getTopic() + " is not a valid Topic";
    logger.atWarning().log(message);
    responseObserver.onError(Status.NOT_FOUND.withDescription(message).asException());
  } else {
    responseObserver.onNext(topic.get());
    responseObserver.onCompleted();
  }
}
 
Example #20
Source File: StatisticsManager.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
@Inject
StatisticsManager(ConfigurationManager configurationManager, Clock clock) {
  this.configurationManager = configurationManager;
  this.clock = clock;

  for (String project : configurationManager.getProjects()) {
    for (Topic topic : configurationManager.getTopics(project)) {
      publishInformationByTopic.put(topic.getName(), new StatisticsInformation());
      subscriberInformationByTopic.put(topic.getName(), new StatisticsInformation());
    }
  }
}
 
Example #21
Source File: AdminService.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
private Map<String, StatisticsConsolidation> processResult(
    Function<String, StatisticsConsolidation> function) {
  return configurationManager
      .getProjects()
      .stream()
      .flatMap(project -> configurationManager.getTopics(project).stream())
      .map(Topic::getName)
      .collect(Collectors.toMap(Function.identity(), function));
}
 
Example #22
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 #23
Source File: PublisherService.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
@Override
public void publish(PublishRequest request, StreamObserver<PublishResponse> responseObserver) {
  logger.atFine().log(
      "Publishing %d messages to %s", request.getMessagesCount(), request.getTopic());
  Optional<Topic> topic = configurationManager.getTopicByName(request.getTopic());
  if (!topic.isPresent()) {
    String message = request.getTopic() + " is not a valid Topic";
    logger.atWarning().log(message);
    responseObserver.onError(Status.NOT_FOUND.withDescription(message).asException());
  } else {
    publishToKafka(request, topic.get(), responseObserver);
  }
}
 
Example #24
Source File: PubsubGrpcClient.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public void createTopic(TopicPath topic) throws IOException {
  Topic request = Topic.newBuilder().setName(topic.getPath()).build();
  publisherStub().createTopic(request); // ignore Topic result.
}
 
Example #25
Source File: TestApp.java    From gcpsamples with Apache License 2.0 4 votes vote down vote up
public TestApp() {
		try
		{

			// use env or set the path directly
			String cred_env = System.getenv("GOOGLE_APPLICATION_CREDENTIALS");
			cred_env = "/path/to/your/cert.json";
			
/*
			<!--use:
				<dependency>
				<groupId>com.google.api-client</groupId>
				<artifactId>google-api-client</artifactId>
				<version>1.23.0</version>
				</dependency>
				<dependency>
				<groupId>com.google.apis</groupId>
				<artifactId>google-api-services-oauth2</artifactId>
				<version>v2-rev114-1.22.0</version>
				</dependency>
			--> 
			HttpTransport httpTransport = new NetHttpTransport();             
			JacksonFactory jsonFactory = new JacksonFactory();

            // unset GOOGLE_APPLICATION_CREDENTIALS
            //String SERVICE_ACCOUNT_JSON_FILE = "YOUR_SERVICE_ACCOUNT_JSON_FILE.json";
            //FileInputStream inputStream = new FileInputStream(new File(SERVICE_ACCOUNT_JSON_FILE));
            //GoogleCredential credential = GoogleCredential.fromStream(inputStream, httpTransport, jsonFactory);

			// to use application default credentials and a JSON file, set the environment variable first:
            // export GOOGLE_APPLICATION_CREDENTIALS=YOUR_SERVICE_ACCOUNT_JSON_FILE.json        
            GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport,jsonFactory);

            if (credential.createScopedRequired())
                credential = credential.createScoped(Arrays.asList(Oauth2Scopes.USERINFO_EMAIL));

			Oauth2 service = new Oauth2.Builder(httpTransport, jsonFactory, credential)
			            .setApplicationName("oauth client")   
			            .build();				            
			Userinfoplus ui = service.userinfo().get().execute();
			System.out.println(ui.getEmail());
*/
/* 
          Using Google Cloud APIs with service account file
		  // You can also just export an export GOOGLE_APPLICATION_CREDENTIALS and use StorageOptions.defaultInstance().service()
		  // see: https://github.com/google/google-auth-library-java#google-auth-library-oauth2-http
		  uncomment the dependencies for google-api-client
		  
			<dependency>
				<groupId>com.google.cloud</groupId>
				<artifactId>google-cloud-storage</artifactId>
				<version>1.35.0</version>
			</dependency>

			<dependency>
				<groupId>com.google.cloud</groupId>
				<artifactId>google-cloud-pubsub</artifactId>
				<version>1.35.0</version>
			</dependency>
*/
		  
		  
		  Storage storage_service = StorageOptions.newBuilder()
			.build()
			.getService();	
		  for (Bucket b : storage_service.list().iterateAll()){
			  System.out.println(b);
		  }

		  //GoogleCredentials creds = GoogleCredentials.fromStream(new FileInputStream(cred_env));	
		  GoogleCredentials creds = GoogleCredentials.getApplicationDefault();	  	  
		  FixedCredentialsProvider credentialsProvider = FixedCredentialsProvider.create(creds);
		  
		  ///ManagedChannel channel = ManagedChannelBuilder.forTarget("pubsub.googleapis.com:443").build();
          //TransportChannelProvider channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));	
		  
		  TransportChannelProvider channelProvider = TopicAdminSettings.defaultTransportChannelProvider();

		  TopicAdminClient topicClient =
			  TopicAdminClient.create(
				  TopicAdminSettings.newBuilder()
					  .setTransportChannelProvider(channelProvider)
					  .setCredentialsProvider(credentialsProvider)
					  .build());

		  ListTopicsRequest listTopicsRequest =
							ListTopicsRequest.newBuilder()
								.setProject(ProjectName.format("your_project"))
								.build();
		  ListTopicsPagedResponse response = topicClient.listTopics(listTopicsRequest);
		  Iterable<Topic> topics = response.iterateAll();
		  for (Topic topic : topics) 
			 System.out.println(topic);
		 

		} 
		catch (Exception ex) {
			System.out.println("Error:  " + ex);
		}
	}
 
Example #26
Source File: TestApp.java    From gcpsamples with Apache License 2.0 4 votes vote down vote up
public TestApp() {
	try
	{
		/*
		// For GoogleAPIs
		HttpTransport httpTransport = new NetHttpTransport();             
		JacksonFactory jsonFactory = new JacksonFactory();
		//ComputeCredential credential = new ComputeCredential.Builder(httpTransport, jsonFactory).build();	
		GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport,jsonFactory);				            
		if (credential.createScopedRequired())
		    credential = credential.createScoped(Arrays.asList(Oauth2Scopes.USERINFO_EMAIL));           				            
		Oauth2 service = new Oauth2.Builder(httpTransport, jsonFactory, credential)
		            .setApplicationName("oauth client")   
		            .build();				            
		Userinfoplus ui = service.userinfo().get().execute();
		System.out.println(ui.getEmail());
		*/

         // Using Google Cloud APIs
	  Storage storage_service = StorageOptions.newBuilder()
		.build()
		.getService();	
	  for (Bucket b : storage_service.list().iterateAll()){
		  System.out.println(b);
	  }

         // String cred_file = "/path/to/cred.json";
	  //GoogleCredentials creds = GoogleCredentials.fromStream(new FileInputStream(cred_file));	
	  GoogleCredentials creds = GoogleCredentials.getApplicationDefault();	  	  
	  FixedCredentialsProvider credentialsProvider = FixedCredentialsProvider.create(creds);
	  
	  ///ManagedChannel channel = ManagedChannelBuilder.forTarget("pubsub.googleapis.com:443").build();
         //TransportChannelProvider channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));	
	  
	  TransportChannelProvider channelProvider = TopicAdminSettings.defaultTransportChannelProvider();

	  TopicAdminClient topicClient =
		  TopicAdminClient.create(
			  TopicAdminSettings.newBuilder()
				  .setTransportChannelProvider(channelProvider)
				  .setCredentialsProvider(credentialsProvider)
				  .build());

	  ListTopicsRequest listTopicsRequest =
						ListTopicsRequest.newBuilder()
							.setProject(ProjectName.format("your_project"))
							.build();
	  ListTopicsPagedResponse response = topicClient.listTopics(listTopicsRequest);
	  Iterable<Topic> topics = response.iterateAll();
	  for (Topic topic : topics) 
		 System.out.println(topic);
	 		  

	} 
	catch (Exception ex) {
		System.out.println("Error:  " + ex);
	}
}
 
Example #27
Source File: TestApp.java    From gcpsamples with Apache License 2.0 4 votes vote down vote up
public TestApp()  {

    String projectId = ServiceOptions.getDefaultProjectId();
	try {

		//export GRPC_PROXY_EXP=localhost:3128
		HttpHost proxy = new HttpHost("127.0.0.1",3128);
		DefaultHttpClient httpClient = new DefaultHttpClient();
		httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
					
		httpClient.addRequestInterceptor(new HttpRequestInterceptor(){            
			@Override
			public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException {
					//if (request.getRequestLine().getMethod().equals("CONNECT"))                 
					//   request.addHeader(new BasicHeader("Proxy-Authorization","Basic dXNlcjE6dXNlcjE="));
				}
			});
		
		mHttpTransport =  new ApacheHttpTransport(httpClient);		

		HttpTransportFactory hf = new HttpTransportFactory(){
			@Override
			public HttpTransport create() {
				return mHttpTransport;
			}
		};            
		
		credential = GoogleCredentials.getApplicationDefault(hf);

		CredentialsProvider credentialsProvider =  new GoogleCredentialsProvider(){
			public List<String> getScopesToApply(){
				return Arrays.asList("https://www.googleapis.com/auth/pubsub");
			   }

			public Credentials getCredentials()  {
				return credential;
			}
		};

		TopicAdminSettings topicAdminSettings =
		     TopicAdminSettings.newBuilder().setCredentialsProvider(credentialsProvider)
				 .build();
				 
		 TopicAdminClient topicAdminClient =
		     TopicAdminClient.create(topicAdminSettings);
		
		//TopicAdminClient topicAdminClient = TopicAdminClient.create();
		ProjectName project = ProjectName.create(projectId);
		for (Topic element : topicAdminClient.listTopics(project).iterateAll()) 
	  		System.out.println(element.getName());
	
	} catch (Exception ex) 
	{
		System.out.println("ERROR " + ex);
	}
  }
 
Example #28
Source File: PublisherService.java    From kafka-pubsub-emulator with Apache License 2.0 4 votes vote down vote up
private void publishToKafka(
    PublishRequest request, Topic topic, StreamObserver<PublishResponse> responseObserver) {
  Instant start = Instant.now();
  String kafkaTopic =
      topic.getLabelsOrDefault(KAFKA_TOPIC, ProjectTopicName.parse(topic.getName()).getTopic());
  int producerIndex = nextProducerIndex.getAndUpdate((value) -> ++value % kafkaProducers.size());
  Producer<String, ByteBuffer> producer = kafkaProducers.get(producerIndex);

  CountDownLatch callbacks = new CountDownLatch(request.getMessagesCount());
  AtomicInteger failures = new AtomicInteger();
  PublishResponse.Builder builder = PublishResponse.newBuilder();
  request
      .getMessagesList()
      .forEach(
          m -> {
            ProducerRecord<String, ByteBuffer> producerRecord =
                buildProducerRecord(kafkaTopic, m);
            long publishedAt = System.currentTimeMillis();
            producer.send(
                producerRecord,
                (recordMetadata, exception) -> {
                  if (recordMetadata != null) {
                    builder.addMessageIds(
                        recordMetadata.partition() + "-" + recordMetadata.offset());
                    statisticsManager.computePublish(topic.getName(), m.getData(), publishedAt);
                  } else {
                    logger.atSevere().withCause(exception).log("Unable to Publish message");
                    statisticsManager.computePublishError(topic.getName());
                    failures.incrementAndGet();
                  }
                  callbacks.countDown();
                });
          });

  try {
    if (!callbacks.await(MAX_PUBLISH_WAIT, TimeUnit.SECONDS)) {
      logger.atWarning().log(
          "%d callbacks remain after %ds", callbacks.getCount(), MAX_PUBLISH_WAIT);
    }

    logger.atFine().log(
        "Published %d of %d messages to %s using KafkaProducer %d in %dms",
        builder.getMessageIdsCount(),
        request.getMessagesCount(),
        kafkaTopic,
        producerIndex,
        Duration.between(start, Instant.now()).toMillis());
    if (failures.get() == 0) {
      responseObserver.onNext(builder.build());
      responseObserver.onCompleted();
    } else {
      String message =
          failures.get() + " of " + request.getMessagesCount() + " Messages failed to Publish";
      logger.atWarning().log(message);
      responseObserver.onError(Status.INTERNAL.withDescription(message).asException());
    }
  } catch (InterruptedException e) {
    responseObserver.onError(Status.INTERNAL.withCause(e).asException());
  }
}
 
Example #29
Source File: PublisherServiceTest.java    From kafka-pubsub-emulator with Apache License 2.0 4 votes vote down vote up
@Test
public void publish_implicitKafkaTopic() {
  blockingStub.createTopic(
      Topic.newBuilder().setName("projects/project-1/topics/implicit-kafka-topic").build());

  int messages = 5;
  PublishRequest request =
      PublishRequest.newBuilder()
          .setTopic("projects/project-1/topics/implicit-kafka-topic")
          .addAllMessages(generatePubsubMessages(messages))
          .build();

  MockProducer<String, ByteBuffer> mockProducer = startPublishExecutor(messages);

  PublishResponse response = blockingStub.publish(request);
  List<String> topics = new ArrayList<>();
  List<String> data = new ArrayList<>();
  for (ProducerRecord<String, ByteBuffer> producerRecord : mockProducer.history()) {
    topics.add(producerRecord.topic());
    data.add(UTF_8.decode(producerRecord.value()).toString());
  }

  assertThat(response.getMessageIdsList(), Matchers.contains("0-0", "0-1", "0-2", "0-3", "0-4"));
  assertThat(
      topics,
      Matchers.contains(
          "project-1" + KAFKA_TOPIC_SEPARATOR + "implicit-kafka-topic",
          "project-1" + KAFKA_TOPIC_SEPARATOR + "implicit-kafka-topic",
          "project-1" + KAFKA_TOPIC_SEPARATOR + "implicit-kafka-topic",
          "project-1" + KAFKA_TOPIC_SEPARATOR + "implicit-kafka-topic",
          "project-1" + KAFKA_TOPIC_SEPARATOR + "implicit-kafka-topic"));
  assertThat(
      data, Matchers.contains("message-0", "message-1", "message-2", "message-3", "message-4"));

  verify(statisticsManager, times(5))
      .computePublish(
          eq("projects/project-1/topics/implicit-kafka-topic"),
          argThat(message -> message.toStringUtf8().matches(MESSAGE_CONTENT_REGEX)),
          anyLong());
  verify(statisticsManager, never()).computePublishError(anyString());
}
 
Example #30
Source File: PubSubSampleApplicationTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
private static List<String> getTopicNamesFromProject() {
	ListTopicsPagedResponse listTopicsResponse = topicAdminClient.listTopics("projects/" + projectName);
	return StreamSupport.stream(listTopicsResponse.iterateAll().spliterator(), false)
			.map(Topic::getName)
			.collect(Collectors.toList());
}