com.google.cloud.pubsub.v1.stub.SubscriberStub Java Examples
The following examples show how to use
com.google.cloud.pubsub.v1.stub.SubscriberStub.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PubsubHelper.java From flink with Apache License 2.0 | 6 votes |
public List<ReceivedMessage> pullMessages(String projectId, String subscriptionId, int maxNumberOfMessages) throws Exception { SubscriberStubSettings subscriberStubSettings = SubscriberStubSettings.newBuilder() .setTransportChannelProvider(channelProvider) .setCredentialsProvider(NoCredentialsProvider.create()) .build(); try (SubscriberStub subscriber = GrpcSubscriberStub.create(subscriberStubSettings)) { String subscriptionName = ProjectSubscriptionName.format(projectId, subscriptionId); PullRequest pullRequest = PullRequest.newBuilder() .setMaxMessages(maxNumberOfMessages) .setReturnImmediately(false) .setSubscription(subscriptionName) .build(); List<ReceivedMessage> receivedMessages = subscriber.pullCallable().call(pullRequest).getReceivedMessagesList(); acknowledgeIds(subscriber, subscriptionName, receivedMessages); return receivedMessages; } }
Example #2
Source File: PubsubHelper.java From flink with Apache License 2.0 | 5 votes |
public List<ReceivedMessage> pullMessages(String projectId, String subscriptionId, int maxNumberOfMessages) throws Exception { SubscriberStubSettings subscriberStubSettings = SubscriberStubSettings.newBuilder() .setTransportChannelProvider(channelProvider) .setCredentialsProvider(NoCredentialsProvider.create()) .build(); try (SubscriberStub subscriber = GrpcSubscriberStub.create(subscriberStubSettings)) { // String projectId = "my-project-id"; // String subscriptionId = "my-subscription-id"; // int numOfMessages = 10; // max number of messages to be pulled String subscriptionName = ProjectSubscriptionName.format(projectId, subscriptionId); PullRequest pullRequest = PullRequest.newBuilder() .setMaxMessages(maxNumberOfMessages) .setReturnImmediately(false) // return immediately if messages are not available .setSubscription(subscriptionName) .build(); // use pullCallable().futureCall to asynchronously perform this operation PullResponse pullResponse = subscriber.pullCallable().call(pullRequest); List<String> ackIds = new ArrayList<>(); for (ReceivedMessage message : pullResponse.getReceivedMessagesList()) { // handle received message // ... ackIds.add(message.getAckId()); } // acknowledge received messages AcknowledgeRequest acknowledgeRequest = AcknowledgeRequest.newBuilder() .setSubscription(subscriptionName) .addAllAckIds(ackIds) .build(); // use acknowledgeCallable().futureCall to asynchronously perform this operation subscriber.acknowledgeCallable().call(acknowledgeRequest); return pullResponse.getReceivedMessagesList(); } }
Example #3
Source File: PubSubExtendedBindingsPropertiesTests.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
@Bean public PubSubTemplate pubSubTemplate() { PublisherFactory publisherFactory = Mockito.mock(PublisherFactory.class); SubscriberFactory subscriberFactory = Mockito.mock(SubscriberFactory.class); when(subscriberFactory.getProjectId()).thenReturn("test-project"); when(subscriberFactory.createSubscriberStub()) .thenReturn(Mockito.mock(SubscriberStub.class)); when(subscriberFactory.createSubscriber(anyString(), any())) .thenReturn(Mockito.mock(Subscriber.class)); return new PubSubTemplate( new PubSubPublisherTemplate(publisherFactory), new PubSubSubscriberTemplate(subscriberFactory)); }
Example #4
Source File: DefaultSubscriberFactory.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
@Override public SubscriberStub createSubscriberStub() { SubscriberStubSettings.Builder subscriberStubSettings = SubscriberStubSettings.newBuilder(); if (this.credentialsProvider != null) { subscriberStubSettings.setCredentialsProvider(this.credentialsProvider); } if (this.pullEndpoint != null) { subscriberStubSettings.setEndpoint(this.pullEndpoint); } if (this.executorProvider != null) { subscriberStubSettings.setExecutorProvider(this.executorProvider); } if (this.headerProvider != null) { subscriberStubSettings.setHeaderProvider(this.headerProvider); } if (this.channelProvider != null) { subscriberStubSettings.setTransportChannelProvider(this.channelProvider); } if (this.apiClock != null) { subscriberStubSettings.setClock(this.apiClock); } if (this.subscriberStubRetrySettings != null) { subscriberStubSettings.pullSettings().setRetrySettings( this.subscriberStubRetrySettings); } try { return GrpcSubscriberStub.create(subscriberStubSettings.build()); } catch (IOException ex) { throw new RuntimeException("Error creating the SubscriberStub", ex); } }
Example #5
Source File: PubsubHelper.java From flink with Apache License 2.0 | 5 votes |
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); }
Example #6
Source File: ConsumeGCPubSub.java From nifi with Apache License 2.0 | 5 votes |
private SubscriberStub getSubscriber(ProcessContext context) throws IOException { final SubscriberStubSettings subscriberStubSettings = SubscriberStubSettings.newBuilder() .setCredentialsProvider(FixedCredentialsProvider.create(getGoogleCredentials(context))) .build(); return GrpcSubscriberStub.create(subscriberStubSettings); }
Example #7
Source File: Poller.java From spanner-event-exporter with Apache License 2.0 | 4 votes |
private String getLastProcessedTimestamp() { String timestamp = ""; try { final SubscriberStubSettings subscriberStubSettings = SubscriberStubSettings.newBuilder() .setTransportChannelProvider( SubscriberStubSettings.defaultGrpcTransportProviderBuilder() .setMaxInboundMessageSize(20 << 20) // 20MB .build()) .build(); try (SubscriberStub subscriber = GrpcSubscriberStub.create(subscriberStubSettings)) { final String subscriptionName = ProjectSubscriptionName.format(PROJECT_ID, tableName); final PullRequest pullRequest = PullRequest.newBuilder() .setMaxMessages(1) .setReturnImmediately(true) .setSubscription(subscriptionName) .build(); final PullResponse pullResponse = subscriber.pullCallable().call(pullRequest); final DatumReader<GenericRecord> datumReader = new GenericDatumReader<GenericRecord>(avroSchema); for (ReceivedMessage message : pullResponse.getReceivedMessagesList()) { final JsonDecoder decoder = DecoderFactory.get() .jsonDecoder(avroSchema, message.getMessage().getData().newInput()); final GenericRecord record = datumReader.read(null, decoder); timestamp = record.get("Timestamp").toString(); log.debug("---------------- Got Timestamp: " + timestamp); } } } catch (IOException e) { log.error("Could not get last processed timestamp from pub / sub", e); // If we cannot find a previously processed timestamp, we will default // to the one present in the config file. return startingTimestamp; } return timestamp; }
Example #8
Source File: SubscriberFactory.java From spring-cloud-gcp with Apache License 2.0 | 2 votes |
/** * Create a {@link SubscriberStub} that is needed to execute {@link PullRequest}s. * @return the {@link SubscriberStub} used for executing {@link PullRequest}s */ SubscriberStub createSubscriberStub();