use of com.google.api.services.pubsub.model.PubsubMessage in project camel by apache.
the class GooglePubsubProducer method sendMessages.
private void sendMessages(List<Exchange> exchanges) throws Exception {
GooglePubsubEndpoint endpoint = (GooglePubsubEndpoint) getEndpoint();
String topicName = String.format("projects/%s/topics/%s", endpoint.getProjectId(), endpoint.getDestinationName());
List<PubsubMessage> messages = new ArrayList<>();
for (Exchange exchange : exchanges) {
PubsubMessage message = new PubsubMessage();
Object body = exchange.getIn().getBody();
if (body instanceof String) {
message.encodeData(((String) body).getBytes("UTF-8"));
} else if (body instanceof byte[]) {
message.encodeData((byte[]) body);
} else {
message.encodeData(serialize(body));
}
Object attributes = exchange.getIn().getHeader(GooglePubsubConstants.ATTRIBUTES);
if (attributes != null && attributes instanceof Map && ((Map) attributes).size() > 0) {
message.setAttributes((Map) attributes);
}
messages.add(message);
}
PublishRequest publishRequest = new PublishRequest().setMessages(messages);
PublishResponse response = endpoint.getPubsub().projects().topics().publish(topicName, publishRequest).execute();
List<String> sentMessageIds = response.getMessageIds();
int i = 0;
for (Exchange entry : exchanges) {
entry.getIn().setHeader(GooglePubsubConstants.MESSAGE_ID, sentMessageIds.get(i));
i++;
}
}
use of com.google.api.services.pubsub.model.PubsubMessage in project beam by apache.
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 publish.
@Override
public int publish(TopicPath topic, List<OutgoingMessage> outgoingMessages) throws IOException {
List<PubsubMessage> pubsubMessages = new ArrayList<>(outgoingMessages.size());
for (OutgoingMessage outgoingMessage : outgoingMessages) {
PubsubMessage pubsubMessage = new PubsubMessage().encodeData(outgoingMessage.elementBytes);
Map<String, String> attributes = outgoingMessage.attributes;
if ((timestampAttribute != null || idAttribute != null) && attributes == null) {
attributes = new TreeMap<>();
}
if (attributes != null) {
pubsubMessage.setAttributes(attributes);
}
if (timestampAttribute != null) {
attributes.put(timestampAttribute, String.valueOf(outgoingMessage.timestampMsSinceEpoch));
}
if (idAttribute != null && !Strings.isNullOrEmpty(outgoingMessage.recordId)) {
attributes.put(idAttribute, outgoingMessage.recordId);
}
pubsubMessages.add(pubsubMessage);
}
PublishRequest request = new PublishRequest().setMessages(pubsubMessages);
PublishResponse response = pubsub.projects().topics().publish(topic.getPath(), request).execute();
return response.getMessageIds().size();
}
use of com.google.api.services.pubsub.model.PubsubMessage in project beam by apache.
the class PubsubJsonClientTest method pullOneMessage.
@Test
public void pullOneMessage() throws IOException {
String expectedSubscription = SUBSCRIPTION.getPath();
PullRequest expectedRequest = new PullRequest().setReturnImmediately(true).setMaxMessages(10);
PubsubMessage expectedPubsubMessage = new PubsubMessage().setMessageId(MESSAGE_ID).encodeData(DATA.getBytes()).setPublishTime(String.valueOf(PUB_TIME)).setAttributes(ImmutableMap.of(TIMESTAMP_ATTRIBUTE, String.valueOf(MESSAGE_TIME), ID_ATTRIBUTE, RECORD_ID));
ReceivedMessage expectedReceivedMessage = new ReceivedMessage().setMessage(expectedPubsubMessage).setAckId(ACK_ID);
PullResponse expectedResponse = new PullResponse().setReceivedMessages(ImmutableList.of(expectedReceivedMessage));
Mockito.when((Object) (mockPubsub.projects().subscriptions().pull(expectedSubscription, expectedRequest).execute())).thenReturn(expectedResponse);
List<IncomingMessage> acutalMessages = client.pull(REQ_TIME, SUBSCRIPTION, 10, true);
assertEquals(1, acutalMessages.size());
IncomingMessage actualMessage = acutalMessages.get(0);
assertEquals(ACK_ID, actualMessage.ackId);
assertEquals(DATA, new String(actualMessage.elementBytes));
assertEquals(RECORD_ID, actualMessage.recordId);
assertEquals(REQ_TIME, actualMessage.requestTimeMsSinceEpoch);
assertEquals(MESSAGE_TIME, actualMessage.timestampMsSinceEpoch);
}
use of com.google.api.services.pubsub.model.PubsubMessage in project beam by apache.
the class PubsubJsonClientTest method publishOneMessage.
@Test
public void publishOneMessage() throws IOException {
String expectedTopic = TOPIC.getPath();
PubsubMessage expectedPubsubMessage = new PubsubMessage().encodeData(DATA.getBytes()).setAttributes(ImmutableMap.<String, String>builder().put(TIMESTAMP_ATTRIBUTE, String.valueOf(MESSAGE_TIME)).put(ID_ATTRIBUTE, RECORD_ID).put("k", "v").build());
PublishRequest expectedRequest = new PublishRequest().setMessages(ImmutableList.of(expectedPubsubMessage));
PublishResponse expectedResponse = new PublishResponse().setMessageIds(ImmutableList.of(MESSAGE_ID));
Mockito.when((Object) (mockPubsub.projects().topics().publish(expectedTopic, expectedRequest).execute())).thenReturn(expectedResponse);
Map<String, String> attrs = new HashMap<>();
attrs.put("k", "v");
OutgoingMessage actualMessage = new OutgoingMessage(DATA.getBytes(), attrs, MESSAGE_TIME, RECORD_ID);
int n = client.publish(TOPIC, ImmutableList.of(actualMessage));
assertEquals(1, n);
}
Aggregations