com.google.cloud.pubsub.v1.SubscriptionAdminClient Java Examples

The following examples show how to use com.google.cloud.pubsub.v1.SubscriptionAdminClient. 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 void createSubscription(String subscriptionProject, String subscription, String topicProject, String topic) throws IOException {
	ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.newBuilder()
		.setProject(subscriptionProject)
		.setSubscription(subscription)
		.build();

	deleteSubscription(subscriptionName);

	SubscriptionAdminClient adminClient = getSubscriptionAdminClient();

	ProjectTopicName topicName = ProjectTopicName.of(topicProject, topic);

	PushConfig pushConfig = PushConfig.getDefaultInstance();

	LOG.info("CreateSubscription {}", subscriptionName);
	getSubscriptionAdminClient().createSubscription(subscriptionName, topicName, pushConfig, 1);
}
 
Example #2
Source File: PubSubConnector.java    From smallrye-reactive-messaging with Apache License 2.0 6 votes vote down vote up
private void createSubscription(final PubSubConfig config) {
    final SubscriptionAdminClient subscriptionAdminClient = pubSubManager.subscriptionAdminClient(config);

    final ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(config.getProjectId(),
            config.getSubscription());

    try {
        subscriptionAdminClient.getSubscription(subscriptionName);
    } catch (final NotFoundException e) {
        final PushConfig pushConfig = PushConfig.newBuilder()
                .build();

        final ProjectTopicName topicName = ProjectTopicName.of(config.getProjectId(), config.getTopic());

        subscriptionAdminClient.createSubscription(subscriptionName, topicName, pushConfig, 0);
    }
}
 
Example #3
Source File: PubSubClient.java    From daq with Apache License 2.0 6 votes vote down vote up
private void resetSubscription(ProjectTopicName topicName, ProjectSubscriptionName subscriptionName) {
  try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
    if (subscriptionExists(subscriptionAdminClient, topicName, subscriptionName)) {
      System.out.println("Resetting existing subscription " + subscriptionName);
      subscriptionAdminClient.seek(getCurrentTimeSeekRequest(subscriptionName.toString()));
      Thread.sleep(SUBSCRIPTION_RACE_DELAY_MS);
    } else {
      System.out.println("Creating new subscription " + subscriptionName);
      subscriptionAdminClient.createSubscription(
          subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
    }
  } catch (Exception e) {
    throw new RuntimeException(
        String.format(REFRESH_ERROR_FORMAT, topicName, subscriptionName), e);
  }
}
 
Example #4
Source File: GcpPubSubAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public SubscriptionAdminClient subscriptionAdminClient(
		TransportChannelProvider transportChannelProvider) {
	try {
		return SubscriptionAdminClient.create(
				SubscriptionAdminSettings.newBuilder()
						.setCredentialsProvider(this.finalCredentialsProvider)
						.setHeaderProvider(this.headerProvider)
						.setTransportChannelProvider(transportChannelProvider)
						.build());
	}
	catch (IOException ioe) {
		throw new PubSubException("An error occurred while creating SubscriptionAdminClient.", ioe);
	}
}
 
Example #5
Source File: PubsubHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
public void deleteSubscription(ProjectSubscriptionName subscriptionName) throws IOException {
	SubscriptionAdminClient adminClient = getSubscriptionAdminClient();
	try {
		adminClient.getSubscription(subscriptionName);
		// If it already exists we must first delete it.
		LOG.info("DeleteSubscription {}", subscriptionName);
		adminClient.deleteSubscription(subscriptionName);
	} catch (NotFoundException e) {
		// Doesn't exist. Good.
	}
}
 
Example #6
Source File: PubsubHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
public SubscriptionAdminClient getSubscriptionAdminClient() throws IOException {
	if (subscriptionAdminClient == null) {
		SubscriptionAdminSettings subscriptionAdminSettings =
			SubscriptionAdminSettings
				.newBuilder()
				.setTransportChannelProvider(channelProvider)
				.setCredentialsProvider(NoCredentialsProvider.create())
				.build();
		subscriptionAdminClient = SubscriptionAdminClient.create(subscriptionAdminSettings);
	}
	return subscriptionAdminClient;
}
 
