Search in sources :

Example 6 with Subscriber

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

the class SubscriberSnippets method createSubscriberWithCustomFlowSettings.

private Subscriber createSubscriberWithCustomFlowSettings() throws Exception {
    // [START pubsub_subscriber_flow_settings]
    int maxMessageCount = 10;
    // Configure max number of messages to be pulled
    FlowControlSettings flowControlSettings = FlowControlSettings.newBuilder().setMaxOutstandingElementCount(maxMessageCount).build();
    Subscriber subscriber = Subscriber.defaultBuilder(subscriptionName, receiver).setFlowControlSettings(flowControlSettings).build();
    // [END pubsub_subscriber_flow_settings]
    return subscriber;
}
Also used : FlowControlSettings(com.google.api.gax.batching.FlowControlSettings) Subscriber(com.google.cloud.pubsub.spi.v1.Subscriber)

Example 7 with Subscriber

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

the class SubscriberTest method testModifyAckDeadline.

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

        @Override
        public ModifyAckDeadline apply(String ack) {
            // It is expected that the deadline is renewed
            return new ModifyAckDeadline(ack, 3);
        // only three more seconds to not pass the max
        // ack deadline ext.
        }
    });
    // 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 8 with Subscriber

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

the class SubscriberTest method testReceiverError_NacksMessage.

@Test
public void testReceiverError_NacksMessage() throws Exception {
    testReceiver.setErrorReply(new RuntimeException("Can't process message"));
    Subscriber subscriber = startSubscriber(getTestSubscriberBuilder(testReceiver));
    sendMessages(ImmutableList.of("A"));
    // Trigger nack sending
    subscriber.stopAsync().awaitTerminated();
    assertEquivalent(ImmutableList.of(new ModifyAckDeadline("A", 0)), fakeSubscriberServiceImpl.waitAndConsumeModifyAckDeadlines(1));
}
Also used : StatusRuntimeException(io.grpc.StatusRuntimeException) ModifyAckDeadline(com.google.cloud.pubsub.spi.v1.FakeSubscriberServiceImpl.ModifyAckDeadline) Test(org.junit.Test)

Example 9 with Subscriber

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

the class SubscriberSnippets method createSubscriber.

private void createSubscriber() throws Exception {
    // [START pubsub_pull]
    String projectId = "my-project-id";
    String subscriptionId = "my-subscription-id";
    SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
    // Instantiate an asynchronous message receiver
    MessageReceiver receiver = new MessageReceiver() {

        @Override
        public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
            // handle incoming message, then ack/nack the received message
            System.out.println("Id : " + message.getMessageId());
            System.out.println("Data : " + message.getData().toStringUtf8());
            consumer.ack();
        }
    };
    Subscriber subscriber = null;
    try {
        // Create a subscriber for "my-subscription-id" bound to the message receiver
        subscriber = Subscriber.defaultBuilder(subscriptionName, receiver).build();
        subscriber.startAsync();
    // ...
    } finally {
        // stop receiving messages
        if (subscriber != null) {
            subscriber.stopAsync();
        }
    }
// [END pubsub_pull]
}
Also used : MessageReceiver(com.google.cloud.pubsub.spi.v1.MessageReceiver) Subscriber(com.google.cloud.pubsub.spi.v1.Subscriber) SubscriptionName(com.google.pubsub.v1.SubscriptionName) AckReplyConsumer(com.google.cloud.pubsub.spi.v1.AckReplyConsumer) PubsubMessage(com.google.pubsub.v1.PubsubMessage)

Example 10 with Subscriber

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

the class SubscriberSnippets method createSingleThreadedSubscriber.

private Subscriber createSingleThreadedSubscriber() throws Exception {
    // [START pubsub_subscriber_single_threaded]
    // provide a separate executor service for polling
    ExecutorProvider executorProvider = InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(1).build();
    Subscriber subscriber = Subscriber.defaultBuilder(subscriptionName, receiver).setExecutorProvider(executorProvider).build();
    // [END pubsub_subscriber_single_threaded]
    return subscriber;
}
Also used : Subscriber(com.google.cloud.pubsub.spi.v1.Subscriber) ExecutorProvider(com.google.api.gax.grpc.ExecutorProvider) InstantiatingExecutorProvider(com.google.api.gax.grpc.InstantiatingExecutorProvider)

Aggregations

Subscriber (com.google.cloud.pubsub.spi.v1.Subscriber)7 PubsubMessage (com.google.pubsub.v1.PubsubMessage)4 Test (org.junit.Test)4 AckReplyConsumer (com.google.cloud.pubsub.spi.v1.AckReplyConsumer)3 ModifyAckDeadline (com.google.cloud.pubsub.spi.v1.FakeSubscriberServiceImpl.ModifyAckDeadline)3 MessageReceiver (com.google.cloud.pubsub.spi.v1.MessageReceiver)3 SubscriptionName (com.google.pubsub.v1.SubscriptionName)3 Publisher (com.google.cloud.pubsub.spi.v1.Publisher)2 TopicName (com.google.pubsub.v1.TopicName)2 FlowControlSettings (com.google.api.gax.batching.FlowControlSettings)1 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)1 FixedCredentialsProvider (com.google.api.gax.core.FixedCredentialsProvider)1 ChannelProvider (com.google.api.gax.grpc.ChannelProvider)1 ExecutorProvider (com.google.api.gax.grpc.ExecutorProvider)1 InstantiatingExecutorProvider (com.google.api.gax.grpc.InstantiatingExecutorProvider)1 SubscriptionAdminClient (com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 FileInputStream (java.io.FileInputStream)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1