Search in sources :

Example 16 with Topic

use of com.google.pubsub.v1.Topic 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 17 with Topic

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

the class Pubsub method grpc.

private static void grpc(final Settings settings) throws Exception {
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
    ManagedChannel channel = NettyChannelBuilder.forTarget(settings.endpoint()).executor(executor).sslContext(GrpcSslContexts.forClient().trustManager(new File(settings.cert())).build()).build();
    final Semaphore semaphore = new Semaphore(settings.numWorkers());
    Metadata header = new Metadata();
    header.put(X_GOOG_HEADER_KEY, X_GOOG_HEADER_VALUE);
    final PublisherFutureStub stub = MetadataUtils.attachHeaders(PublisherGrpc.newFutureStub(channel), header);
    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);
        Futures.addCallback(stub.getTopic(GetTopicRequest.newBuilder().setTopic(TOPIC_NAME_STRING).build()), new FutureCallback<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));
    channel.shutdown().awaitTermination(10, TimeUnit.SECONDS);
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Metadata(io.grpc.Metadata) Semaphore(java.util.concurrent.Semaphore) PublisherFutureStub(com.google.pubsub.v1.PublisherGrpc.PublisherFutureStub) AtomicLong(java.util.concurrent.atomic.AtomicLong) ManagedChannel(io.grpc.ManagedChannel) Topic(com.google.pubsub.v1.Topic) File(java.io.File)

Example 18 with Topic

use of com.google.pubsub.v1.Topic in project beam by apache.

the class PubsubGrpcClientTest method publishOneMessage.

@Test
public void publishOneMessage() throws IOException {
    String expectedTopic = TOPIC.getPath();
    PubsubMessage expectedPubsubMessage = PubsubMessage.newBuilder().setData(ByteString.copyFrom(DATA.getBytes())).putAllAttributes(ATTRIBUTES).putAllAttributes(ImmutableMap.of(TIMESTAMP_ATTRIBUTE, String.valueOf(MESSAGE_TIME), ID_ATTRIBUTE, RECORD_ID)).build();
    final PublishRequest expectedRequest = PublishRequest.newBuilder().setTopic(expectedTopic).addAllMessages(ImmutableList.of(expectedPubsubMessage)).build();
    final PublishResponse response = PublishResponse.newBuilder().addAllMessageIds(ImmutableList.of(MESSAGE_ID)).build();
    final List<PublishRequest> requestsReceived = new ArrayList<>();
    PublisherImplBase publisherImplBase = new PublisherImplBase() {

        @Override
        public void publish(PublishRequest request, StreamObserver<PublishResponse> responseObserver) {
            requestsReceived.add(request);
            responseObserver.onNext(response);
            responseObserver.onCompleted();
        }
    };
    Server server = InProcessServerBuilder.forName(channelName).addService(publisherImplBase).build().start();
    try {
        OutgoingMessage actualMessage = new OutgoingMessage(DATA.getBytes(), ATTRIBUTES, MESSAGE_TIME, RECORD_ID);
        int n = client.publish(TOPIC, ImmutableList.of(actualMessage));
        assertEquals(1, n);
        assertEquals(expectedRequest, Iterables.getOnlyElement(requestsReceived));
    } finally {
        server.shutdownNow();
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) PublishResponse(com.google.pubsub.v1.PublishResponse) OutgoingMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage) Server(io.grpc.Server) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) PublishRequest(com.google.pubsub.v1.PublishRequest) PubsubMessage(com.google.pubsub.v1.PubsubMessage) PublisherImplBase(com.google.pubsub.v1.PublisherGrpc.PublisherImplBase) Test(org.junit.Test)

Example 19 with Topic

use of com.google.pubsub.v1.Topic 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 20 with Topic

use of com.google.pubsub.v1.Topic 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

TopicName (com.google.pubsub.v1.TopicName)26 Test (org.junit.Test)22 Topic (com.google.pubsub.v1.Topic)13 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)9 SubscriptionName (com.google.pubsub.v1.SubscriptionName)9 TopicAdminClient (com.google.cloud.pubsub.spi.v1.TopicAdminClient)8 ByteString (com.google.protobuf.ByteString)7 PubsubMessage (com.google.pubsub.v1.PubsubMessage)7 ApiException (com.google.api.gax.grpc.ApiException)6 StatusRuntimeException (io.grpc.StatusRuntimeException)6 Subscription (com.google.pubsub.v1.Subscription)5 ArrayList (java.util.ArrayList)5 ListTopicsPagedResponse (com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse)3 Publisher (com.google.cloud.pubsub.spi.v1.Publisher)3 SubscriptionAdminClient (com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient)3 DeleteTopicRequest (com.google.pubsub.v1.DeleteTopicRequest)3 ListTopicsRequest (com.google.pubsub.v1.ListTopicsRequest)3 PublishRequest (com.google.pubsub.v1.PublishRequest)3 PublishResponse (com.google.pubsub.v1.PublishResponse)3 AckReplyConsumer (com.google.cloud.pubsub.spi.v1.AckReplyConsumer)2