public SendMessageBatchRequestEntry unmarshall(StaxUnmarshallerContext context) throws Exception { SendMessageBatchRequestEntry sendMessageBatchRequestEntry = new SendMessageBatchRequestEntry(); int originalDepth = context.getCurrentDepth(); int targetDepth = originalDepth + 1; sendMessageBatchRequestEntry.setId(StringStaxUnmarshaller.getInstance().unmarshall(context)); continue; sendMessageBatchRequestEntry.setMessageBody(StringStaxUnmarshaller.getInstance().unmarshall(context)); continue; sendMessageBatchRequestEntry.setDelaySeconds(IntegerStaxUnmarshaller.getInstance().unmarshall(context)); continue; sendMessageBatchRequestEntry.addMessageAttributesEntry(entry.getKey(), entry.getValue()); continue; sendMessageBatchRequestEntry.setMessageDeduplicationId(StringStaxUnmarshaller.getInstance().unmarshall(context)); continue; sendMessageBatchRequestEntry.setMessageGroupId(StringStaxUnmarshaller.getInstance().unmarshall(context)); continue;
/** * Creates a #SendMessageBatchRequestEntry object from a id and #SendMessageRequest object. * The common members of {@link SendMessageRequest} object are copied to the * returned {@link SendMessageBatchRequestEntry} object. */ public static SendMessageBatchRequestEntry createSendMessageBatchRequestEntryFrom(String id, SendMessageRequest sendMessageRequest) { return new SendMessageBatchRequestEntry() .withId(id) .withMessageBody(sendMessageRequest.getMessageBody()) .withDelaySeconds(sendMessageRequest.getDelaySeconds()) .withMessageAttributes(sendMessageRequest.getMessageAttributes()) .withMessageDeduplicationId(sendMessageRequest.getMessageDeduplicationId()) .withMessageGroupId(sendMessageRequest.getMessageGroupId()); } }
@Override public int hashCode() { final int prime = 31; int hashCode = 1; hashCode = prime * hashCode + ((getId() == null) ? 0 : getId().hashCode()); hashCode = prime * hashCode + ((getMessageBody() == null) ? 0 : getMessageBody().hashCode()); hashCode = prime * hashCode + ((getDelaySeconds() == null) ? 0 : getDelaySeconds().hashCode()); hashCode = prime * hashCode + ((getMessageAttributes() == null) ? 0 : getMessageAttributes().hashCode()); hashCode = prime * hashCode + ((getMessageDeduplicationId() == null) ? 0 : getMessageDeduplicationId().hashCode()); hashCode = prime * hashCode + ((getMessageGroupId() == null) ? 0 : getMessageGroupId().hashCode()); return hashCode; }
/** * Constructs a new SendMessageBatchRequestEntry object. Callers should use the setter or fluent setter (with...) * methods to initialize any additional object members. * * @param id * An identifier for a message in this batch used to communicate the result.</p> <note> * <p> * The <code>Id</code>s of a batch request need to be unique within a request * </p> * <p> * This identifier can have up to 80 characters. The following characters are accepted: alphanumeric * characters, hyphens(-), and underscores (_). * </p> * @param messageBody * The body of the message. */ public SendMessageBatchRequestEntry(String id, String messageBody) { setId(id); setMessageBody(messageBody); }
/** * Puts multiple tasks on the queue using batch puts. The tasks argument * can contain more than 10 Tasks, in that case there will be multiple SQS * batch send requests made each containing up to 10 messages. * * @param tasks */ @Override public void put(Set<Task> tasks) { String msgBody = null; SendMessageBatchRequestEntry msgEntry = null; Set<SendMessageBatchRequestEntry> msgEntries = new HashSet<>(); for (Task task : tasks) { msgBody = unmarshallTask(task); msgEntry = new SendMessageBatchRequestEntry() .withMessageBody(msgBody) .withId(msgEntries.size() + ""); // must set unique ID for each msg in the batch request msgEntries.add(msgEntry); // Can only send batch of max 10 messages in a SQS queue request if (msgEntries.size() == 10) { this.sendBatchMessages(msgEntries); msgEntries.clear(); // clear the already sent messages } } // After for loop check to see if there are msgs in msgEntries that // haven't been sent yet because the size never reached 10. if (!msgEntries.isEmpty()) { this.sendBatchMessages(msgEntries); } }
requests.size()); for (int i = 0, n = requests.size(); i < n; i++) entries.add(new SendMessageBatchRequestEntry() .withId(Integer.toString(i)) .withMessageBody(requests.get(i).getMessageBody()) .withDelaySeconds(requests.get(i).getDelaySeconds()) .withMessageAttributes(requests.get(i).getMessageAttributes())); batchRequest.setEntries(entries);
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); entry.setMessageAttributes(messageAttributes); entry.setDelaySeconds(context.getProperty(DELAY).asTimePeriod(TimeUnit.SECONDS).intValue()); entries.add(entry);
if (sendMessageBatchRequest.getEntries() != null) { for (SendMessageBatchRequestEntry entry : sendMessageBatchRequest.getEntries()) { idToRequestEntryMap.put(entry.getId(), entry); String messageBody = idToRequestEntryMap.get(entry.getId()).getMessageBody(); String bodyMd5Returned = entry.getMD5OfMessageBody(); String clientSideBodyMd5 = calculateMessageBodyMd5(messageBody); .getMessageAttributes(); if (messageAttr != null && !messageAttr.isEmpty()) { String attrMd5Returned = entry.getMD5OfMessageAttributes();
.withQueueUrl(queueUrl) .withEntries( new SendMessageBatchRequestEntry( "msg_1", "Hello from message 1"), new SendMessageBatchRequestEntry( "msg_2", "Hello from message 2") .withDelaySeconds(10)); sqs.sendMessageBatch(send_batch_request);
private void publishMessages(List<Message> messages) { logger.info("Sending {} messages to the SQS queue: {}", messages.size(), queueName); SendMessageBatchRequest batch = new SendMessageBatchRequest(queueURL); messages.forEach(msg -> { SendMessageBatchRequestEntry sendr = new SendMessageBatchRequestEntry(msg.getId(), msg.getPayload()); batch.getEntries().add(sendr); }); logger.info("sending {} messages in batch", batch.getEntries().size()); SendMessageBatchResult result = client.sendMessageBatch(batch); logger.info("send result: {} for SQS queue: {}", result.getFailed().toString(), queueName); }
private SendMessageBatchRequestEntry storeMessageInS3(SendMessageBatchRequestEntry batchEntry) { checkMessageAttributes(batchEntry.getMessageAttributes()); String s3Key = UUID.randomUUID().toString(); // Read the content of the message from message body String messageContentStr = batchEntry.getMessageBody(); Long messageContentSize = getStringSizeInBytes(messageContentStr); // Add a new message attribute as a flag MessageAttributeValue messageAttributeValue = new MessageAttributeValue(); messageAttributeValue.setDataType("Number"); messageAttributeValue.setStringValue(messageContentSize.toString()); batchEntry.addMessageAttributesEntry(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME, messageAttributeValue); // Store the message content in S3. storeTextInS3(s3Key, messageContentStr, messageContentSize); LOG.info("S3 object created, Bucket name: " + clientConfiguration.getS3BucketName() + ", Object key: " + s3Key + "."); // Convert S3 pointer (bucket name, key, etc) to JSON string MessageS3Pointer s3Pointer = new MessageS3Pointer(clientConfiguration.getS3BucketName(), s3Key); String s3PointerStr = getJSONFromS3Pointer(s3Pointer); // Storing S3 pointer in the message body. batchEntry.setMessageBody(s3PointerStr); return batchEntry; }
private boolean isLarge(SendMessageBatchRequestEntry batchEntry) { int msgAttributesSize = getMsgAttributesSize(batchEntry.getMessageAttributes()); long msgBodySize = getStringSizeInBytes(batchEntry.getMessageBody()); long totalMsgSize = msgAttributesSize + msgBodySize; return (totalMsgSize > clientConfiguration.getMessageSizeThreshold()); }
/** * <p> * The body of the message. * </p> * * @param messageBody * The body of the message. * @return Returns a reference to this object so that method calls can be chained together. */ public SendMessageBatchRequestEntry withMessageBody(String messageBody) { setMessageBody(messageBody); return this; }
/** * <p> * The length of time, in seconds, for which a specific message is delayed. Valid values: 0 to 900. Maximum: 15 * minutes. Messages with a positive <code>DelaySeconds</code> value become available for processing after the delay * period is finished. If you don't specify a value, the default value for the queue is applied. * </p> * <note> * <p> * When you set <code>FifoQueue</code>, you can't set <code>DelaySeconds</code> per message. You can set this * parameter only on a queue level. * </p> * </note> * * @param delaySeconds * The length of time, in seconds, for which a specific message is delayed. Valid values: 0 to 900. Maximum: * 15 minutes. Messages with a positive <code>DelaySeconds</code> value become available for processing after * the delay period is finished. If you don't specify a value, the default value for the queue is applied. * </p> <note> * <p> * When you set <code>FifoQueue</code>, you can't set <code>DelaySeconds</code> per message. You can set this * parameter only on a queue level. * </p> * @return Returns a reference to this object so that method calls can be chained together. */ public SendMessageBatchRequestEntry withDelaySeconds(Integer delaySeconds) { setDelaySeconds(delaySeconds); return this; }
/** * <p> * An identifier for a message in this batch used to communicate the result. * </p> * <note> * <p> * The <code>Id</code>s of a batch request need to be unique within a request * </p> * <p> * This identifier can have up to 80 characters. The following characters are accepted: alphanumeric characters, * hyphens(-), and underscores (_). * </p> * </note> * * @param id * An identifier for a message in this batch used to communicate the result.</p> <note> * <p> * The <code>Id</code>s of a batch request need to be unique within a request * </p> * <p> * This identifier can have up to 80 characters. The following characters are accepted: alphanumeric * characters, hyphens(-), and underscores (_). * </p> * @return Returns a reference to this object so that method calls can be chained together. */ public SendMessageBatchRequestEntry withId(String id) { setId(id); return this; }
String message = messages.get(i); if (!StringUtils.isBlank(message)) { msgs.add(new SendMessageBatchRequestEntry(). withMessageBody(message). withId(Integer.toString(i)));
requests.size()); for (int i = 0, n = requests.size(); i < n; i++) entries.add(new SendMessageBatchRequestEntry() .withId(Integer.toString(i)) .withMessageBody(requests.get(i).getMessageBody()) .withDelaySeconds(requests.get(i).getDelaySeconds()) .withMessageAttributes(requests.get(i).getMessageAttributes())); batchRequest.setEntries(entries);
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); entry.setMessageAttributes(messageAttributes); entry.setDelaySeconds(context.getProperty(DELAY).asTimePeriod(TimeUnit.SECONDS).intValue()); entries.add(entry);
if (sendMessageBatchRequest.getEntries() != null) { for (SendMessageBatchRequestEntry entry : sendMessageBatchRequest.getEntries()) { idToRequestEntryMap.put(entry.getId(), entry); String messageBody = idToRequestEntryMap.get(entry.getId()).getMessageBody(); String bodyMd5Returned = entry.getMD5OfMessageBody(); String clientSideBodyMd5 = calculateMessageBodyMd5(messageBody); entry.getId()).getMessageAttributes(); if (messageAttr != null && !messageAttr.isEmpty()) { String attrMd5Returned = entry.getMD5OfMessageAttributes();
@Override public List<LegacyQueueMessage> sendQueueMessages(List<LegacyQueueMessage> queueMessages) throws IOException { List<LegacyQueueMessage> successMessages = new ArrayList<>(); if ( sqs == null ) { logger.error( "SQS client is null, perhaps it failed to initialize successfully" ); return successMessages; } String url = getReadQueue().getUrl(); List<SendMessageBatchRequestEntry> entries = new ArrayList<>(); for (LegacyQueueMessage queueMessage : queueMessages) { entries.add(new SendMessageBatchRequestEntry(queueMessage.getMessageId(), queueMessage.getStringBody())); } SendMessageBatchResult result = sqs.sendMessageBatch(url, entries); Set<String> successIDs = new HashSet<>(); logger.debug("sendQueueMessages: successful: {}, failed: {}", result.getSuccessful().size(), result.getFailed().size()); for (SendMessageBatchResultEntry batchResultEntry : result.getSuccessful()) { successIDs.add(batchResultEntry.getId()); } for (LegacyQueueMessage queueMessage : queueMessages) { if (successIDs.contains(queueMessage.getMessageId())) { successMessages.add(queueMessage); } } return successMessages; }