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

The following examples show how to use com.google.pubsub.v1.ProjectSubscriptionName#parse() . 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: ConfigurationManager.java    From kafka-pubsub-emulator with Apache License 2.0 6 votes vote down vote up
/** Updates the managed Server when a Subscription is deleted. */
public final void deleteSubscription(String subscriptionName)
    throws ConfigurationNotFoundException {
  ProjectSubscriptionName projectSubscriptionName =
      ProjectSubscriptionName.parse(subscriptionName);
  ProjectName projectName = ProjectName.of(projectSubscriptionName.getProject());

  Set<com.google.pubsub.v1.Subscription> subscriptions =
      subscriptionsByProject.get(projectName.toString());
  getSubscriptionByName(subscriptionName)
      .map(subscriptions::remove)
      .orElseThrow(
          () ->
              new ConfigurationNotFoundException(
                  "Subscription " + projectSubscriptionName.toString() + " does not exist"));
  pubSubRepository.save(getPubSub());
}
 
Example 2
Source File: PubSubSubscriptionUtils.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
/**
 * Create a {@link ProjectSubscriptionName} based on a subscription name within a project or the
 * fully-qualified subscription name. If the specified subscription is in the
 * {@code projects/<project_name>/subscriptions/<subscription_name>} format, then the {@code projectId} is
 * ignored}
 * @param subscription the subscription name in the project or the fully-qualified project name
 * @param projectId the project ID to use if the subscription is not a fully-qualified name
 * @return the Pub/Sub object representing the subscription name
 */
public static ProjectSubscriptionName toProjectSubscriptionName(String subscription, @Nullable String projectId) {
	Assert.notNull(subscription, "The subscription can't be null.");

	ProjectSubscriptionName projectSubscriptionName = null;

	if (ProjectSubscriptionName.isParsableFrom(subscription)) {
		// Fully-qualified subscription name in the "projects/<project_name>/subscriptions/<subscription_name>" format
		projectSubscriptionName = ProjectSubscriptionName.parse(subscription);
	}
	else {
		Assert.notNull(projectId, "The project ID can't be null when using canonical subscription name.");
		projectSubscriptionName = ProjectSubscriptionName.of(projectId, subscription);
	}

	return projectSubscriptionName;
}
 
Example 3
Source File: ConfigurationManager.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
/** Updates the managed Server when a new Subscription is created. */
public final com.google.pubsub.v1.Subscription createSubscription(
    com.google.pubsub.v1.Subscription subscription)
    throws ConfigurationAlreadyExistsException, ConfigurationNotFoundException {
  ProjectTopicName projectTopicName = ProjectTopicName.parse(subscription.getTopic());
  ProjectSubscriptionName projectSubscriptionName =
      ProjectSubscriptionName.parse(subscription.getName());

  if (getSubscriptionByName(subscription.getName()).isPresent()) {
    throw new ConfigurationAlreadyExistsException(
        "Subscription " + projectSubscriptionName.toString() + " already exists");
  }
  com.google.pubsub.v1.Topic topic =
      getTopicByName(projectTopicName.toString())
          .orElseThrow(
              () ->
                  new ConfigurationNotFoundException(
                      "Topic " + projectTopicName.toString() + " does not exist"));
  com.google.pubsub.v1.Subscription.Builder builder =
      subscription.toBuilder().putLabels(KAFKA_TOPIC, topic.getLabelsOrThrow(KAFKA_TOPIC));
  if (subscription.getAckDeadlineSeconds() == 0) {
    builder.setAckDeadlineSeconds(10);
  }
  builder.setPushConfig(PushConfig.getDefaultInstance()).build();
  com.google.pubsub.v1.Subscription built = builder.build();
  subscriptionsByProject.put(
      ProjectName.of(projectSubscriptionName.getProject()).toString(), built);
  pubSubRepository.save(getPubSub());
  return built;
}
 
Example 4
Source File: Pubsub.java    From gcp-ingestion with Mozilla Public License 2.0 5 votes vote down vote up
/** Constructor. */
public <T> Read(String subscriptionName, Function<PubsubMessage, CompletableFuture<T>> output,
    Function<Subscriber.Builder, Subscriber.Builder> config,
    Function<PubsubMessage, PubsubMessage> decompress) {
  ProjectSubscriptionName subscription = ProjectSubscriptionName.parse(subscriptionName);
  subscriber = config.apply(Subscriber.newBuilder(subscription,
      // Synchronous CompletableFuture methods are executed by the thread that completes the
      // future, or the current thread if the future is already complete. Use that here to
      // minimize memory usage by doing as much work as immediately possible.
      (message, consumer) -> CompletableFuture.completedFuture(message).thenApply(decompress)
          .thenCompose(output).whenComplete((result, exception) -> {
            if (exception == null) {
              consumer.ack();
            } else {
              // exception is always a CompletionException caused by another exception
              if (exception.getCause() instanceof BatchException) {
                // only log batch exception once
                ((BatchException) exception.getCause()).handle((batchExc) -> LOG.error(
                    String.format("failed to deliver %d messages", batchExc.size),
                    batchExc.getCause()));
              } else {
                // log exception specific to this message
                LOG.error("failed to deliver message", exception.getCause());
              }
              consumer.nack();
            }
          })))
      .build();
}