Search in sources :

Example 6 with TopicAdminClient

use of com.google.cloud.pubsub.spi.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.spi.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.spi.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.spi.v1.TopicAdminClient in project google-cloud-java by GoogleCloudPlatform.

the class TopicAdminClientSnippets method getTopic.

/** Example of getting a topic. */
public Topic getTopic(String topicId) throws Exception {
    // [START pubsub_get_topic]
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        TopicName topicName = TopicName.create(projectId, topicId);
        Topic topic = topicAdminClient.getTopic(topicName);
        return topic;
    }
// [END pubsub_get_topic]
}
Also used : TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) Topic(com.google.pubsub.v1.Topic) TopicName(com.google.pubsub.v1.TopicName)

Example 10 with TopicAdminClient

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

the class TopicAdminClientSnippets method listTopics.

/** Example of listing topics.  */
public ListTopicsPagedResponse listTopics() throws Exception {
    // [START pubsub_list_topics]
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        ListTopicsRequest listTopicsRequest = ListTopicsRequest.newBuilder().setProjectWithProjectName(ProjectName.create(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]
}
Also used : ListTopicsPagedResponse(com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse) TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) ListTopicsRequest(com.google.pubsub.v1.ListTopicsRequest) Topic(com.google.pubsub.v1.Topic)

Aggregations

TopicAdminClient (com.google.cloud.pubsub.spi.v1.TopicAdminClient)12 TopicName (com.google.pubsub.v1.TopicName)7 Topic (com.google.pubsub.v1.Topic)4 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 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 Binding (com.google.iam.v1.Binding)1 ListTopicSubscriptionsRequest (com.google.pubsub.v1.ListTopicSubscriptionsRequest)1 ListTopicsRequest (com.google.pubsub.v1.ListTopicsRequest)1 CallOptions (io.grpc.CallOptions)1 Channel (io.grpc.Channel)1 ClientInterceptor (io.grpc.ClientInterceptor)1 SimpleForwardingClientCall (io.grpc.ForwardingClientCall.SimpleForwardingClientCall)1 ManagedChannel (io.grpc.ManagedChannel)1 Metadata (io.grpc.Metadata)1 MethodDescriptor (io.grpc.MethodDescriptor)1