com.limemojito.aws.sqs.SqsSender Maven / Gradle / Ivy
/*
* Copyright 2011-2024 Lime Mojito Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.limemojito.aws.sqs;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.*;
import java.util.*;
import java.util.function.Supplier;
import static java.util.Collections.emptyMap;
/**
* Sends Messages to SQS using standardized message attributes compatible with Spring Messaging and general
* MIME type conventions. Supports FIFO and batch sending.
*
* Always adds the following SQS attributes:
*
* - id - unique id for each message
* - timestamp - epoch milliseconds
* - contentType - mime type
* - Content-Type - Mime like attribute
* - Content-Length - Mime like attribute
*
*/
@RequiredArgsConstructor
@Slf4j
public class SqsSender {
/**
* This constant represents the header name for the deduplication ID of a message.
* The value of this header is expected to be a unique identifier for the message,
* used for identifying and avoiding processing duplicate messages.
*
* The constant is a string with the value "message-deduplication-id".
*/
public static final String ATTRIBUTE_MESSAGE_DEDUPLICATION_ID = "message-deduplication-id";
/**
* Represents the header for application generated id.
*
* The constant is a string with the value "id".
*/
public static final String ATTRIBUTE_ID = "id";
/**
* Represents the header for application generated timestamp in epoch milliseconds.
*
* The constant is a string with the value "timestamp".
*/
public static final String ATTRIBUTE_TIMESTAMP = "timestamp";
/**
* Represents the header for message body content length in a MIME like style.
*
* The constant is a string with the value "Content-Length".
*/
public static final String ATTRIBUTE_CONTENT_LENGTH = "Content-Length";
/**
* Represents the header for message body content type in a MIME like style.
*
* The constant is a string with the value "Content-Type".
*/
public static final String ATTRIBUTE_CONTENT_TYPE = "Content-Type";
/**
* Represents the header for message body content type in a MIME like style using a name compatible with Spring
* Messaging.
*
* The constant is a string with the value "contentType".
*/
public static final String ATTRIBUTE_SPRING_CONTENT_TYPE = "contentType";
/**
* The constant variable ATTRIBUTE_MESSAGE_GROUP_ID represents the name of the header that
* stores the unique identifier of a message group.
*
* The value of this variable is set to "message-group-id".
*/
public static final String ATTRIBUTE_MESSAGE_GROUP_ID = "message-group-id";
private static final String JSON_CONTENT = "application/json";
private final SqsClient sqs;
private final ObjectMapper objectMapper;
/**
* Sends a message to a specified queue URL.
*
* The message will be enriched with standard attributes and the body as json.
*
* @param queueUrl the URL of the queue to which the message is sent
* @param message the message to be sent
*/
public void send(String queueUrl, Object message) {
send(queueUrl, message, emptyMap());
}
/**
* Sends a message to the specified queue.
*
* The message will be enriched with standard attributes and the body as json.To send fifo information, you must include
* ATTRIBUTE_MESSAGE_DEDUPLICATION_ID and ATTRIBUTE_MESSAGE_GROUP_ID in the attributeValues.
*
* @param queueUrl the URL of the queue to send the message to
* @param message the message to send
* @param attributeValues additional attribute values for the message
* @see #ATTRIBUTE_MESSAGE_DEDUPLICATION_ID
* @see #ATTRIBUTE_MESSAGE_GROUP_ID
*/
public void send(String queueUrl, Object message, Map attributeValues) {
assertFifo(queueUrl, attributeValues);
SendMessageRequest.Builder r = SendMessageRequest.builder();
r.queueUrl(queueUrl);
final String body = toJson(message);
r.messageBody(body);
fifoOptions(r, queueUrl, attributeValues);
if (attributeValues != null && !attributeValues.isEmpty()) {
r.messageAttributes(sqsAttrFrom(
body.length(),
attributeValues));
}
sqs.sendMessage(r.build());
log.info("Sent message: {} {} to {}", message, attributeValues, queueUrl);
}
/**
* Sends a set of message to the specified queue using batch request.
*
* The message will be enriched with standard attributes and the body as json. This method does not support FIFO
* queues.
*
* @param queueUrl the URL of the queue to send the message to
* @param messages Map of message object to message attributes.
* @return Message batch sent.
*/
public SendMessageBatchResponse sendBatch(String queueUrl,
Collection