Example #7
Source File: PubsubHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
public SubscriptionAdminClient getSubscriptionAdminClient() throws IOException {
	if (subscriptionAdminClient == null) {
		SubscriptionAdminSettings subscriptionAdminSettings =
			SubscriptionAdminSettings
				.newBuilder()
				.setTransportChannelProvider(channelProvider)
				.setCredentialsProvider(NoCredentialsProvider.create())
				.build();
		subscriptionAdminClient = SubscriptionAdminClient.create(subscriptionAdminSettings);
	}
	return subscriptionAdminClient;
}
 
Example #8
Source File: RiskAnalysisTests.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Before
public void before() throws Exception {
  // Create a new topic
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.createTopic(topicName);
  }
  // Create a new subscription
  try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
    subscriptionAdminClient
        .createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
  }
}
 
Example #9
Source File: InspectTests.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Before
public void before() throws Exception {
  // Create a new topic
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.createTopic(topicName);
  }

  // Create a new subscription
  try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
    subscriptionAdminClient
        .createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
  }
}
 
Example #10
Source File: SamplesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@AfterClass
public static void tearDownClass() {
  try {
    SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create();
    ProjectSubscriptionName subName = ProjectSubscriptionName.of(PROJECT_ID, subId);
    subscriptionAdminClient.deleteSubscription(subName);
    subscriptionAdminClient.shutdownNow();
  } catch (Exception e) {
    // these exceptions aren't relevant to the tests
    System.out.println("TearDownClass Error: " + e.toString());
  }
}
 
Example #11
Source File: Subscriptions.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static Subscription createOccurrenceSubscription(String subId, String projectId) 
    throws IOException, StatusRuntimeException, InterruptedException {
  // This topic id will automatically receive messages when Occurrences are added or modified
  String topicId = "container-analysis-occurrences-v1";
  ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
  ProjectSubscriptionName subName = ProjectSubscriptionName.of(projectId, subId);

  SubscriptionAdminClient client = SubscriptionAdminClient.create();
  PushConfig config = PushConfig.getDefaultInstance();
  Subscription sub = client.createSubscription(subName, topicName, config, 0);
  return sub;
}
 
