aws-sdk-java-v2-messaging
giuseppe-trisciuoglio/developer-kit
提供基于 AWS SDK for Java 2.x 的 AWS 消息传递模式,适用于 SQS 队列和 SNS 主题。支持消息的发送/接收、FIFO 队列、DLQ、订阅以及发布/订阅模式。在实现基于 SQS 或 SNS 的消息传递时可使用此功能。
...展开全部关于aws-sdk-java-v2-messaging
aws-sdk-java-v2-messaging 是一份关于使用 AWS SDK for Java 2.x 实现 AWS 消息传递的开发者参考指南,涵盖了 Amazon SQS 队列和 Amazon SNS 主题。 它通过将符合惯例的客户端配置和消息操作模式与可运行的代码整合在一起,解决了在 Java 服务中正确连接生产者、消费者以及发布/订阅流的问题,因此构建事件驱动系统的工程师无需从零散的文档中重新整理这些内容。
SKILL.md 及其参考文件演示了客户端配置(使用 DefaultCredentialsProvider 和区域的 SqsClient/SnsClient 构建器)、SQS 操作(创建队列、发送、使用长轮询接收、通过收据句柄删除)、 支持基于内容的去重和消息组 ID 的 FIFO 队列、SNS 操作(创建主题、带主题和消息属性的发布、FIFO 发布),以及 SNS 到 SQS/电子邮件/Lambda 的订阅。 此外,还展示了 Spring Boot 与注入式客户端及配置驱动的主题 ARN 的集成,并介绍了死信队列、批处理操作、可见性超时以及使用 S3 存储大于 256KB 的消息等高级功能。 随附的参考资料涵盖了详细的 SQS 和 SNS 操作、Spring Boot 集成,以及指向 AWS 官方文档和示例代码库的链接。
本指南面向在 AWS 上构建消息缓冲、发布/订阅(pub/sub)及事件驱动架构的 Java 后端开发人员,特别是使用 Spring Boot 的开发者。凭据通过 SDK 的标准 DefaultCredentialsProvider 进行管理,而非硬编码的密钥,且所有展示的操作均为常规的应用程序级消息调用。
常见问题
本指南涵盖哪些 AWS 服务?
使用 AWS SDK for Java 2.x 的 Amazon SQS(标准队列和 FIFO 队列、DLQ、长轮询)以及 Amazon SNS(主题、发布、SQS 订阅、电子邮件和 Lambda 订阅)。
凭证如何处理?
客户端基于 DefaultCredentialsProvider 并指定明确的区域构建,依赖标准的 AWS 凭证链,而非在技能中硬编码密钥。
是否支持 Spring Boot?
支持——其中包含 Spring Boot 集成示例,其中通过 @Value 配置注入 SnsClient/ObjectMapper 并提供主题 ARN,还提供了一个专用的 spring-boot-integration 参考文件。
它能否处理 FIFO 排序和去重?
支持。它展示了具有基于内容的去重功能的 FIFO 队列和主题的创建,此外还提供了消息组 ID 和去重 ID,以实现有序的“精确一次”风格的投递。
对于大消息或失败的消息呢?
参考文档中说明了对于大于 256KB 的消息应使用 S3,并配置死信队列、可见性超时和批处理操作以实现稳健处理。
所有文件
5个文件 references/detailed-sns-operations.md 5.1 KB查看 references/aws-official-documentation.md5.2KB 查看 references/detailed-sqs-operations.md 6.0KB 查看 references/spring-boot-integration.md 7.7KB 查看 SKILL.md 7.2 KB 查看Overview
Provides patterns for SQS queues and SNS topics with AWS SDK for Java 2.x: client setup, queue management, message operations, subscriptions, and Spring Boot integration.
When to Use
- Setting up SQS queues (standard or FIFO) for message buffering
- Implementing pub/sub with SNS topics and subscriptions
- Processing messages from SQS queues with long polling
- Configuring dead letter queues (DLQ) for error handling
- Integrating AWS messaging with Spring Boot applications
- Building event-driven architectures with SQS/SNS
Examples
Quick Setup
Dependencies:
<dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>sqs</artifactId></dependency><dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>sns</artifactId></dependency>
Client Configuration:
SqsClient sqsClient = SqsClient.builder() .region(Region.US_EAST_1) .credentialsProvider(DefaultCredentialsProvider.create()) .build();SnsClient snsClient = SnsClient.builder() .region(Region.US_EAST_1) .build();
SQS Operations
Create and Send Message:
String queueUrl = sqsClient.createQueue(CreateQueueRequest.builder() .queueName("my-queue") .build()).queueUrl();String messageId = sqsClient.sendMessage(SendMessageRequest.builder() .queueUrl(queueUrl) .messageBody("Hello, SQS!") .build()).messageId();
Receive and Delete Message:
ReceiveMessageResponse response = sqsClient.receiveMessage(ReceiveMessageRequest.builder() .queueUrl(queueUrl) .maxNumberOfMessages(10) .waitTimeSeconds(20) .build());response.messages().forEach(message -> { processMessage(message.body()); sqsClient.deleteMessage(DeleteMessageRequest.builder() .queueUrl(queueUrl) .receiptHandle(message.receiptHandle()) .build());});
FIFO Queue:
Map<QueueAttributeName, String> attributes = Map.of( QueueAttributeName.FIFO_QUEUE, "true", QueueAttributeName.CONTENT_BASED_DEDUPLICATION, "true");String fifoQueueUrl = sqsClient.createQueue(CreateQueueRequest.builder() .queueName("my-queue.fifo") .attributes(attributes) .build()).queueUrl();sqsClient.sendMessage(SendMessageRequest.builder() .queueUrl(fifoQueueUrl) .messageBody("Order #12345") .messageGroupId("orders") .messageDeduplicationId(UUID.randomUUID().toString()) .build());
SNS Operations
Create Topic and Publish:
String topicArn = snsClient.createTopic(CreateTopicRequest.builder() .name("my-topic") .build()).topicArn();snsClient.publish(PublishRequest.builder() .topicArn(topicArn) .subject("Test Notification") .message("Hello, SNS!") .build());
SNS to SQS Subscription:
String queueArn = sqsClient.getQueueAttributes(GetQueueAttributesRequest.builder() .queueUrl(queueUrl) .attributeNames(QueueAttributeName.QUEUE_ARN) .build()).attributes().get(QueueAttributeName.QUEUE_ARN);snsClient.subscribe(SubscribeRequest.builder() .protocol("sqs") .endpoint(queueArn) .topicArn(topicArn) .build());
Spring Boot Integration
@Service@RequiredArgsConstructorpublic class OrderNotificationService { private final SnsClient snsClient; private final ObjectMapper objectMapper; @Value("${aws.sns.order-topic-arn}") private String orderTopicArn; public void sendOrderNotification(Order order) throws JsonProcessingException { snsClient.publish(PublishRequest.builder() .topicArn(orderTopicArn) .subject("New Order Received") .message(objectMapper.writeValueAsString(order)) .messageAttributes(Map.of( "orderType", MessageAttributeValue.builder() .dataType("String") .stringValue(order.getType()) .build())) .build()); }}
Instructions
Implement Message Processing (with Validation)
- Create queues/topics with appropriate configuration
- Send messages and validate
messageIdis returned - Receive messages with long polling (
waitTimeSeconds: 20) - Process messages - validate payload before processing
- Delete messages only after successful processing - verify deletion response
- Check DLQ periodically for failed messages using
redrivePolicy - Verify delivery - monitor CloudWatch
NumberOfMessagesSentmetric
Validation Checklist:
// After sendif (messageId == null || messageId.isEmpty()) { throw new MessagingException("Message send failed - no messageId returned");}// After receiveif (response.messages().isEmpty()) { log.debug("No messages available - normal with long polling");}// After deleteif (!deleteResponse.sdkHttpResponse().isSuccessful()) { throw new MessagingException("Message deletion failed");}
Setup Credentials
export AWS_ACCESS_KEY_ID=your-access-keyexport AWS_SECRET_ACCESS_KEY=your-secret-keyexport AWS_REGION=us-east-1
Monitor and Debug
- CloudWatch metrics:
ApproximateNumberOfMessages,NumberOfMessagesSent,NumberOfMessagesReceived - Enable SDK logging:
software.amazon.awssdkat DEBUG level - Use X-Ray for distributed tracing
Best Practices
SQS:
- Use long polling (20-40s) to reduce empty responses and costs
- Always delete messages after successful processing
- Implement idempotent processing for duplicate handling
- Configure DLQ (
redrivePolicy) for failed messages - Use FIFO queues when order matters (300 msg/sec limit)
SNS:
- Use filter policies to reduce unnecessary deliveries
- Keep messages under 256KB
- Implement retry with exponential backoff
- Monitor
NumberOfNotificationFailedmetric
General:
- Use IAM roles over static credentials
- Reuse clients (they are thread-safe)
- Test with LocalStack or Testcontainers
Detailed References
- references/detailed-sqs-operations.md
- references/detailed-sns-operations.md
- references/spring-boot-integration.md
- references/aws-official-documentation.md
Constraints and Warnings
- Message Size: Maximum 256KB for SQS and SNS
- Visibility Timeout: Undeleted messages reappear after timeout - always delete after processing
- Input Validation: Sanitize message body before processing - messages may contain untrusted payloads
- FIFO Naming: Must end with
.fifosuffix - FIFO Throughput: 300 msg/sec per queue (use partitioning for higher throughput)
- Message Retention: SQS retains messages max 14 days
- DLQ Required: Configure dead letter queue to prevent message loss
- Region-Specific: SQS queues are region-specific; cross-region requires SNS
所有文件
0 个文件安装 aws-sdk-java-v2-messaging
下载技能文件并将其解压到 .claude/skills/ 目录中。
下载ZIP克隆仓库并复制技能文件到您的项目中。
git clone https://github.com/giuseppe-trisciuoglio/developer-kit/blob/main/plugins/developer-kit-java/skills/aws-sdk-java-v2-messaging/SKILL.md # Copy SKILL.md to your .claude/skills/ directory
复制





首页
