Java Code Examples for com.google.cloud.pubsub.v1.stub.GrpcSubscriberStub#create()

The following examples show how to use com.google.cloud.pubsub.v1.stub.GrpcSubscriberStub#create() . 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: CloudPubSubGRPCSubscriber.java    From pubsub with Apache License 2.0 6 votes vote down vote up
private void makeSubscriber() {
  try {
    log.info("Creating subscriber.");
    SubscriberStubSettings subscriberStubSettings =
    SubscriberStubSettings.newBuilder()
      .setTransportChannelProvider(
          SubscriberStubSettings.defaultGrpcTransportProviderBuilder()
              .setMaxInboundMessageSize(20 << 20) // 20MB
              .build())
      .setCredentialsProvider(gcpCredentialsProvider)
      .build();
    subscriber = GrpcSubscriberStub.create(subscriberStubSettings);
    // We change the subscriber every 25 - 35 minutes in order to avoid GOAWAY errors.
    nextSubscriberResetTime =
        System.currentTimeMillis() + rand.nextInt(10 * 60 * 1000) + 25 * 60 * 1000;
  } catch (IOException e) {
    throw new RuntimeException("Could not create subscriber stub; no subscribing can occur.", e);
  }
}
 
Example 2
Source File: CloudPubSubSourceConnector.java    From pubsub with Apache License 2.0 6 votes vote down vote up
/**
 * Check whether the user provided Cloud Pub/Sub subscription name specified by {@link
 * #CPS_SUBSCRIPTION_CONFIG} exists or not.
 */
@VisibleForTesting
public void verifySubscription(String cpsProject, String cpsSubscription, CredentialsProvider credentialsProvider) {
  try {
    SubscriberStubSettings subscriberStubSettings =
    SubscriberStubSettings.newBuilder()
      .setTransportChannelProvider(
          SubscriberStubSettings.defaultGrpcTransportProviderBuilder()
              .setMaxInboundMessageSize(20 << 20) // 20MB
              .build())
      .setCredentialsProvider(credentialsProvider)
      .build();
    GrpcSubscriberStub stub = GrpcSubscriberStub.create(subscriberStubSettings);
    GetSubscriptionRequest request =
        GetSubscriptionRequest.newBuilder()
            .setSubscription(
                String.format(
                    ConnectorUtils.CPS_SUBSCRIPTION_FORMAT, cpsProject, cpsSubscription))
            .build();
    stub.getSubscriptionCallable().call(request);
  } catch (Exception e) {
    throw new ConnectException(
        "Error verifying the subscription " + cpsSubscription + " for project " + cpsProject, e);
  }
}
 
Example 3
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 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: DefaultSubscriberFactory.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Override
public SubscriberStub createSubscriberStub() {
	SubscriberStubSettings.Builder subscriberStubSettings = SubscriberStubSettings.newBuilder();

	if (this.credentialsProvider != null) {
		subscriberStubSettings.setCredentialsProvider(this.credentialsProvider);
	}

	if (this.pullEndpoint != null) {
		subscriberStubSettings.setEndpoint(this.pullEndpoint);
	}

	if (this.executorProvider != null) {
		subscriberStubSettings.setExecutorProvider(this.executorProvider);
	}

	if (this.headerProvider != null) {
		subscriberStubSettings.setHeaderProvider(this.headerProvider);
	}

	if (this.channelProvider != null) {
		subscriberStubSettings.setTransportChannelProvider(this.channelProvider);
	}

	if (this.apiClock != null) {
		subscriberStubSettings.setClock(this.apiClock);
	}

	if (this.subscriberStubRetrySettings != null) {
		subscriberStubSettings.pullSettings().setRetrySettings(
				this.subscriberStubRetrySettings);
	}

	try {
		return GrpcSubscriberStub.create(subscriberStubSettings.build());
	}
	catch (IOException ex) {
		throw new RuntimeException("Error creating the SubscriberStub", ex);
	}
}
 
Example 6
Source File: ConsumeGCPubSub.java    From nifi with Apache License 2.0 5 votes vote down vote up
private SubscriberStub getSubscriber(ProcessContext context) throws IOException {

        final SubscriberStubSettings subscriberStubSettings = SubscriberStubSettings.newBuilder()
                .setCredentialsProvider(FixedCredentialsProvider.create(getGoogleCredentials(context)))
                .build();

        return GrpcSubscriberStub.create(subscriberStubSettings);
    }
 
Example 7
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;
  }