Search in sources :

Example 1 with PublishResponse

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++;
    }
}
Also used : PublishResponse(com.google.api.services.pubsub.model.PublishResponse) ArrayList(java.util.ArrayList) PublishRequest(com.google.api.services.pubsub.model.PublishRequest) PubsubMessage(com.google.api.services.pubsub.model.PubsubMessage) Exchange(org.apache.camel.Exchange) Map(java.util.Map)

Example 2 with PublishResponse

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();
}
Also used : PublishResponse(com.google.api.services.pubsub.model.PublishResponse) ArrayList(java.util.ArrayList) PublishRequest(com.google.api.services.pubsub.model.PublishRequest) PubsubMessage(com.google.api.services.pubsub.model.PubsubMessage)

Example 3 with PublishResponse

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);
}
Also used : PublishResponse(com.google.api.services.pubsub.model.PublishResponse) OutgoingMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage) HashMap(java.util.HashMap) PublishRequest(com.google.api.services.pubsub.model.PublishRequest) PubsubMessage(com.google.api.services.pubsub.model.PubsubMessage) Test(org.junit.Test)

Aggregations

PublishRequest (com.google.api.services.pubsub.model.PublishRequest)3 PublishResponse (com.google.api.services.pubsub.model.PublishResponse)3 PubsubMessage (com.google.api.services.pubsub.model.PubsubMessage)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 OutgoingMessage (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage)1 Exchange (org.apache.camel.Exchange)1 Test (org.junit.Test)1