List of usage examples for com.amazonaws.services.sqs.model SendMessageBatchRequest setEntries
public void setEntries(java.util.Collection<SendMessageBatchRequestEntry> entries)
A list of SendMessageBatchRequestEntry items.
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 w ww . jav a2 s . co 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:org.apache.nifi.processors.aws.sqs.PutSQS.java
License:Apache License
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) { FlowFile flowFile = session.get();//from w ww . j a v a 2s .co m if (flowFile == null) { return; } final long startNanos = System.nanoTime(); final AmazonSQSClient client = getClient(); final SendMessageBatchRequest request = new SendMessageBatchRequest(); final String queueUrl = context.getProperty(QUEUE_URL).evaluateAttributeExpressions(flowFile).getValue(); request.setQueueUrl(queueUrl); final Set<SendMessageBatchRequestEntry> entries = new HashSet<>(); final SendMessageBatchRequestEntry entry = new SendMessageBatchRequestEntry(); entry.setId(flowFile.getAttribute("uuid")); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); session.exportTo(flowFile, baos); final String flowFileContent = baos.toString(); entry.setMessageBody(flowFileContent); final Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); for (final PropertyDescriptor descriptor : userDefinedProperties) { final MessageAttributeValue mav = new MessageAttributeValue(); mav.setDataType("String"); mav.setStringValue(context.getProperty(descriptor).evaluateAttributeExpressions(flowFile).getValue()); messageAttributes.put(descriptor.getName(), mav); } entry.setMessageAttributes(messageAttributes); entry.setDelaySeconds(context.getProperty(DELAY).asTimePeriod(TimeUnit.SECONDS).intValue()); entries.add(entry); request.setEntries(entries); try { client.sendMessageBatch(request); } catch (final Exception e) { getLogger().error("Failed to send messages to Amazon SQS due to {}; routing to failure", new Object[] { e }); flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_FAILURE); return; } getLogger().info("Successfully published message to Amazon SQS for {}", new Object[] { flowFile }); session.transfer(flowFile, REL_SUCCESS); final long transmissionMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos); session.getProvenanceReporter().send(flowFile, queueUrl, transmissionMillis); }
From source file:scheduler.SQSService.java
License:Apache License
public void batchSend(List<SendMessageBatchRequestEntry> entries) { try {/* ww w. j a va 2 s .co m*/ // Send batch messages //System.out.println("\nSending a message to jobQueue.\n"); SendMessageBatchRequest batchRequest = new SendMessageBatchRequest().withQueueUrl(queueUrl); batchRequest.setEntries(entries); SendMessageBatchResult batchResult = sqs.sendMessageBatch(batchRequest); // sendMessageBatch can return successfully, and yet individual batch // items fail. So, make sure to retry the failed ones. if (!batchResult.getFailed().isEmpty()) { //System.out.println("Retry sending failed messages..."); List<SendMessageBatchRequestEntry> failedEntries = new ArrayList<SendMessageBatchRequestEntry>(); Iterator<SendMessageBatchRequestEntry> iter = entries.iterator(); while (iter.hasNext()) { if (batchResult.getFailed().contains(iter.next())) { failedEntries.add((SendMessageBatchRequestEntry) iter.next()); } } batchRequest.setEntries(failedEntries); sqs.sendMessageBatch(batchRequest); } } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon SQS, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with SQS, such as not " + "being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:shnakkydoodle.queueing.provider.aws.AwsProvider.java
License:Open Source License
/** * Enqueue a bunch of message/* ww w .ja v a2 s. com*/ * * @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); }