Java Code Examples for com.google.pubsub.v1.ProjectSubscriptionName#format()

The following examples show how to use com.google.pubsub.v1.ProjectSubscriptionName#format() . 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: PubsubHelper.java    From flink with Apache License 2.0 6 votes vote down vote up
public List<ReceivedMessage> pullMessages(String projectId, String subscriptionId, int maxNumberOfMessages) throws Exception {
	SubscriberStubSettings subscriberStubSettings =
		SubscriberStubSettings.newBuilder()
			.setTransportChannelProvider(channelProvider)
			.setCredentialsProvider(NoCredentialsProvider.create())
			.build();
	try (SubscriberStub subscriber = GrpcSubscriberStub.create(subscriberStubSettings)) {
		String subscriptionName = ProjectSubscriptionName.format(projectId, subscriptionId);
		PullRequest pullRequest =
			PullRequest.newBuilder()
				.setMaxMessages(maxNumberOfMessages)
				.setReturnImmediately(false)
				.setSubscription(subscriptionName)
				.build();

		List<ReceivedMessage> receivedMessages = subscriber.pullCallable().call(pullRequest).getReceivedMessagesList();
		acknowledgeIds(subscriber, subscriptionName, receivedMessages);
		return receivedMessages;
	}
}
 
Example 2
Source File: PubSubSource.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Actually build the desired instance of the PubSubSourceBuilder.
 *
 * @return a brand new SourceFunction
 * @throws IOException              in case of a problem getting the credentials
 * @throws IllegalArgumentException in case required fields were not specified.
 */
public PubSubSource<OUT> build() throws IOException {
	if (credentials == null) {
		credentials = defaultCredentialsProviderBuilder().build().getCredentials();
	}

	if (pubSubSubscriberFactory == null) {
		pubSubSubscriberFactory = new DefaultPubSubSubscriberFactory(ProjectSubscriptionName.format(projectName, subscriptionName),
																	3,
																	Duration.ofSeconds(15),
																	100);
	}

	return new PubSubSource<>(deserializationSchema, pubSubSubscriberFactory, credentials, maxMessageToAcknowledge, new AcknowledgeOnCheckpointFactory());
}
 
Example 3
Source File: PubSubSubscriberFactoryForEmulator.java    From flink with Apache License 2.0 5 votes vote down vote up
public PubSubSubscriberFactoryForEmulator(String hostAndPort, String project, String subscription, int retries, Duration timeout, int maxMessagesPerPull) {
	this.hostAndPort = hostAndPort;
	this.retries = retries;
	this.timeout = timeout;
	this.maxMessagesPerPull = maxMessagesPerPull;
	this.projectSubscriptionName = ProjectSubscriptionName.format(project, subscription);
}
 
Example 4
Source File: PubsubHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
public List<ReceivedMessage> pullMessages(String projectId, String subscriptionId, int maxNumberOfMessages) throws Exception {
	SubscriberStubSettings subscriberStubSettings =
		SubscriberStubSettings.newBuilder()
			.setTransportChannelProvider(channelProvider)
			.setCredentialsProvider(NoCredentialsProvider.create())
			.build();
	try (SubscriberStub subscriber = GrpcSubscriberStub.create(subscriberStubSettings)) {
		// String projectId = "my-project-id";
		// String subscriptionId = "my-subscription-id";
		// int numOfMessages = 10;   // max number of messages to be pulled
		String subscriptionName = ProjectSubscriptionName.format(projectId, subscriptionId);
		PullRequest pullRequest =
			PullRequest.newBuilder()
				.setMaxMessages(maxNumberOfMessages)
				.setReturnImmediately(false) // return immediately if messages are not available
				.setSubscription(subscriptionName)
				.build();

		// use pullCallable().futureCall to asynchronously perform this operation
		PullResponse pullResponse = subscriber.pullCallable().call(pullRequest);
		List<String> ackIds = new ArrayList<>();
		for (ReceivedMessage message : pullResponse.getReceivedMessagesList()) {
			// handle received message
			// ...
			ackIds.add(message.getAckId());
		}
		// acknowledge received messages
		AcknowledgeRequest acknowledgeRequest =
			AcknowledgeRequest.newBuilder()
				.setSubscription(subscriptionName)
				.addAllAckIds(ackIds)
				.build();
		// use acknowledgeCallable().futureCall to asynchronously perform this operation
		subscriber.acknowledgeCallable().call(acknowledgeRequest);
		return pullResponse.getReceivedMessagesList();
	}
}
 
