Search in sources :

Example 1 with Subscriber

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

the class ITPubSubTest method testPublishSubscribe.

@Test
public void testPublishSubscribe() throws Exception {
    TopicName topicName = TopicName.create(projectId, formatForTest("testing-publish-subscribe-topic"));
    SubscriptionName subscriptionName = SubscriptionName.create(projectId, formatForTest("testing-publish-subscribe-subscription"));
    topicAdminClient.createTopic(topicName);
    subscriptionAdminClient.createSubscription(subscriptionName, topicName, PushConfig.newBuilder().build(), 10);
    PubsubMessage message = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8("my message")).build();
    final SettableApiFuture<PubsubMessage> received = SettableApiFuture.create();
    Subscriber subscriber = Subscriber.defaultBuilder(subscriptionName, new MessageReceiver() {

        @Override
        public void receiveMessage(final PubsubMessage message, final AckReplyConsumer consumer) {
            if (received.set(message)) {
                consumer.ack();
            } else {
                consumer.nack();
            }
        }
    }).build();
    subscriber.addListener(new Subscriber.Listener() {

        public void failed(Subscriber.State from, Throwable failure) {
            received.setException(failure);
        }
    }, MoreExecutors.directExecutor());
    subscriber.startAsync();
    Publisher publisher = Publisher.defaultBuilder(topicName).build();
    publisher.publish(message).get();
    publisher.shutdown();
    assertEquals(received.get().getData(), message.getData());
    subscriber.stopAsync().awaitTerminated();
    subscriptionAdminClient.deleteSubscription(subscriptionName);
    topicAdminClient.deleteTopic(topicName);
}
Also used : Subscriber(com.google.cloud.pubsub.spi.v1.Subscriber) MessageReceiver(com.google.cloud.pubsub.spi.v1.MessageReceiver) SubscriptionName(com.google.pubsub.v1.SubscriptionName) Publisher(com.google.cloud.pubsub.spi.v1.Publisher) AckReplyConsumer(com.google.cloud.pubsub.spi.v1.AckReplyConsumer) PubsubMessage(com.google.pubsub.v1.PubsubMessage) TopicName(com.google.pubsub.v1.TopicName) Test(org.junit.Test)

Example 2 with Subscriber

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

the class SubscriberTest method testModifyAckDeadline_defaultMaxExtensionPeriod.

@Test
public void testModifyAckDeadline_defaultMaxExtensionPeriod() throws Exception {
    Subscriber subscriber = startSubscriber(getTestSubscriberBuilder(testReceiver).setAckExpirationPadding(Duration.ofSeconds(1)));
    // Send messages to be acked
    List<String> testAckIdsBatch = ImmutableList.of("A", "B", "C");
    testReceiver.setExplicitAck(true);
    // A modify ack deadline should be schedule for the next 9s
    fakeExecutor.setupScheduleExpectation(Duration.ofSeconds(9));
    sendMessages(testAckIdsBatch);
    // To ensure the first modify ack deadlines got scheduled
    fakeExecutor.waitForExpectedWork();
    // Next modify ack deadline should be schedule in the next 1s
    fakeExecutor.advanceTime(Duration.ofSeconds(9));
    assertEquivalentWithTransformation(testAckIdsBatch, fakeSubscriberServiceImpl.waitAndConsumeModifyAckDeadlines(3), new Function<String, ModifyAckDeadline>() {

        @Override
        public ModifyAckDeadline apply(String ack) {
            return new ModifyAckDeadline(ack, INITIAL_ACK_DEADLINE_EXTENSION_SECS);
        }
    });
    fakeExecutor.advanceTime(Duration.ofSeconds(1));
    // Second time increment
    int timeIncrementSecs = INITIAL_ACK_DEADLINE_EXTENSION_SECS;
    // Check ack deadline extensions while the current time has not reached 60 minutes
    while (fakeExecutor.getClock().millisTime() + timeIncrementSecs - 1 < 1000 * 60 * 60) {
        timeIncrementSecs *= 2;
        final int expectedIncrementSecs = Math.min(600, timeIncrementSecs);
        assertEquivalentWithTransformation(testAckIdsBatch, fakeSubscriberServiceImpl.waitAndConsumeModifyAckDeadlines(3), new Function<String, ModifyAckDeadline>() {

            @Override
            public ModifyAckDeadline apply(String ack) {
                return new ModifyAckDeadline(ack, expectedIncrementSecs);
            }
        });
        fakeExecutor.advanceTime(Duration.ofSeconds(timeIncrementSecs - 1));
    }
    // No more modify ack deadline extension should be triggered at this point
    fakeExecutor.advanceTime(Duration.ofSeconds(20));
    assertTrue(fakeSubscriberServiceImpl.getModifyAckDeadlines().isEmpty());
    testReceiver.replyAllOutstandingMessage();
    subscriber.stopAsync().awaitTerminated();
}
Also used : ModifyAckDeadline(com.google.cloud.pubsub.spi.v1.FakeSubscriberServiceImpl.ModifyAckDeadline) Test(org.junit.Test)

