Search in sources :

Example 6 with TopicAdminClient

use of com.google.cloud.pubsub.v1.TopicAdminClient in project google-cloud-java by GoogleCloudPlatform.

the class Cleanup method deleteTestTopics.

private static void deleteTestTopics(String projectId, String[] testTopics) throws Exception {
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        for (String topicId : testTopics) {
            try {
                topicAdminClient.deleteTopic(TopicName.create(projectId, topicId));
                System.out.println("Topic deleted : " + topicId);
            } catch (Exception e) {
            //do nothing catch clause
            }
        }
    }
}
Also used : TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient)

Example 7 with TopicAdminClient

use of com.google.cloud.pubsub.v1.TopicAdminClient in project google-cloud-java by GoogleCloudPlatform.

the class SubscriptionAdminClientSnippets method testSubscriptionPermissions.

/** 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");
        SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
        TestIamPermissionsResponse testedPermissions = topicAdminClient.testIamPermissions(subscriptionName.toString(), permissions);
        return testedPermissions;
    }
// [END pubsub_test_subscription_permissions]
}
Also used : TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) TestIamPermissionsResponse(com.google.iam.v1.TestIamPermissionsResponse) SubscriptionName(com.google.pubsub.v1.SubscriptionName) LinkedList(java.util.LinkedList)

Example 8 with TopicAdminClient

use of com.google.cloud.pubsub.v1.TopicAdminClient in project toolkit by googleapis.

the class Pubsub method gapic.

private static void gapic(final Settings settings) throws Exception {
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
    // In real clients, InstantiatingChannelProvider is responsible for adding interceptors.
    // We can't use it here because InstantiatingChannelProvider doesn't have sslContext method.
    // Instead, we imitate HeaderInterceptor.
    ClientInterceptor headerInterceptor = new ClientInterceptor() {

        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
            ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
            return new SimpleForwardingClientCall<ReqT, RespT>(call) {

                @Override
                public void start(ClientCall.Listener<RespT> responseListener, Metadata headers) {
                    headers.put(X_GOOG_HEADER_KEY, X_GOOG_HEADER_VALUE);
                    super.start(responseListener, headers);
                }
            };
        }
    };
    ManagedChannel channel = NettyChannelBuilder.forTarget(settings.endpoint()).executor(executor).sslContext(GrpcSslContexts.forClient().trustManager(new File(settings.cert())).build()).intercept(headerInterceptor).build();
    final Semaphore semaphore = new Semaphore(settings.numWorkers());
    final TopicAdminClient client = TopicAdminClient.create(TopicAdminSettings.defaultBuilder().setChannelProvider(FixedChannelProvider.create(channel)).setExecutorProvider(FixedExecutorProvider.create(executor)).build());
    final AtomicLong resetTime = new AtomicLong();
    final AtomicLong numCalls = new AtomicLong();
    final AtomicLong numErrs = new AtomicLong();
    long endTime = System.nanoTime() + settings.warmDurNano() + settings.targetDurNano();
    Thread resetter = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(settings.warmDurNano() / MILLION);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            numCalls.set(0);
            numErrs.set(0);
            resetTime.set(System.nanoTime());
        }
    });
    resetter.start();
    while (System.nanoTime() < endTime) {
        semaphore.acquire(1);
        ApiFutures.addCallback(client.getTopicCallable().futureCall(GetTopicRequest.newBuilder().setTopicWithTopicName(TOPIC_NAME_RESOURCE).build()), new ApiFutureCallback<Topic>() {

            @Override
            public void onSuccess(Topic topic) {
                if (!topic.getName().equals(TOPIC_NAME_STRING)) {
                    numErrs.incrementAndGet();
                }
                both();
            }

            @Override
            public void onFailure(Throwable t) {
                numErrs.incrementAndGet();
                both();
            }

            void both() {
                numCalls.incrementAndGet();
                semaphore.release(1);
            }
        });
    }
    long nCalls = numCalls.get();
    long nErrs = numErrs.get();
    long runDurNano = System.nanoTime() - resetTime.get();
    System.out.println("errors: " + nErrs);
    System.out.println("calls: " + nCalls);
    System.out.println("time per call (ns): " + (runDurNano / nCalls));
    System.out.println("QPS: " + (nCalls * BILLION / runDurNano));
    client.close();
    channel.shutdown().awaitTermination(10, TimeUnit.SECONDS);
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) Semaphore(java.util.concurrent.Semaphore) ClientInterceptor(io.grpc.ClientInterceptor) ManagedChannel(io.grpc.ManagedChannel) Topic(com.google.pubsub.v1.Topic) ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) MethodDescriptor(io.grpc.MethodDescriptor) AtomicLong(java.util.concurrent.atomic.AtomicLong) TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) File(java.io.File) SimpleForwardingClientCall(io.grpc.ForwardingClientCall.SimpleForwardingClientCall)

Example 9 with TopicAdminClient

use of com.google.cloud.pubsub.v1.TopicAdminClient in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method createIotTopic.

/**
 * Creates a topic and grants the IoT service account access.
 */
