Java Code Examples for com.google.pubsub.v1.PullResponse#getReceivedMessagesList()
The following examples show how to use
com.google.pubsub.v1.PullResponse#getReceivedMessagesList() .
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 | 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 2
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 3
Source File: PubsubGrpcClient.java From beam with Apache License 2.0 | 4 votes |
@Override public List<IncomingMessage> pull( long requestTimeMsSinceEpoch, SubscriptionPath subscription, int batchSize, boolean returnImmediately) throws IOException { PullRequest request = PullRequest.newBuilder() .setSubscription(subscription.getPath()) .setReturnImmediately(returnImmediately) .setMaxMessages(batchSize) .build(); PullResponse response = subscriberStub().pull(request); if (response.getReceivedMessagesCount() == 0) { return ImmutableList.of(); } List<IncomingMessage> incomingMessages = new ArrayList<>(response.getReceivedMessagesCount()); for (ReceivedMessage message : response.getReceivedMessagesList()) { PubsubMessage pubsubMessage = message.getMessage(); @Nullable Map<String, String> attributes = pubsubMessage.getAttributes(); // Timestamp. String pubsubTimestampString = null; Timestamp timestampProto = pubsubMessage.getPublishTime(); if (timestampProto != null) { pubsubTimestampString = String.valueOf(timestampProto.getSeconds() + timestampProto.getNanos() / 1000L); } long timestampMsSinceEpoch = extractTimestamp(timestampAttribute, pubsubTimestampString, attributes); // Ack id. String ackId = message.getAckId(); checkState(!Strings.isNullOrEmpty(ackId)); // Record id, if any. @Nullable String recordId = null; if (idAttribute != null && attributes != null) { recordId = attributes.get(idAttribute); } if (Strings.isNullOrEmpty(recordId)) { // Fall back to the Pubsub provided message id. recordId = pubsubMessage.getMessageId(); } incomingMessages.add( IncomingMessage.of( pubsubMessage, timestampMsSinceEpoch, requestTimeMsSinceEpoch, ackId, recordId)); } return incomingMessages; }
Example 4
Source File: ConsumeGCPubSub.java From nifi with Apache License 2.0 | 4 votes |
@Override public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { if (subscriber == null) { if (storedException.get() != null) { getLogger().error("Failed to create Google Cloud PubSub subscriber due to {}", new Object[]{storedException.get()}); } else { getLogger().error("Google Cloud PubSub Subscriber was not properly created. Yielding the processor..."); } context.yield(); return; } final PullResponse pullResponse = subscriber.pullCallable().call(pullRequest); final List<String> ackIds = new ArrayList<>(); for (ReceivedMessage message : pullResponse.getReceivedMessagesList()) { if (message.hasMessage()) { FlowFile flowFile = session.create(); final Map<String, String> attributes = new HashMap<>(); ackIds.add(message.getAckId()); attributes.put(ACK_ID_ATTRIBUTE, message.getAckId()); attributes.put(SERIALIZED_SIZE_ATTRIBUTE, String.valueOf(message.getSerializedSize())); attributes.put(MESSAGE_ID_ATTRIBUTE, message.getMessage().getMessageId()); attributes.put(MSG_ATTRIBUTES_COUNT_ATTRIBUTE, String.valueOf(message.getMessage().getAttributesCount())); attributes.put(MSG_PUBLISH_TIME_ATTRIBUTE, String.valueOf(message.getMessage().getPublishTime().getSeconds())); attributes.putAll(message.getMessage().getAttributesMap()); flowFile = session.putAllAttributes(flowFile, attributes); flowFile = session.write(flowFile, out -> out.write(message.getMessage().getData().toByteArray())); session.transfer(flowFile, REL_SUCCESS); session.getProvenanceReporter().receive(flowFile, getSubscriptionName(context)); } } if (!ackIds.isEmpty()) { AcknowledgeRequest acknowledgeRequest = AcknowledgeRequest.newBuilder() .addAllAckIds(ackIds) .setSubscription(getSubscriptionName(context)) .build(); subscriber.acknowledgeCallable().call(acknowledgeRequest); } }