use of com.google.api.services.pubsub.model.PublishResponse 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.PublishResponse 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.PublishResponse 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