use of com.google.api.services.pubsub.model.PubsubMessage in project DataflowJavaSDK-examples by GoogleCloudPlatform.
the class Injector method publishData.
/**
* Publish 'numMessages' arbitrary events from live users with the provided delay, to a
* PubSub topic.
*/
public static void publishData(int numMessages, int delayInMillis) throws IOException {
List<PubsubMessage> pubsubMessages = new ArrayList<>();
for (int i = 0; i < Math.max(1, numMessages); i++) {
Long currTime = System.currentTimeMillis();
String message = generateEvent(currTime, delayInMillis);
PubsubMessage pubsubMessage = new PubsubMessage().encodeData(message.getBytes("UTF-8"));
pubsubMessage.setAttributes(ImmutableMap.of(TIMESTAMP_ATTRIBUTE, Long.toString((currTime - delayInMillis) / 1000 * 1000)));
if (delayInMillis != 0) {
System.out.println(pubsubMessage.getAttributes());
System.out.println("late data for: " + message);
}
pubsubMessages.add(pubsubMessage);
}
PublishRequest publishRequest = new PublishRequest();
publishRequest.setMessages(pubsubMessages);
pubsub.projects().topics().publish(topic, publishRequest).execute();
}
use of com.google.api.services.pubsub.model.PubsubMessage in project beam by apache.
the class PubsubJsonClient method pull.
@Override
public List<IncomingMessage> pull(long requestTimeMsSinceEpoch, SubscriptionPath subscription, int batchSize, boolean returnImmediately) throws IOException {
PullRequest request = new PullRequest().setReturnImmediately(returnImmediately).setMaxMessages(batchSize);
PullResponse response = pubsub.projects().subscriptions().pull(subscription.getPath(), request).execute();
if (response.getReceivedMessages() == null || response.getReceivedMessages().size() == 0) {
return ImmutableList.of();
}
List<IncomingMessage> incomingMessages = new ArrayList<>(response.getReceivedMessages().size());
for (ReceivedMessage message : response.getReceivedMessages()) {
PubsubMessage pubsubMessage = message.getMessage();
@Nullable Map<String, String> attributes = pubsubMessage.getAttributes();
// Payload.
byte[] elementBytes = pubsubMessage.decodeData();
// Timestamp.
long timestampMsSinceEpoch = extractTimestamp(timestampAttribute, message.getMessage().getPublishTime(), 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(new IncomingMessage(elementBytes, attributes, timestampMsSinceEpoch, requestTimeMsSinceEpoch, ackId, recordId));
}
return incomingMessages;
}
Aggregations