Example #12
Source File: Connection.java    From heroic with Apache License 2.0 5 votes vote down vote up
void createSubscription() throws IOException {
    log.info("Creating subscription {} for topic {}", subscriptionName, topicName);
    SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create(
        SubscriptionAdminSettings.newBuilder()
          .setTransportChannelProvider(channelProvider)
          .setCredentialsProvider(credentialsProvider)
          .build()
    );
    try {
        subscriptionAdminClient.createSubscription(
            subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
    } catch (AlreadyExistsException e) {
        log.info("Subscription already exists");
    }
}
 
Example #13
Source File: EmulatorHelper.java    From heroic with Apache License 2.0 5 votes vote down vote up
public static void deleteSubscription(
    String projectId, String subscriptionId, String endpoint
) throws IOException {
    SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create(
        SubscriptionAdminSettings.newBuilder()
            .setTransportChannelProvider(channelProvider(endpoint))
            .setCredentialsProvider(credentialsProvider)
            .build()
    );
    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
    subscriptionAdminClient.deleteSubscription(subscriptionName);
}
 
Example #14
Source File: GcpPubSubAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public PubSubAdmin pubSubAdmin(TopicAdminClient topicAdminClient,
		SubscriptionAdminClient subscriptionAdminClient) {
	return new PubSubAdmin(this.finalProjectIdProvider, topicAdminClient,
			subscriptionAdminClient);
}
 
Example #15
Source File: PubSubAdmin.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
/**
 * Return every subscription in a project.
 * <p>If there are multiple pages, they will all be merged into the same result.
 * @return a list of subscriptions
 */
public List<Subscription> listSubscriptions() {
	SubscriptionAdminClient.ListSubscriptionsPagedResponse subscriptionsPage =
			this.subscriptionAdminClient.listSubscriptions(ProjectName.of(this.projectId));

	List<Subscription> subscriptions = new ArrayList<>();
	subscriptionsPage.iterateAll().forEach(subscriptions::add);
	return Collections.unmodifiableList(subscriptions);
}
 
Example #16
Source File: PubSubAdmin.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
public PubSubAdmin(GcpProjectIdProvider projectIdProvider, TopicAdminClient topicAdminClient,
		SubscriptionAdminClient subscriptionAdminClient) {
	Assert.notNull(projectIdProvider, "The project ID provider can't be null.");
	Assert.notNull(topicAdminClient, "The topic administration client can't be null");
	Assert.notNull(subscriptionAdminClient,
			"The subscription administration client can't be null");

	this.projectId = projectIdProvider.getProjectId();
	Assert.hasText(this.projectId, "The project ID can't be null or empty.");
	this.topicAdminClient = topicAdminClient;
	this.subscriptionAdminClient = subscriptionAdminClient;
}
 
Example #17
Source File: PubSubAdmin.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
/**
 * This constructor instantiates TopicAdminClient and SubscriptionAdminClient with all their
 * defaults and the provided credentials provider.
 * @param projectIdProvider the project id provider to use
 * @param credentialsProvider the credentials provider to use
 * @throws IOException thrown when there are errors in contacting Google Cloud Pub/Sub
 */
public PubSubAdmin(GcpProjectIdProvider projectIdProvider,
		CredentialsProvider credentialsProvider) throws IOException {
	this(projectIdProvider,
			TopicAdminClient.create(
					TopicAdminSettings.newBuilder()
							.setCredentialsProvider(credentialsProvider)
							.build()),
			SubscriptionAdminClient.create(
					SubscriptionAdminSettings.newBuilder()
					.setCredentialsProvider(credentialsProvider)
					.build()));
}
 
Example #18
Source File: PubSubSampleApplicationTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void prepare() throws IOException {
	assumeThat(
			"PUB/SUB-sample integration tests are disabled. Please use '-Dit.pubsub=true' "
					+ "to enable them. ",
			System.getProperty("it.pubsub"), is("true"));

	projectName = ProjectName.of(ServiceOptions.getDefaultProjectId()).getProject();
	topicAdminClient = TopicAdminClient.create();
	subscriptionAdminClient = SubscriptionAdminClient.create();

	topicAdminClient.createTopic(ProjectTopicName.of(projectName, SAMPLE_TEST_TOPIC));
	topicAdminClient.createTopic(ProjectTopicName.of(projectName, SAMPLE_TEST_TOPIC2));

	subscriptionAdminClient.createSubscription(
			ProjectSubscriptionName.of(projectName, SAMPLE_TEST_SUBSCRIPTION1),
			ProjectTopicName.of(projectName, SAMPLE_TEST_TOPIC),
			PushConfig.getDefaultInstance(),
			10);

	subscriptionAdminClient.createSubscription(
			ProjectSubscriptionName.of(projectName, SAMPLE_TEST_SUBSCRIPTION2),
			ProjectTopicName.of(projectName, SAMPLE_TEST_TOPIC2),
			PushConfig.getDefaultInstance(),
			10);
	subscriptionAdminClient.createSubscription(
			ProjectSubscriptionName.of(projectName, SAMPLE_TEST_SUBSCRIPTION3),
			ProjectTopicName.of(projectName, SAMPLE_TEST_TOPIC2),
			PushConfig.getDefaultInstance(),
			10);
}
 
Example #19
Source File: PubsubIntegrationTest.java    From gcp-ingestion with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Create a Pub/Sub topic and subscription.
 *
 * @throws IOException if Pub/Sub is unavailable
 */
@Before
public void initializePubsubResources() throws IOException {
  projectId = ServiceOptions.getDefaultProjectId();
  topicId = "test-topic-" + UUID.randomUUID().toString();
  subscriptionId = "test-subscription-" + UUID.randomUUID().toString();
  topicName = ProjectTopicName.of(projectId, topicId);
  subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);

  TopicAdminClient.create().createTopic(topicName);
  SubscriptionAdminClient.create().createSubscription(subscriptionName, topicName,
      PushConfig.getDefaultInstance(), 0);
}
 
Example #20
Source File: PubSubClient.java    From daq with Apache License 2.0 5 votes vote down vote up
private boolean subscriptionExists(SubscriptionAdminClient subscriptionAdminClient,
    ProjectTopicName topicName, ProjectSubscriptionName subscriptionName) {
  ListSubscriptionsPagedResponse listSubscriptionsPagedResponse = subscriptionAdminClient
      .listSubscriptions(ProjectName.of(PROJECT_ID));
  for (Subscription subscription : listSubscriptionsPagedResponse.iterateAll()) {
    if (subscription.getName().equals(subscriptionName.toString())) {
      return true;
    }
  }
  return false;
}
 
