com.google.pubsub.v1.ProjectTopicName Java Examples

The following examples show how to use com.google.pubsub.v1.ProjectTopicName. 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: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateGetDevice() throws Exception {
  final String deviceName = "rsa-device";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.getDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Created device: {"));
  Assert.assertTrue(got.contains("Retrieving device"));

  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #3
Source File: CdcPCollectionsFetchers.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
private ProjectTopicName getPubSubTopic() {
  if (options.getInputSubscriptions() != null && !options.getInputSubscriptions().isEmpty()) {
    try {
      return PubsubUtils.getPubSubTopicFromSubscription(
          options.as(GcpOptions.class).getProject(), options.getInputSubscriptions());
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  } else {
    Preconditions.checkArgument(
        options.getInputTopics() != null && !options.getInputTopics().isEmpty(),
        "Must provide an inputSubscriptions or inputTopics parameter.");
    return ProjectTopicName.of(
        options.as(GcpOptions.class).getProject(), options.getInputTopics());
  }
}
 
Example #4
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateConfigureDevice() throws Exception {
  final String deviceName = "rsa-device-config";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.setDeviceConfiguration(
      deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID, "some-test-data", 0L);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Updated: 2"));

  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #5
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateListDevices() throws Exception {
  final String deviceName = "rsa-device";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.listDevices(PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Created device: {"));
  Assert.assertTrue(got.contains("Found"));

  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #6
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateDeleteEsDevice() throws Exception {
  final String deviceName = "es-device";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithEs256(
      deviceName, ES_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.getDeviceStates(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Created device: {"));

  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #7
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateDeleteUnauthDevice() throws Exception {
  final String deviceName = "noauth-device";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);

  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithNoAuth(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Created device: {"));

  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #8
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testPatchEs() throws Exception {
  final String deviceName = "patchme-device-es";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);

  try {
    DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
    DeviceRegistryExample.createDeviceWithNoAuth(
        deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
    DeviceRegistryExample.patchEs256ForAuth(
        deviceName, ES_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

    String got = bout.toString(StandardCharsets.UTF_8.name());
    Assert.assertTrue(got.contains("Created device: {"));
  } finally {
    DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
    DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  }

  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #9
Source File: BaseIT.java    From kafka-pubsub-emulator with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a KafkaConsumer that is manually assigned to all partitions of the test topic indicated
 * by the {@code subscription}.
 */
protected Consumer<String, ByteBuffer> getValidationConsumer(String topic, String subscription) {
  Consumer<String, ByteBuffer> consumer =
      kafkaClientFactory.createConsumer(
          ProjectSubscriptionName.of(PROJECT, subscription).toString());

  Set<TopicPartition> topicPartitions =
      consumer
          .listTopics()
          .entrySet()
          .stream()
          .filter(e -> e.getKey().equals(ProjectTopicName.of(PROJECT, topic).toString()))
          .flatMap(
              e -> e.getValue().stream().map(p -> new TopicPartition(p.topic(), p.partition())))
          .collect(Collectors.toSet());
  consumer.assign(topicPartitions);

  return consumer;
}
 
Example #10
Source File: GoogleCloudPubSubSinkConfiguration.java    From divolte-collector with Apache License 2.0 6 votes vote down vote up
private SinkFactory createFlushingPool(final RetrySettings retrySettings,
                                       final BatchingSettings batchingSettings) {
    return (vc, sinkName, registry) -> {
        final String projectId = vc.configuration().global.gcps.projectId.orElseThrow(IllegalStateException::new);
        final ProjectTopicName topicName = ProjectTopicName.of(projectId, topic);
        final Publisher.Builder builder =
            Publisher.newBuilder(topicName)
                     .setRetrySettings(retrySettings)
                     .setBatchingSettings(batchingSettings);
        final Publisher publisher = IOExceptions.wrap(builder::build).get();
        return new GoogleCloudPubSubFlushingPool(sinkName,
                                                 vc.configuration().global.gcps.threads,
                                                 vc.configuration().global.gcps.bufferSize,
                                                 publisher,
                                                 Optional.empty(),
                                                 registry.getSchemaBySinkName(sinkName));
    };
}
 
Example #11
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testPatchRsa() throws Exception {
  final String deviceName = "patchme-device-rsa";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);

  try {
    DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
    DeviceRegistryExample.createDeviceWithNoAuth(
        deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
    DeviceRegistryExample.patchRsa256ForAuth(
        deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

    String got = bout.toString(StandardCharsets.UTF_8.name());
    Assert.assertTrue(got.contains("Created device: {"));
  } finally {
    DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
    DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  }

  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #12
Source File: PubSubTopicUtils.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
/**
 * Create a {@link ProjectTopicName} based on a topic name within a project or the
 * fully-qualified topic name. If the specified topic is in the
 * {@code projects/<project_name>/topics/<topic_name>} format, then the {@code projectId} is
 * ignored}
 * @param topic the topic name in the project or the fully-qualified project name
 * @param projectId the project ID to use if the topic is not a fully-qualified name
 * @return the Pub/Sub object representing the topic name
 */
public static ProjectTopicName toProjectTopicName(String topic, @Nullable String projectId) {
	Assert.notNull(topic, "The topic can't be null.");

	ProjectTopicName projectTopicName = null;

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

	return projectTopicName;
}
 
Example #13
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 new Topic is created. */
public final com.google.pubsub.v1.Topic createTopic(com.google.pubsub.v1.Topic topic)
    throws ConfigurationAlreadyExistsException {
  ProjectTopicName projectTopicName = ProjectTopicName.parse(topic.getName());
  if (getTopicByName(topic.getName()).isPresent()) {
    throw new ConfigurationAlreadyExistsException(
        "Topic " + projectTopicName.toString() + " already exists");
  }
  com.google.pubsub.v1.Topic.Builder builder = topic.toBuilder();
  if (topic.getLabelsOrDefault(KAFKA_TOPIC, null) == null) {
    builder.putLabels(
        KAFKA_TOPIC,
        String.join(
            KAFKA_TOPIC_SEPARATOR, projectTopicName.getProject(), projectTopicName.getTopic()));
  }

  com.google.pubsub.v1.Topic built = builder.build();
  topicsByProject.put(ProjectName.of(projectTopicName.getProject()).toString(), built);
  pubSubRepository.save(getPubSub());
  return built;
}
 
Example #14
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 #15
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateDeleteRsaDevice() throws Exception {
  final String deviceName = "rsa-device";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.getDeviceStates(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Created device: {"));

  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #16
Source File: PubSubTest.java    From smallrye-reactive-messaging with Apache License 2.0 6 votes vote down vote up
@Test
public void testSourceAndSink() {
    final ConsumptionBean consumptionBean = container.select(ConsumptionBean.class).get();

    // wait until the subscription is ready
    final PubSubManager manager = container.select(PubSubManager.class).get();
    await().until(() -> manager
            .topicAdminClient(
                    new PubSubConfig(PROJECT_ID, topic, null, true, "localhost", CONTAINER.getMappedPort(PUBSUB_PORT)))
            .listTopicSubscriptions(ProjectTopicName.of(PROJECT_ID, topic))
            .getPage()
            .getPageElementCount() > 0);

    send("Hello-0", topic);
    await().until(() -> consumptionBean.getMessages().size() == 1);
    assertThat(consumptionBean.getMessages().get(0)).isEqualTo("Hello-0");
    for (int i = 1; i < 11; i++) {
        send("Hello-" + i, topic);
    }
    await().until(() -> consumptionBean.getMessages().size() == 11);
    assertThat(consumptionBean.getMessages()).allSatisfy(s -> assertThat(s).startsWith("Hello-"));
}
 
Example #17
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 #18
Source File: PubSubPublish.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws IOException, ServletException {
  Publisher publisher = this.publisher;
  try {
    String topicId = System.getenv("PUBSUB_TOPIC");
    // create a publisher on the topic
    if (publisher == null) {
      publisher = Publisher.newBuilder(
          ProjectTopicName.of(ServiceOptions.getDefaultProjectId(), topicId))
          .build();
    }
    // construct a pubsub message from the payload
    final String payload = req.getParameter("payload");
    PubsubMessage pubsubMessage =
        PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(payload)).build();

    publisher.publish(pubsubMessage);
    // redirect to home page
    resp.sendRedirect("/");
  } catch (Exception e) {
    resp.sendError(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
  }
}
 
Example #19
Source File: PubSubSink.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void open(Configuration configuration) throws Exception {
	serializationSchema.open(() -> getRuntimeContext().getMetricGroup().addGroup("user"));

	Publisher.Builder builder = Publisher
		.newBuilder(ProjectTopicName.of(projectName, topicName))
		.setCredentialsProvider(FixedCredentialsProvider.create(credentials));

	if (hostAndPortForEmulator != null) {
		managedChannel = ManagedChannelBuilder
			.forTarget(hostAndPortForEmulator)
			.usePlaintext(true) // This is 'Ok' because this is ONLY used for testing.
			.build();
		channel = GrpcTransportChannel.newBuilder().setManagedChannel(managedChannel).build();
		builder.setChannelProvider(FixedTransportChannelProvider.create(channel))
				.setCredentialsProvider(NoCredentialsProvider.create());
	}

	publisher = builder.build();
	isRunning = true;
}
 
Example #20
Source File: Poller.java    From spanner-event-exporter with Apache License 2.0 6 votes vote down vote up
private Publisher configurePubSub() {
  if (publishToPubSub) {
    ProjectTopicName topicName =
        ProjectTopicName.of(PROJECT_ID, tableName); // Topic name will always be the Table Name
    try {
      Publisher publisher = Publisher.newBuilder(topicName).build();
      return publisher;
    } catch (IOException e) {
      log.error("Was not able to create a publisher for topic: " + topicName, e);

      stop();
      System.exit(1);
    }
  }

  // If configured to publishToPubSub, this function will return a publisher or throw
  return null;
}
 
Example #21
Source File: CPSPublisherTask.java    From pubsub with Apache License 2.0 6 votes vote down vote up
CPSPublisherTask(StartRequest request, MetricsHandler metricsHandler, int workerCount) {
  super(request, metricsHandler, workerCount);
  log.warn("constructing CPS publisher");
  this.payload = getPayload();
  try {
    this.publisher =
        Publisher.newBuilder(ProjectTopicName.of(request.getProject(), request.getTopic()))
            .setBatchingSettings(
                BatchingSettings.newBuilder()
                    .setElementCountThreshold((long) request.getPublisherOptions().getBatchSize())
                    .setRequestByteThreshold(9500000L)
                    .setDelayThreshold(
                        Duration.ofMillis(
                            Durations.toMillis(request.getPublisherOptions().getBatchDuration())))
                    .setIsEnabled(true)
                    .build())
            .build();
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example #22
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 #23
Source File: PubsubHelper.java    From flink with Apache License 2.0 6 votes vote down vote up
public void deleteTopic(ProjectTopicName topicName) throws IOException {
	TopicAdminClient adminClient = getTopicAdminClient();
	try {
		adminClient.getTopic(topicName);
	} catch (NotFoundException e) {
		// Doesn't exist. Good.
		return;
	}

	// If it exists we delete all subscriptions and the topic itself.
	LOG.info("DeleteTopic {} first delete old subscriptions.", topicName);
	adminClient
		.listTopicSubscriptions(topicName)
		.iterateAllAsProjectSubscriptionName()
		.forEach(subscriptionAdminClient::deleteSubscription);
	LOG.info("DeleteTopic {}", topicName);
	adminClient
		.deleteTopic(topicName);
}
 
Example #24
Source File: PubsubHelper.java    From flink with Apache License 2.0 6 votes vote down vote up
public void deleteTopic(ProjectTopicName topicName) throws IOException {
	TopicAdminClient adminClient = getTopicAdminClient();
	try {
		adminClient.getTopic(topicName);
	} catch (NotFoundException e) {
		// Doesn't exist. Good.
		return;
	}

	// If it exists we delete all subscriptions and the topic itself.
	LOG.info("DeleteTopic {} first delete old subscriptions.", topicName);
	adminClient
		.listTopicSubscriptions(topicName)
		.iterateAllAsProjectSubscriptionName()
		.forEach(subscriptionAdminClient::deleteSubscription);
	LOG.info("DeleteTopic {}", topicName);
	adminClient
		.deleteTopic(topicName);
}
 
Example #25
Source File: PublishMessage.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void service(HttpRequest request, HttpResponse response) throws IOException {
  Optional<String> maybeTopicName = request.getFirstQueryParameter("topic");
  Optional<String> maybeMessage = request.getFirstQueryParameter("message");

  BufferedWriter responseWriter = response.getWriter();

  if (maybeTopicName.isEmpty() || maybeMessage.isEmpty()) {
    response.setStatusCode(HttpURLConnection.HTTP_BAD_REQUEST);

    responseWriter.write("Missing 'topic' and/or 'subscription' parameter(s).");
    return;
  }

  String topicName = maybeTopicName.get();
  logger.info("Publishing message to topic: " + topicName);

  // Create the PubsubMessage object
  // (This is different than the PubSubMessage POJO used in Pub/Sub-triggered functions)
  ByteString byteStr = ByteString.copyFrom(maybeMessage.get(), StandardCharsets.UTF_8);
  PubsubMessage pubsubApiMessage = PubsubMessage.newBuilder().setData(byteStr).build();

  Publisher publisher = Publisher.newBuilder(
      ProjectTopicName.of(PROJECT_ID, topicName)).build();

  // Attempt to publish the message
  String responseMessage;
  try {
    publisher.publish(pubsubApiMessage).get();
    responseMessage = "Message published.";
  } catch (InterruptedException | ExecutionException e) {
    logger.log(Level.SEVERE, "Error publishing Pub/Sub message: " + e.getMessage(), e);
    responseMessage = "Error publishing Pub/Sub message; see logs for more info.";
  }

  responseWriter.write(responseMessage);
}
 
Example #26
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testMqttDeviceState() throws Exception {
  final String deviceName = "rsa-device-mqtt-state";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.listDevices(PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  // Device bootstrapped, time to connect and run.
  String[] testArgs = {
    "-project_id=" + PROJECT_ID,
    "-registry_id=" + REGISTRY_ID,
    "-device_id=" + deviceName,
    "-private_key_file=" + PKCS_PATH,
    "-message_type=state",
    "-algorithm=RS256"
  };
  com.example.cloud.iot.examples.MqttExample.main(testArgs);
  // End device test.

  // Assertions
  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Publishing state message 1"));
  Assert.assertTrue(got.contains("Finished loop successfully. Goodbye!"));

  // Clean up
  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #27
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetIam() throws Exception {
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.setIamPermissions(PROJECT_ID, CLOUD_REGION, REGISTRY_ID, MEMBER, ROLE);
  DeviceRegistryExample.getIamPermissions(PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("ETAG"));

  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example #28
Source File: PubSubPublish.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws IOException, ServletException {
  Publisher publisher = this.publisher;
  try {
    String topicId = System.getenv("PUBSUB_TOPIC");
    // create a publisher on the topic
    if (publisher == null) {
      ProjectTopicName topicName =
          ProjectTopicName.newBuilder()
              .setProject(ServiceOptions.getDefaultProjectId())
              .setTopic(topicId)
              .build();
      publisher = Publisher.newBuilder(topicName).build();
    }
    // construct a pubsub message from the payload
    final String payload = req.getParameter("payload");
    PubsubMessage pubsubMessage =
        PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(payload)).build();

    publisher.publish(pubsubMessage);
    // redirect to home page
    resp.sendRedirect("/");
  } catch (Exception e) {
    resp.sendError(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
  }
}
 
Example #29
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 #30
Source File: Connection.java    From heroic with Apache License 2.0 5 votes vote down vote up
Connection(
    ConsumerSchema.Consumer consumer,
    ConsumerReporter reporter,
    AtomicLong errors,
    LongAdder consumed,
    String projectId,
    String topicId,
    String subscriptionId,
    int threads,
    long maxOutstandingElementCount,
    long maxOutstandingRequestBytes,
    int maxInboundMessageSize,
    long keepAlive
) {
    this.consumer = consumer;
    this.reporter = reporter;
    this.errors = errors;
    this.consumed = consumed;
    this.projectId = projectId;
    this.subscriptionId = subscriptionId;
    this.threads = threads;
    this.topicName = ProjectTopicName.of(projectId, topicId);
    this.subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
    this.credentialsProvider = SubscriptionAdminSettings
        .defaultCredentialsProviderBuilder()
        .build();
    this.channelProvider = SubscriptionAdminSettings
        .defaultGrpcTransportProviderBuilder()
        .setMaxInboundMessageSize(maxInboundMessageSize)
        .setKeepAliveTime(Duration.ofSeconds(keepAlive))
        .build();
    this.maxOutstandingElementCount = maxOutstandingElementCount;
    this.maxOutstandingRequestBytes = maxOutstandingRequestBytes;
}