/** 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" ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId); Topic topic = topicAdminClient.createTopic(topicName); return topic; } // [END pubsub_create_topic] }
/** Example of deleting a topic. */ public ProjectTopicName deleteTopic(String topicId) throws Exception { // [START pubsub_delete_topic] try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId); topicAdminClient.deleteTopic(topicName); return topicName; } // [END pubsub_delete_topic] }
/** Example of getting a topic. */ public Topic getTopic(String topicId) throws Exception { // [START pubsub_get_topic] try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId); Topic topic = topicAdminClient.getTopic(topicName); return topic; } // [END pubsub_get_topic] } }
public static void createTopic() throws Exception { ProjectTopicName topic = ProjectTopicName.of("my-project-id", "my-topic-id"); try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { topicAdminClient.createTopic(topic); } }
/** Constructs an instance of TopicAdminClient with default settings. */ public static final TopicAdminClient create() throws IOException { return create(TopicAdminSettings.newBuilder().build()); }
/** * Create a topic. * * @param args topicId * @throws Exception exception thrown if operation is unsuccessful */ public static void main(String... args) throws Exception { // Your Google Cloud Platform project ID String projectId = ServiceOptions.getDefaultProjectId(); // Your topic ID, eg. "my-topic" String topicId = args[0]; // Create a new topic ProjectTopicName topic = ProjectTopicName.of(projectId, topicId); try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { topicAdminClient.createTopic(topic); } catch (ApiException e) { // example : code = ALREADY_EXISTS(409) implies topic already exists System.out.print(e.getStatusCode().getCode()); System.out.print(e.isRetryable()); } System.out.printf("Topic %s:%s created.\n", topic.getProject(), topic.getTopic()); } }
/** Example of testing whether the caller has the provided permissions on a subscription. */ public TestIamPermissionsResponse testSubscriptionPermissions(String subscriptionId) throws Exception { // [START pubsub_test_subscription_permissions] try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { List<String> permissions = new LinkedList<>(); permissions.add("pubsub.subscriptions.get"); ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId); TestIamPermissionsResponse testedPermissions = topicAdminClient.testIamPermissions(subscriptionName.toString(), permissions); return testedPermissions; } // [END pubsub_test_subscription_permissions] }
/** * Example of testing whether the caller has the provided permissions on a topic. Only viewer, * editor or admin/owner can view results of pubsub.topics.get */ public TestIamPermissionsResponse testTopicPermissions(String topicId) throws Exception { // [START pubsub_test_topic_permissions] try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { List<String> permissions = new LinkedList<>(); permissions.add("pubsub.topics.get"); ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId); TestIamPermissionsResponse testedPermissions = topicAdminClient.testIamPermissions(topicName.toString(), permissions); return testedPermissions; } // [END pubsub_test_topic_permissions] }
/** Example of getting a topic policy. */ public Policy getTopicPolicy(String topicId) throws Exception { // [START pubsub_get_topic_policy] try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId); Policy policy = topicAdminClient.getIamPolicy(topicName.toString()); if (policy == null) { // topic iam policy was not found } return policy; } // [END pubsub_get_topic_policy] }
public static void executeNoCatch(String projectId) throws Exception { try (TopicAdminClient client = TopicAdminClient.create()) { ProjectName project = ProjectName.of(projectId); ListTopicsPagedResponse pagedResponse = client.listTopics(project); } }
/** Example of listing topics. */ public ListTopicsPagedResponse listTopics() throws Exception { // [START pubsub_list_topics] try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { ListTopicsRequest listTopicsRequest = ListTopicsRequest.newBuilder().setProject(ProjectName.format(projectId)).build(); ListTopicsPagedResponse response = topicAdminClient.listTopics(listTopicsRequest); Iterable<Topic> topics = response.iterateAll(); for (Topic topic : topics) { // do something with the topic } return response; } // [END pubsub_list_topics] }
@BeforeClass public static void setupClass() throws Exception { topicAdminClient = TopicAdminClient.create(); subscriptionAdminClient = SubscriptionAdminClient.create(); projectId = ServiceOptions.getDefaultProjectId(); }
/** Example of listing subscriptions for a topic. */ public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String topicId) throws Exception { // [START pubsub_list_topic_subscriptions] try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId); ListTopicSubscriptionsRequest request = ListTopicSubscriptionsRequest.newBuilder().setTopic(topicName.toString()).build(); ListTopicSubscriptionsPagedResponse response = topicAdminClient.listTopicSubscriptions(request); Iterable<String> subscriptionNames = response.iterateAll(); for (String subscriptionName : subscriptionNames) { // do something with the subscription name } return response; } // [END pubsub_list_topic_subscriptions] }
TopicAdminClient.create( TopicAdminSettings.newBuilder() .setTransportChannelProvider(channelProvider)
@Before public void setUp() throws IOException { serviceHelper.reset(); channelProvider = serviceHelper.createChannelProvider(); TopicAdminSettings settings = TopicAdminSettings.newBuilder() .setTransportChannelProvider(channelProvider) .setCredentialsProvider(NoCredentialsProvider.create()) .build(); client = TopicAdminClient.create(settings); }
/** Example of replacing a topic policy. */ public Policy replaceTopicPolicy(String topicId) throws Exception { // [START pubsub_set_topic_policy] try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { String topicName = ProjectTopicName.format(projectId, topicId); Policy policy = topicAdminClient.getIamPolicy(topicName); // add role -> members binding Binding binding = Binding.newBuilder() .setRole(Role.viewer().toString()) .addMembers(Identity.allAuthenticatedUsers().toString()) .build(); // create updated policy Policy updatedPolicy = Policy.newBuilder(policy).addBindings(binding).build(); updatedPolicy = topicAdminClient.setIamPolicy(topicName, updatedPolicy); return updatedPolicy; } // [END pubsub_set_topic_policy] }
@BeforeClass public static void beforeClass() throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { remoteStorageHelper = RemoteStorageHelper.create(); topicAdminClient = TopicAdminClient.create(); storageService = remoteStorageHelper.getOptions().getService(); notificationService = new DefaultNotificationFactory().create(remoteStorageHelper.getOptions().getService()); storageService.create(BucketInfo.of(BUCKET)); projectId = ServiceOptions.getDefaultProjectId(); }
@Bean @ConditionalOnMissingBean public TopicAdminClient topicAdminClient( TopicAdminSettings topicAdminSettings) { try { return TopicAdminClient.create(topicAdminSettings); } catch (IOException ioe) { throw new PubSubException("An error occurred while creating TopicAdminClient.", ioe); } }
public TopicAdminClient topicAdmin() { if (topicAdmin == null) { try { TopicAdminSettings settings = TopicAdminSettings.defaultBuilder() .setCredentialsProvider(this).build(); topicAdmin = TopicAdminClient.create(settings); } catch (IOException e) { throw new RuntimeException(e); } } return topicAdmin; }
@Provides @Singleton public TopicAdminClient getClient() throws IOException { final TopicAdminSettings.Builder adminSetting = TopicAdminSettings.newBuilder(); final String emulatorHost = System.getenv("PUBSUB_EMULATOR_HOST"); if (emulatorHost != null) { ManagedChannel channel = ManagedChannelBuilder.forTarget(emulatorHost).usePlaintext().build(); TransportChannelProvider channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel)); adminSetting.setTransportChannelProvider(channelProvider); adminSetting.setCredentialsProvider(NoCredentialsProvider.create()); } return TopicAdminClient.create(adminSetting.build()); }