use of com.google.pubsub.v1.SubscriptionName in project flink by apache.
the class PubsubHelper method acknowledgeIds.
private void acknowledgeIds(SubscriberStub subscriber, String subscriptionName, List<ReceivedMessage> receivedMessages) {
if (receivedMessages.isEmpty()) {
return;
}
List<String> ackIds = receivedMessages.stream().map(ReceivedMessage::getAckId).collect(Collectors.toList());
// acknowledge received messages
AcknowledgeRequest acknowledgeRequest = AcknowledgeRequest.newBuilder().setSubscription(subscriptionName).addAllAckIds(ackIds).build();
// use acknowledgeCallable().futureCall to asynchronously perform this operation
subscriber.acknowledgeCallable().call(acknowledgeRequest);
}
use of com.google.pubsub.v1.SubscriptionName in project flink by apache.
the class PubsubHelper method subscribeToSubscription.
public Subscriber subscribeToSubscription(String project, String subscription, MessageReceiver messageReceiver) {
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(project, subscription);
Subscriber subscriber = Subscriber.newBuilder(subscriptionName, messageReceiver).setChannelProvider(channelProvider).setCredentialsProvider(EmulatorCredentialsProvider.create()).build();
subscriber.startAsync();
return subscriber;
}
use of com.google.pubsub.v1.SubscriptionName in project spring-cloud-gcp by spring-cloud.
the class PubSubSampleApplicationTests method getMessagesFromSubscription.
private List<String> getMessagesFromSubscription(String subscriptionName) {
String projectSubscriptionName = ProjectSubscriptionName.format(projectName, subscriptionName);
PullRequest pullRequest = PullRequest.newBuilder().setReturnImmediately(true).setMaxMessages(10).setSubscription(projectSubscriptionName).build();
PullResponse pullResponse = subscriptionAdminClient.getStub().pullCallable().call(pullRequest);
return pullResponse.getReceivedMessagesList().stream().map((message) -> message.getMessage().getData().toStringUtf8()).collect(Collectors.toList());
}
use of com.google.pubsub.v1.SubscriptionName in project spring-cloud-gcp by spring-cloud.
the class PubSubChannelProvisioner method provisionConsumerDestination.
@Override
public ConsumerDestination provisionConsumerDestination(String topicName, String group, ExtendedConsumerProperties<PubSubConsumerProperties> properties) {
// topicName may be either the short or fully-qualified version.
String topicShortName = TopicName.isParsableFrom(topicName) ? TopicName.parse(topicName).getTopic() : topicName;
Optional<Topic> topic = ensureTopicExists(topicName, properties.getExtension().isAutoCreateResources());
String subscriptionName;
Subscription subscription;
if (StringUtils.hasText(group)) {
subscriptionName = topicShortName + "." + group;
subscription = this.pubSubAdmin.getSubscription(subscriptionName);
} else {
// Generate anonymous random group since one wasn't provided
subscriptionName = "anonymous." + topicShortName + "." + UUID.randomUUID().toString();
subscription = this.pubSubAdmin.createSubscription(subscriptionName, topicName);
this.anonymousGroupSubscriptionNames.add(subscriptionName);
}
if (subscription == null) {
if (properties.getExtension().isAutoCreateResources()) {
this.pubSubAdmin.createSubscription(subscriptionName, topicName);
} else {
throw new ProvisioningException("Non-existing '" + subscriptionName + "' subscription.");
}
} else if (topic.isPresent() && !subscription.getTopic().equals(topic.get().getName())) {
throw new ProvisioningException("Existing '" + subscriptionName + "' subscription is for a different topic '" + subscription.getTopic() + "'.");
}
return new PubSubConsumerDestination(subscriptionName);
}
use of com.google.pubsub.v1.SubscriptionName in project spring-cloud-gcp by spring-cloud.
the class PubSubTemplateDocumentationTests method testCreatePublishPullNextAndDelete.
@Test
public void testCreatePublishPullNextAndDelete() {
pubSubTest((PubSubTemplate pubSubTemplate, String subscriptionName, String topicName) -> {
// tag::publish[]
Map<String, String> headers = Collections.singletonMap("key1", "val1");
pubSubTemplate.publish(topicName, "message", headers).get();
// end::publish[]
PubsubMessage pubsubMessage = pubSubTemplate.pullNext(subscriptionName);
assertThat(pubsubMessage.getData()).isEqualTo(ByteString.copyFromUtf8("message"));
assertThat(pubsubMessage.getAttributesCount()).isEqualTo(1);
assertThat(pubsubMessage.getAttributesOrThrow("key1")).isEqualTo("val1");
});
}
Aggregations