Example usage for com.amazonaws.services.sqs.model SendMessageBatchRequest SendMessageBatchRequest

List of usage examples for com.amazonaws.services.sqs.model SendMessageBatchRequest SendMessageBatchRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.sqs.model SendMessageBatchRequest SendMessageBatchRequest.

Prototype

public SendMessageBatchRequest(String queueUrl) 

Source Link

Document

Constructs a new SendMessageBatchRequest object.

Usage

From source file:com.dushyant.flume.sink.aws.sqs.BatchSQSMsgSender.java

License:Apache License

/**
 * The method creates batch of requests based on the configured "batchSize" to send to SQS.
 * <p>/*from  ww w . jav a  2s  .  c  o  m*/
 * When the combined payload size of the messages in the batch increases beyond the configured max allowed limit
 * then the method rolls the remaining messages into another batch.
 * <p>
 * For example, let's say that the <i>batchSize</i> is 10 and <i>maxMessageSize</i> is 256Kb and after creating a
 * batch with 7 messages the 256KB size limit is reached, in that case the method will split the remaining 3(i.e. 10
 * - 7) messages into its own batch request.
 * <p>
 * The returned list from this method will usually contain only 1 batch request (with all the messages part of that
 * batch request) when the total message size in the batch is within the allowed limit.
 *
 * @param channel Flume channel to take the messages from
 *
 * @return A list of {@link SendMessageBatchRequest} objects.
 *
 * @throws EventDeliveryException In case the message to be sent to SQS cannot be encoded in UTF-8 format
 */
protected List<SendMessageBatchRequest> createBatches(Channel channel) throws EventDeliveryException {
    List<SendMessageBatchRequest> batchRequests = new ArrayList<SendMessageBatchRequest>();
    // Create a batch request
    SendMessageBatchRequest batchRequest = new SendMessageBatchRequest(sqsUrl);
    Collection<SendMessageBatchRequestEntry> entries = new ArrayList<SendMessageBatchRequestEntry>();
    long numberOfBytesInBatch = 0;
    for (int i = 0; i < batchSize; ++i) {
        // Take event from the channel and add corresponding message to the batch
        Event event = channel.take();
        byte[] msgBytes = (event == null) ? null : event.getBody();
        if (msgBytes == null || msgBytes.length == 0) {
            // Channel returned null or empty event. Just break. Create batch with whatever events we have got so
            // far. This can happen when the channel is empty or is receiving events but with empty body (For
            // example, exec tail source may send empty events).
            break;
        }

        // Using message number as Id. This id is used for identifying the message entries within the batch.
        // It needs to be unique within a batch.
        String id = String.valueOf(i + 1);

        numberOfBytesInBatch += msgBytes.length;

        if (numberOfBytesInBatch > maxMessageSize) {
            // Max size per batch reached. Split into another batch.

            // Add entries collected so far into the current batch
            batchRequest.setEntries(entries);
            // Add the current batch into the list of batches to return
            batchRequests.add(batchRequest);

            // reset byte counter
            numberOfBytesInBatch = 0;

            // create new batch request
            batchRequest = new SendMessageBatchRequest(sqsUrl);
            // reset entries for the new batch
            entries = new ArrayList<SendMessageBatchRequestEntry>();
        }
        SendMessageBatchRequestEntry requestEntry = null;
        try {
            requestEntry = new SendMessageBatchRequestEntry(id, new String(msgBytes, "UTF-8").trim());
        } catch (UnsupportedEncodingException e) {
            throw new EventDeliveryException("Character set UTF-8 not supported.", e);
        }
        entries.add(requestEntry);
    }

    // The last batch request may not have been populated yet. Populate if there are any entries to be populated.
    if ((batchRequest.getEntries() == null || batchRequest.getEntries().size() == 0) && entries.size() > 0) {
        batchRequest.setEntries(entries);
        batchRequests.add(batchRequest);
    }
    return batchRequests;
}

From source file:com.netflix.conductor.contribs.queue.sqs.SQSObservableQueue.java

License:Apache License

void publishMessages(List<Message> messages) {
    logger.info("Sending {} messages", messages.size());
    SendMessageBatchRequest batch = new SendMessageBatchRequest(queueURL);
    messages.stream().forEach(msg -> {
        SendMessageBatchRequestEntry sendr = new SendMessageBatchRequestEntry(msg.getId(), msg.getPayload());
        batch.getEntries().add(sendr);//from  w ww.  j  a  v a 2  s  . c om
    });
    logger.info("sending {}", batch.getEntries().size());
    SendMessageBatchResult result = client.sendMessageBatch(batch);
    logger.info("send result {}", result.getFailed().toString());
}

From source file:shnakkydoodle.queueing.provider.aws.AwsProvider.java

License:Open Source License

/**
 * Enqueue a bunch of message//from   w ww .  j  a v  a 2s . c  om
 * 
 * @param queuename
 * @param message
 */
@Override
public void enqueue(String queueName, ArrayList<String> messages) throws Exception {
    AmazonSQS sqs = new AmazonSQSClient(credentials);
    sqs.setRegion(Region.EU_Ireland.toAWSRegion());
    sqs.createQueue(queueName);
    CreateQueueRequest createQueueRequest = new CreateQueueRequest(queueName);
    String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();

    SendMessageBatchRequest batchRequest = new SendMessageBatchRequest(myQueueUrl);
    batchRequest.setQueueUrl(myQueueUrl);

    List<SendMessageBatchRequestEntry> entries = new ArrayList<SendMessageBatchRequestEntry>();
    for (String message : messages) {
        entries.add(new SendMessageBatchRequestEntry(UUID.randomUUID().toString(), message));
    }
    batchRequest.setEntries(entries);
    sqs.sendMessageBatch(batchRequest);
}