Example #21
Source File: PubSubManager.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
private static SubscriptionAdminClient buildSubscriptionAdminClient(final PubSubConfig config) {
    final SubscriptionAdminSettings.Builder subscriptionAdminSettingsBuilder = SubscriptionAdminSettings.newBuilder();

    buildCredentialsProvider(config).ifPresent(subscriptionAdminSettingsBuilder::setCredentialsProvider);
    buildTransportChannelProvider(config).ifPresent(subscriptionAdminSettingsBuilder::setTransportChannelProvider);

    try {
        return SubscriptionAdminClient.create(subscriptionAdminSettingsBuilder.build());
    } catch (final IOException e) {
        throw ex.illegalStateUnableToBuildSubscriptionAdminClient(e);
    }
}
 
Example #22
Source File: PubsubHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
public void deleteSubscription(ProjectSubscriptionName subscriptionName) throws IOException {
	SubscriptionAdminClient adminClient = getSubscriptionAdminClient();
	try {
		adminClient.getSubscription(subscriptionName);
		// If it already exists we must first delete it.
		LOG.info("DeleteSubscription {}", subscriptionName);
		adminClient.deleteSubscription(subscriptionName);
	} catch (NotFoundException e) {
		// Doesn't exist. Good.
	}
}
 
Example #23
Source File: PubSubTestBinder.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
public PubSubTestBinder(String host) {
	GcpProjectIdProvider projectIdProvider = () -> "porto sentido";

	// Transport channel provider so that test binder talks to emulator.
	ManagedChannel channel = ManagedChannelBuilder
			.forTarget(host)
			.usePlaintext()
			.build();
	TransportChannelProvider transportChannelProvider =
			FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));

	PubSubChannelProvisioner pubSubChannelProvisioner;

	try {
		SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create(
				SubscriptionAdminSettings.newBuilder()
						.setTransportChannelProvider(transportChannelProvider)
						.setCredentialsProvider(NoCredentialsProvider.create())
						.build()
		);

		TopicAdminClient topicAdminClient = TopicAdminClient.create(
				TopicAdminSettings.newBuilder()
						.setTransportChannelProvider(transportChannelProvider)
						.setCredentialsProvider(NoCredentialsProvider.create())
						.build()
		);

		pubSubChannelProvisioner = new PubSubChannelProvisioner(
				new PubSubAdmin(projectIdProvider, topicAdminClient, subscriptionAdminClient)
		);
	}
	catch (IOException ioe) {
		throw new RuntimeException("Couldn't build test binder.", ioe);
	}

	DefaultSubscriberFactory subscriberFactory = new DefaultSubscriberFactory(projectIdProvider);
	subscriberFactory.setChannelProvider(transportChannelProvider);
	subscriberFactory.setCredentialsProvider(NoCredentialsProvider.create());

	DefaultPublisherFactory publisherFactory = new DefaultPublisherFactory(projectIdProvider);
	publisherFactory.setChannelProvider(transportChannelProvider);
	publisherFactory.setCredentialsProvider(NoCredentialsProvider.create());

	PubSubTemplate pubSubTemplate = new PubSubTemplate(publisherFactory, subscriberFactory);

	PubSubMessageChannelBinder binder =
			new PubSubMessageChannelBinder(null, pubSubChannelProvisioner, pubSubTemplate,
					new PubSubExtendedBindingProperties());
	GenericApplicationContext context = new GenericApplicationContext();
	binder.setApplicationContext(context);
	this.setBinder(binder);
}
 
Example #24
Source File: PubsubUtils.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
private static void setupSubscriptionClient() throws IOException {
  subscriptionAdminClient = SubscriptionAdminClient.create();
}
 
Example #25
Source File: PubsubIntegrationTest.java    From gcp-ingestion with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Clean up all the Pub/Sub resources we created.
 *
 * @throws IOException if Pub/Sub is unavailable
 */
@After
public void deletePubsubResources() throws IOException {
  SubscriptionAdminClient.create().deleteSubscription(subscriptionName);
  TopicAdminClient.create().deleteTopic(topicName);
}
 
Example #26
Source File: PubSubManager.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
public SubscriptionAdminClient subscriptionAdminClient(final PubSubConfig config) {
    return SUBSCRIPTION_ADMIN_CLIENT_MAP.computeIfAbsent(config, PubSubManager::buildSubscriptionAdminClient);
}