Example 5
Source File: PubSubSampleApplicationTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
private List<String> getMessagesFromSubscription(String subscriptionName) {
	String projectSubscriptionName = ProjectSubscriptionName.format(
			projectName, subscriptionName);

	PullRequest pullRequest = PullRequest.newBuilder()
			.setReturnImmediately(true)
			.setMaxMessages(10)
			.setSubscription(projectSubscriptionName)
			.build();

	PullResponse pullResponse = subscriptionAdminClient.getStub().pullCallable().call(pullRequest);
	return pullResponse.getReceivedMessagesList().stream()
			.map((message) -> message.getMessage().getData().toStringUtf8())
			.collect(Collectors.toList());
}
 
Example 6
Source File: PubSubSampleApplicationTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
private void createSubscription(String subscriptionName, String topicName) {
	String url = UriComponentsBuilder.fromHttpUrl(this.appUrl + "/createSubscription")
			.queryParam("topicName", topicName)
			.queryParam("subscriptionName", subscriptionName)
			.toUriString();
	this.testRestTemplate.postForEntity(url, null, String.class);

	String projectSubscriptionName = ProjectSubscriptionName.format(projectName, subscriptionName);
	await().atMost(PUBSUB_CLIENT_TIMEOUT_SECONDS, TimeUnit.SECONDS).untilAsserted(
			() -> {
				List<String> subscriptions = getSubscriptionNamesFromProject();
				assertThat(subscriptions).contains(projectSubscriptionName);
			});
}
 
Example 7
Source File: PubSubSampleApplicationTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
private void deleteSubscription(String subscriptionName) {
	String url = UriComponentsBuilder.fromHttpUrl(this.appUrl + "/deleteSubscription")
			.queryParam("subscription", subscriptionName)
			.toUriString();
	this.testRestTemplate.postForEntity(url, null, String.class);

	String projectSubscriptionName = ProjectSubscriptionName.format(projectName, subscriptionName);
	await().atMost(PUBSUB_CLIENT_TIMEOUT_SECONDS, TimeUnit.SECONDS).untilAsserted(
			() -> {
				List<String> subscriptions = getSubscriptionNamesFromProject();
				assertThat(subscriptions).doesNotContain(projectSubscriptionName);
			});
}
 
Example 8
Source File: PubSubSampleApplicationTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
private static void deleteSubscriptions(String... testSubscriptions) {
	for (String testSubscription : testSubscriptions) {
		String testSubscriptionName = ProjectSubscriptionName.format(
				projectName, testSubscription);
		List<String> projectSubscriptions = getSubscriptionNamesFromProject();
		if (projectSubscriptions.contains(testSubscriptionName)) {
			subscriptionAdminClient.deleteSubscription(testSubscriptionName);
		}
	}
}
 
Example 9
Source File: PubSubSource.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Actually build the desired instance of the PubSubSourceBuilder.
 *
 * @return a brand new SourceFunction
 * @throws IOException              in case of a problem getting the credentials
 * @throws IllegalArgumentException in case required fields were not specified.
 */
public PubSubSource<OUT> build() throws IOException {
	if (credentials == null) {
		credentials = defaultCredentialsProviderBuilder().build().getCredentials();
	}

	if (pubSubSubscriberFactory == null) {
		pubSubSubscriberFactory = new DefaultPubSubSubscriberFactory(ProjectSubscriptionName.format(projectName, subscriptionName),
																	3,
																	Duration.ofSeconds(15),
																	100);
	}

	return new PubSubSource<>(deserializationSchema, pubSubSubscriberFactory, credentials, new AcknowledgeOnCheckpointFactory(), new GuavaFlinkConnectorRateLimiter(), messagePerSecondRateLimit);
}
 
