use of com.google.cloud.pubsub.spi.v1.Subscriber in project spring-cloud-gcp by spring-cloud.
the class PubSubSubscriberTemplate method subscribeAndConvert.
@Override
public <T> Subscriber subscribeAndConvert(String subscription, Consumer<ConvertedBasicAcknowledgeablePubsubMessage<T>> messageConsumer, Class<T> payloadType) {
Assert.notNull(messageConsumer, "The messageConsumer can't be null.");
Subscriber subscriber = this.subscriberFactory.createSubscriber(subscription, (message, ackReplyConsumer) -> messageConsumer.accept(new ConvertedPushedAcknowledgeablePubsubMessage<>(PubSubSubscriptionUtils.toProjectSubscriptionName(subscription, this.subscriberFactory.getProjectId()), message, this.getMessageConverter().fromPubSubMessage(message, payloadType), ackReplyConsumer)));
subscriber.startAsync();
return subscriber;
}
use of com.google.cloud.pubsub.spi.v1.Subscriber in project spring-cloud-gcp by spring-cloud.
the class PubSubSubscriberTemplate method subscribe.
@Override
@Deprecated
public Subscriber subscribe(String subscription, MessageReceiver messageReceiver) {
Assert.hasText(subscription, "The subscription can't be null or empty.");
Assert.notNull(messageReceiver, "The messageReceiver can't be null.");
Subscriber subscriber = this.subscriberFactory.createSubscriber(subscription, messageReceiver);
subscriber.startAsync();
return 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]
}
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;
}
use of com.google.cloud.pubsub.spi.v1.Subscriber in project google-cloud-java by GoogleCloudPlatform.
the class SubscriberSnippets method createSubscriberWithCustomCredentials.
private Subscriber createSubscriberWithCustomCredentials() throws Exception {
// [START pubsub_subscriber_custom_credentials]
CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(new FileInputStream("credentials.json")));
ChannelProvider channelProvider = TopicAdminSettings.defaultChannelProviderBuilder().setCredentialsProvider(credentialsProvider).build();
Subscriber subscriber = Subscriber.defaultBuilder(subscriptionName, receiver).setChannelProvider(channelProvider).build();
// [START pubsub_subscriber_custom_credentials]
return subscriber;
}
Aggregations