Example 3 with Subscriber

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

the class ITPubSubSnippets method testPublisherSubscriberHelper.

private void testPublisherSubscriberHelper(TopicName topicName, SubscriptionName subscriptionName) throws Exception {
    String messageToPublish = "my-message";
    Publisher publisher = null;
    try {
        publisher = Publisher.defaultBuilder(topicName).build();
        PublisherSnippets snippets = new PublisherSnippets(publisher);
        final SettableApiFuture<Void> done = SettableApiFuture.create();
        ApiFutures.addCallback(snippets.publish(messageToPublish), new ApiFutureCallback<String>() {

            public void onSuccess(String messageId) {
                done.set(null);
            }

            public void onFailure(Throwable t) {
                done.setException(t);
            }
        });
        done.get();
    } finally {
        if (publisher != null) {
            publisher.shutdown();
        }
    }
    final BlockingQueue<PubsubMessage> queue = new ArrayBlockingQueue<>(1);
    final SettableApiFuture<Void> done = SettableApiFuture.create();
    final SettableApiFuture<PubsubMessage> received = SettableApiFuture.create();
    SubscriberSnippets snippets = new SubscriberSnippets(subscriptionName, new MessageReceiverSnippets(queue).messageReceiver(), done, MoreExecutors.directExecutor());
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                received.set(queue.poll(10, TimeUnit.MINUTES));
            } catch (InterruptedException e) {
                received.set(null);
            }
            // signal the subscriber to clean up
            done.set(null);
        }
    }).start();
    // blocks until done is set
    snippets.startAndWait();
    PubsubMessage message = received.get();
    assertNotNull(message);
    assertEquals(message.getData().toStringUtf8(), messageToPublish);
}
Also used : Publisher(com.google.cloud.pubsub.spi.v1.Publisher) PubsubMessage(com.google.pubsub.v1.PubsubMessage) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue)

Example 4 with Subscriber

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

the class CreateSubscriptionAndConsumeMessages method main.

public static void main(String... args) throws Exception {
    TopicName topic = TopicName.create("my-project-id", "my-topic-id");
    SubscriptionName subscription = SubscriptionName.create("my-project-id", "my-topic-id");
    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
        subscriptionAdminClient.createSubscription(subscription, topic, PushConfig.getDefaultInstance(), 0);
    }
    MessageReceiver receiver = new MessageReceiver() {

        @Override
        public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
            System.out.println("Received message: " + message.getData().toStringUtf8());
            consumer.ack();
        }
    };
    Subscriber subscriber = null;
    try {
        subscriber = Subscriber.defaultBuilder(subscription, receiver).build();
        subscriber.addListener(new Subscriber.Listener() {

            @Override
            public void failed(Subscriber.State from, Throwable failure) {
                // Handle failure. This is called when the Subscriber encountered a fatal error and is shutting down.
                System.err.println(failure);
            }
        }, MoreExecutors.directExecutor());
        subscriber.startAsync().awaitRunning();
        Thread.sleep(60000);
    } finally {
        if (subscriber != null) {
            subscriber.stopAsync();
        }
    }
}
Also used : MessageReceiver(com.google.cloud.pubsub.spi.v1.MessageReceiver) Subscriber(com.google.cloud.pubsub.spi.v1.Subscriber) SubscriptionAdminClient(com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient) SubscriptionName(com.google.pubsub.v1.SubscriptionName) AckReplyConsumer(com.google.cloud.pubsub.spi.v1.AckReplyConsumer) PubsubMessage(com.google.pubsub.v1.PubsubMessage) TopicName(com.google.pubsub.v1.TopicName)

Example 5 with Subscriber

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

the class SubscriberSnippets method startAndWait.

// [TARGET startAsync()]
public void startAndWait() throws Exception {
    Subscriber subscriber = Subscriber.defaultBuilder(subscriptionName, receiver).build();
    subscriber.addListener(new Subscriber.Listener() {

        public void failed(Subscriber.State from, Throwable failure) {
        // Handle error.
        }
    }, executor);
    subscriber.startAsync();
    // Wait for a stop signal.
    done.get();
    subscriber.stopAsync().awaitTerminated();
}
Also used : Subscriber(com.google.cloud.pubsub.spi.v1.Subscriber)

Aggregations

Subscriber (com.google.cloud.pubsub.v1.Subscriber)20 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)13 ArrayList (java.util.ArrayList)9 List (java.util.List)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 DefaultParser (org.apache.commons.cli.DefaultParser)8