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;
}
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();
}
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));
}
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]
}
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;
}
Aggregations