Example 10
Source File: PubSubSubscriberFactoryForEmulator.java    From flink with Apache License 2.0 5 votes vote down vote up
public PubSubSubscriberFactoryForEmulator(String hostAndPort, String project, String subscription, int retries, Duration timeout, int maxMessagesPerPull) {
	this.hostAndPort = hostAndPort;
	this.retries = retries;
	this.timeout = timeout;
	this.maxMessagesPerPull = maxMessagesPerPull;
	this.projectSubscriptionName = ProjectSubscriptionName.format(project, subscription);
}
 
Example 11
Source File: Poller.java    From spanner-event-exporter with Apache License 2.0 4 votes vote down vote up
private String getLastProcessedTimestamp() {

    String timestamp = "";
    try {
      final SubscriberStubSettings subscriberStubSettings =
          SubscriberStubSettings.newBuilder()
              .setTransportChannelProvider(
                  SubscriberStubSettings.defaultGrpcTransportProviderBuilder()
                      .setMaxInboundMessageSize(20 << 20) // 20MB
                      .build())
              .build();

      try (SubscriberStub subscriber = GrpcSubscriberStub.create(subscriberStubSettings)) {
        final String subscriptionName = ProjectSubscriptionName.format(PROJECT_ID, tableName);
        final PullRequest pullRequest =
            PullRequest.newBuilder()
                .setMaxMessages(1)
                .setReturnImmediately(true)
                .setSubscription(subscriptionName)
                .build();

        final PullResponse pullResponse = subscriber.pullCallable().call(pullRequest);
        final DatumReader<GenericRecord> datumReader =
            new GenericDatumReader<GenericRecord>(avroSchema);

        for (ReceivedMessage message : pullResponse.getReceivedMessagesList()) {
          final JsonDecoder decoder =
              DecoderFactory.get()
                  .jsonDecoder(avroSchema, message.getMessage().getData().newInput());

          final GenericRecord record = datumReader.read(null, decoder);
          timestamp = record.get("Timestamp").toString();

          log.debug("---------------- Got Timestamp: " + timestamp);
        }
      }
    } catch (IOException e) {
      log.error("Could not get last processed timestamp from pub / sub", e);

      // If we cannot find a previously processed timestamp, we will default
      // to the one present in the config file.
      return startingTimestamp;
    }

    return timestamp;
  }
 
Example 12
Source File: PubSubSource.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * There is a default PubSubSubscriber factory that uses gRPC to pull in PubSub messages. This method can be used to tune this default factory.
       * Note this will not work in combination with a custom PubSubSubscriber factory.
 * @param maxMessagesPerPull the number of messages pulled per request. Default: 100
 * @param perRequestTimeout the timeout per request. Default: 15 seconds
 * @param retries the number of retries when requests fail
 * @return The current PubSubSourceBuilder instance
 */
public PubSubSourceBuilder<OUT> withPubSubSubscriberFactory(int maxMessagesPerPull, Duration perRequestTimeout, int retries) {
	this.pubSubSubscriberFactory = new DefaultPubSubSubscriberFactory(ProjectSubscriptionName.format(projectName, subscriptionName),
																	retries,
																	perRequestTimeout,
																	maxMessagesPerPull);
	return this;
}
 
Example 13
Source File: PubSubSource.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * There is a default PubSubSubscriber factory that uses gRPC to pull in PubSub messages. This method can be used to tune this default factory.
       * Note this will not work in combination with a custom PubSubSubscriber factory.
 * @param maxMessagesPerPull the number of messages pulled per request. Default: 100
 * @param perRequestTimeout the timeout per request. Default: 15 seconds
 * @param retries the number of retries when requests fail
 * @return The current PubSubSourceBuilder instance
 */
public PubSubSourceBuilder<OUT> withPubSubSubscriberFactory(int maxMessagesPerPull, Duration perRequestTimeout, int retries) {
	this.pubSubSubscriberFactory = new DefaultPubSubSubscriberFactory(ProjectSubscriptionName.format(projectName, subscriptionName),
																	retries,
																	perRequestTimeout,
																	maxMessagesPerPull);
	return this;
}