use of com.google.cloud.pubsub.v1.TopicAdminClient in project google-cloud-java by GoogleCloudPlatform.
the class TopicAdminClientSnippets method createTopic.
/** Example of creating a topic. */
public Topic createTopic(String topicId) throws Exception {
// [START pubsub_create_topic]
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
// projectId <= unique project identifier, eg. "my-project-id"
// topicId <= "my-topic-id"
TopicName topicName = TopicName.create(projectId, topicId);
Topic topic = topicAdminClient.createTopic(topicName);
return topic;
}
// [END pubsub_create_topic]
}
use of com.google.cloud.pubsub.v1.TopicAdminClient in project google-cloud-java by GoogleCloudPlatform.
the class ITPubSubSnippets method testPublisherSubscriber.
@Test
public void testPublisherSubscriber() throws Exception {
TopicName topicName = TopicName.create(ServiceOptions.getDefaultProjectId(), formatForTest("test-topic"));
SubscriptionName subscriptionName = SubscriptionName.create(ServiceOptions.getDefaultProjectId(), formatForTest("test-subscription"));
try (TopicAdminClient publisherClient = TopicAdminClient.create();
SubscriptionAdminClient subscriberClient = SubscriptionAdminClient.create()) {
publisherClient.createTopic(topicName);
subscriberClient.createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
testPublisherSubscriberHelper(topicName, subscriptionName);
subscriberClient.deleteSubscription(subscriptionName);
publisherClient.deleteTopic(topicName);
}
}
use of com.google.cloud.pubsub.v1.TopicAdminClient in project java-docs-samples by GoogleCloudPlatform.
the class ManagerIT method testHttpDeviceState.
@Test
public void testHttpDeviceState() throws Exception {
final String deviceName = "rsa-device-http-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, "-num_messages=1", "-message_type=state", "-algorithm=RS256" };
com.example.cloud.iot.examples.HttpExample.main(testArgs);
// End device test.
// Assertions
String got = bout.toString();
Assert.assertTrue(got.contains("200"));
Assert.assertTrue(got.contains("OK"));
// 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(topic.getNameAsTopicName());
}
}
use of com.google.cloud.pubsub.v1.TopicAdminClient in project java-docs-samples by GoogleCloudPlatform.
the class ManagerIT method testMqttDeviceEvents.
@Test
public void testMqttDeviceEvents() throws Exception {
final String deviceName = "rsa-device-mqtt-events";
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=events", "-num_messages=1", "-algorithm=RS256" };
com.example.cloud.iot.examples.MqttExample.main(testArgs);
// End device test.
// Assertions
String got = bout.toString();
//
// Finished loop successfully. Goodbye!
Assert.assertTrue(got.contains("Publishing events 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(topic.getNameAsTopicName());
}
}
use of com.google.cloud.pubsub.v1.TopicAdminClient in project java-docs-samples by GoogleCloudPlatform.
the class ManagerIT method testHttpDeviceEvent.
// HTTP device tests
@Test
public void testHttpDeviceEvent() throws Exception {
final String deviceName = "rsa-device-http-event";
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, "-num_messages=1", "-message_type=event", "-algorithm=RS256" };
com.example.cloud.iot.examples.HttpExample.main(testArgs);
// End device test.
// Assertions
String got = bout.toString();
Assert.assertTrue(got.contains("200"));
Assert.assertTrue(got.contains("OK"));
// 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(topic.getNameAsTopicName());
}
}
Aggregations