Search in sources :

Example 16 with Subscriber

use of com.google.cloud.pubsub.spi.v1.Subscriber in project beam by apache.

the class TestPubsubSignal method pollForResultForDuration.

private String pollForResultForDuration(SubscriptionPath signalSubscriptionPath, Duration timeoutDuration) throws IOException {
    AtomicReference<String> result = new AtomicReference<>(null);
    MessageReceiver receiver = (com.google.pubsub.v1.PubsubMessage message, AckReplyConsumer replyConsumer) -> {
        LOG.info("Received message: {}", message.getData().toStringUtf8());
        // Ignore empty messages
        if (message.getData().isEmpty()) {
            replyConsumer.ack();
        }
        if (result.compareAndSet(null, message.getData().toStringUtf8())) {
            replyConsumer.ack();
        } else {
            replyConsumer.nack();
        }
    };
    Subscriber subscriber = Subscriber.newBuilder(signalSubscriptionPath.getPath(), receiver).setCredentialsProvider(pipelineOptions::getGcpCredential).setEndpoint(pubsubEndpoint).build();
    subscriber.startAsync();
    DateTime startTime = new DateTime();
    int timeoutSeconds = timeoutDuration.toStandardSeconds().getSeconds();
    while (result.get() == null && Seconds.secondsBetween(startTime, new DateTime()).getSeconds() < timeoutSeconds) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ignored) {
        }
    }
    subscriber.stopAsync();
    subscriber.awaitTerminated();
    if (result.get() == null) {
        throw new AssertionError(String.format("Did not receive signal on %s in %ss", signalSubscriptionPath, timeoutDuration.getStandardSeconds()));
    }
    return result.get();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) DateTime(org.joda.time.DateTime) MessageReceiver(com.google.cloud.pubsub.v1.MessageReceiver) Subscriber(com.google.cloud.pubsub.v1.Subscriber) AckReplyConsumer(com.google.cloud.pubsub.v1.AckReplyConsumer)

Example 17 with Subscriber

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

the class PubsubBenchWrapperImpl method recv.

public void recv(PubsubRecv request, StreamObserver<EmptyResponse> responseObserver) {
    System.out.println("recv has been called");
    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of("some-project", request.getSubName());
    Subscriber subscriber = null;
    try {
        InstantiatingExecutorProvider executorProvider = InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(1).build();
        subscriber = Subscriber.newBuilder(subscriptionName, new SimpleReceiver()).setExecutorProvider(executorProvider).build();
        subscriber.startAsync().awaitRunning();
        // Allow the subscriber to run indefinitely unless an unrecoverable error occurs.
        subscriber.awaitTerminated();
    } catch (IllegalStateException e) {
        System.out.println("Subscriber unexpectedly stopped: " + e);
    }
    EmptyResponse reply = EmptyResponse.newBuilder().build();
    responseObserver.onNext(reply);
    responseObserver.onCompleted();
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) Subscriber(com.google.cloud.pubsub.v1.Subscriber) InstantiatingExecutorProvider(com.google.api.gax.core.InstantiatingExecutorProvider)

Example 18 with Subscriber

use of com.google.cloud.pubsub.spi.v1.Subscriber in project spring-cloud-gcp by spring-cloud.

the class PubSubTemplateDocumentationTests method subscribeSimpleTest.

@Test
public void subscribeSimpleTest() {
    pubSubTest((PubSubTemplate pubSubTemplate, String subscriptionName, String topicName) -> {
        pubSubTemplate.publish(topicName, "message");
        Logger logger = new Logger();
        // tag::subscribe[]
        Subscriber subscriber = pubSubTemplate.subscribe(subscriptionName, (message) -> {
            logger.info("Message received from " + subscriptionName + " subscription: " + message.getPubsubMessage().getData().toStringUtf8());
            message.ack();
        });
        // end::subscribe[]
        List<String> messages = logger.getMessages();
        Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> !messages.isEmpty());
        assertThat(messages).containsExactly("Message received from " + subscriptionName + " subscription: message");
    });
}
Also used : Subscriber(com.google.cloud.pubsub.v1.Subscriber) ByteString(com.google.protobuf.ByteString) PubSubTemplate(org.springframework.cloud.gcp.pubsub.core.PubSubTemplate) Test(org.junit.Test)

Example 19 with Subscriber

use of com.google.cloud.pubsub.spi.v1.Subscriber in project spring-cloud-gcp by spring-cloud.

the class PubSubSubscriberTemplate method subscribe.

@Override
public Subscriber subscribe(String subscription, Consumer<BasicAcknowledgeablePubsubMessage> messageConsumer) {
    Assert.notNull(messageConsumer, "The messageConsumer can't be null.");
    Subscriber subscriber = this.subscriberFactory.createSubscriber(subscription, (message, ackReplyConsumer) -> messageConsumer.accept(new PushedAcknowledgeablePubsubMessage(PubSubSubscriptionUtils.toProjectSubscriptionName(subscription, this.subscriberFactory.getProjectId()), message, ackReplyConsumer)));
    subscriber.startAsync();
    return subscriber;
}
Also used : Subscriber(com.google.cloud.pubsub.v1.Subscriber)

Example 20 with Subscriber

use of com.google.cloud.pubsub.spi.v1.Subscriber in project spring-cloud-gcp by spring-cloud.

the class DefaultSubscriberFactoryTests method testNewSubscriber.

@Test
public void testNewSubscriber() {
    DefaultSubscriberFactory factory = new DefaultSubscriberFactory(() -> "angeldust");
    factory.setCredentialsProvider(this.credentialsProvider);
    Subscriber subscriber = factory.createSubscriber("midnight cowboy", (message, consumer) -> {
    });
    assertThat(subscriber.getSubscriptionNameString()).isEqualTo("projects/angeldust/subscriptions/midnight cowboy");
}
Also used : Subscriber(com.google.cloud.pubsub.v1.Subscriber) Test(org.junit.Test)

Aggregations

Subscriber (com.google.cloud.pubsub.v1.Subscriber)25 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)13 ArrayList (java.util.ArrayList)9 List (java.util.List)9 Test (org.junit.Test)9 SettableApiFuture (com.google.api.core.SettableApiFuture)8 ServiceOptions (com.google.cloud.ServiceOptions)8 DlpServiceClient (com.google.cloud.dlp.v2.DlpServiceClient)8 Action (com.google.privacy.dlp.v2.Action)8 BigQueryTable (com.google.privacy.dlp.v2.BigQueryTable)8 CreateDlpJobRequest (com.google.privacy.dlp.v2.CreateDlpJobRequest)8 DlpJob (com.google.privacy.dlp.v2.DlpJob)8 GetDlpJobRequest (com.google.privacy.dlp.v2.GetDlpJobRequest)8 InfoType (com.google.privacy.dlp.v2.InfoType)8 ProjectName (com.google.privacy.dlp.v2.ProjectName)8 ProjectTopicName (com.google.pubsub.v1.ProjectTopicName)8 Collections (java.util.Collections)8 TimeUnit (java.util.concurrent.TimeUnit)8 CommandLine (org.apache.commons.cli.CommandLine)8 CommandLineParser (org.apache.commons.cli.CommandLineParser)8