public static Topic createIotTopic(String projectId, String topicId) throws Exception {
    // Create a new topic
    final TopicName topicName = TopicName.create(projectId, topicId);
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        final Topic topic = topicAdminClient.createTopic(topicName);
        com.google.iam.v1.Policy policy = topicAdminClient.getIamPolicy(topicName.toString());
        // add role -> members binding
        Binding binding = Binding.newBuilder().addMembers("serviceAccount:cloud-iot@system.gserviceaccount.com").setRole(Role.owner().toString()).build();
        // create updated policy
        com.google.iam.v1.Policy updatedPolicy = com.google.iam.v1.Policy.newBuilder(policy).addBindings(binding).build();
        topicAdminClient.setIamPolicy(topicName.toString(), updatedPolicy);
        System.out.println("Setup topic / policy for: " + topic.getName());
        return topic;
    }
}
Also used : Binding(com.google.iam.v1.Binding) TopicAdminClient(com.google.cloud.pubsub.v1.TopicAdminClient) Topic(com.google.pubsub.v1.Topic) TopicName(com.google.pubsub.v1.TopicName)

Example 10 with TopicAdminClient

use of com.google.cloud.pubsub.v1.TopicAdminClient in project java-docs-samples by GoogleCloudPlatform.

the class ManagerIT method testCreateGetRegistry.

@Test
public void testCreateGetRegistry() throws Exception {
    topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
    DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
    DeviceRegistryExample.getRegistry(PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
    String got = bout.toString();
    Assert.assertFalse(got.contains("eventNotificationConfigs"));
    DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        topicAdminClient.deleteTopic(topic.getNameAsTopicName());
    }
}
Also used : TopicAdminClient(com.google.cloud.pubsub.v1.TopicAdminClient) Test(org.junit.Test)

Aggregations

TopicAdminClient (com.google.cloud.pubsub.v1.TopicAdminClient)19 Test (org.junit.Test)18 TopicAdminClient (com.google.cloud.pubsub.spi.v1.TopicAdminClient)12 TopicName (com.google.pubsub.v1.TopicName)8 Topic (com.google.pubsub.v1.Topic)5 Binding (com.google.iam.v1.Binding)2 Policy (com.google.iam.v1.Policy)2 TestIamPermissionsResponse (com.google.iam.v1.TestIamPermissionsResponse)2 SubscriptionName (com.google.pubsub.v1.SubscriptionName)2 LinkedList (java.util.LinkedList)2 ApiException (com.google.api.gax.rpc.ApiException)1 ListTopicSubscriptionsPagedResponse (com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse)1 ListTopicsPagedResponse (com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse)1 SubscriptionAdminClient (com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient)1 ListTopicSubscriptionsRequest (com.google.pubsub.v1.ListTopicSubscriptionsRequest)1 ListTopicsRequest (com.google.pubsub.v1.ListTopicsRequest)1 ProjectTopicName (com.google.pubsub.v1.ProjectTopicName)1 CallOptions (io.grpc.CallOptions)1 Channel (io.grpc.Channel)1 ClientInterceptor (io.grpc.ClientInterceptor)1