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