home / skills / giuseppe-trisciuoglio / developer-kit / aws-sdk-java-v2-messaging

This skill helps you implement AWS messaging with SQS and SNS using Java SDK 2.x, covering queues, topics, DLQ, and Spring Boot patterns.

npx playbooks add skill giuseppe-trisciuoglio/developer-kit --skill aws-sdk-java-v2-messaging

Review the files below or copy the command above to add this skill to your agents.

Files (5)
SKILL.md
11.0 KB
---
name: aws-sdk-java-v2-messaging
description: Provides AWS messaging patterns using AWS SDK for Java 2.x for SQS queues and SNS topics. Handles sending/receiving messages, FIFO queues, DLQ, subscriptions, and pub/sub patterns. Use when implementing messaging with SQS or SNS.
category: aws
tags: [aws, sqs, sns, java, sdk, messaging, pub-sub, queues, events]
version: 1.1.0
allowed-tools: Read, Write, Edit, Bash, Glob, Grep
---

# AWS SDK for Java 2.x - Messaging (SQS & SNS)

## Overview

Provide comprehensive AWS messaging patterns using AWS SDK for Java 2.x for both SQS and SNS services. Include client setup, queue management, message operations, subscription management, and Spring Boot integration patterns.

## When to Use

Use this skill when working with:
- Amazon SQS queues for message queuing
- SNS topics for event publishing and notification
- FIFO queues and standard queues
- Dead Letter Queues (DLQ) for message handling
- SNS subscriptions with email, SMS, SQS, Lambda endpoints
- Pub/sub messaging patterns and event-driven architectures
- Spring Boot integration with AWS messaging services
- Testing strategies using LocalStack or Testcontainers

## Quick Start

### Dependencies

```xml
<!-- SQS -->
<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>sqs</artifactId>
</dependency>

<!-- SNS -->
<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>sns</artifactId>
</dependency>
```

### Basic Client Setup

```java
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sns.SnsClient;

SqsClient sqsClient = SqsClient.builder()
    .region(Region.US_EAST_1)
    .build();

SnsClient snsClient = SnsClient.builder()
    .region(Region.US_EAST_1)
    .build();
```

## Examples

### Basic SQS Operations

#### Create and Send Message
```java
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.*;

// Setup SQS client
SqsClient sqsClient = SqsClient.builder()
    .region(Region.US_EAST_1)
    .build();

// Create queue
String queueUrl = sqsClient.createQueue(CreateQueueRequest.builder()
    .queueName("my-queue")
    .build()).queueUrl();

// Send message
String messageId = sqsClient.sendMessage(SendMessageRequest.builder()
    .queueUrl(queueUrl)
    .messageBody("Hello, SQS!")
    .build()).messageId();
```

#### Receive and Delete Message
```java
// Receive messages with long polling
ReceiveMessageResponse response = sqsClient.receiveMessage(ReceiveMessageRequest.builder()
    .queueUrl(queueUrl)
    .maxNumberOfMessages(10)
    .waitTimeSeconds(20)
    .build());

// Process and delete messages
response.messages().forEach(message -> {
    System.out.println("Received: " + message.body());
    sqsClient.deleteMessage(DeleteMessageRequest.builder()
        .queueUrl(queueUrl)
        .receiptHandle(message.receiptHandle())
        .build());
});
```

### Basic SNS Operations

#### Create Topic and Publish
```java
import software.amazon.awssdk.services.sns.SnsClient;
import software.amazon.awssdk.services.sns.model.*;

// Setup SNS client
SnsClient snsClient = SnsClient.builder()
    .region(Region.US_EAST_1)
    .build();

// Create topic
String topicArn = snsClient.createTopic(CreateTopicRequest.builder()
    .name("my-topic")
    .build()).topicArn();

// Publish message
String messageId = snsClient.publish(PublishRequest.builder()
    .topicArn(topicArn)
    .subject("Test Notification")
    .message("Hello, SNS!")
    .build()).messageId();
```

### Advanced Examples

#### FIFO Queue Pattern
```java
// Create 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();

// Send FIFO message with group ID
String fifoMessageId = sqsClient.sendMessage(SendMessageRequest.builder()
    .queueUrl(fifoQueueUrl)
    .messageBody("Order #12345")
    .messageGroupId("orders")
    .messageDeduplicationId(UUID.randomUUID().toString())
    .build()).messageId();
```

#### SNS to SQS Subscription
```java
// Create SQS queue for subscription
String subscriptionQueueUrl = sqsClient.createQueue(CreateQueueRequest.builder()
    .queueName("notification-subscriber")
    .build()).queueUrl();

// Get queue ARN
String queueArn = sqsClient.getQueueAttributes(GetQueueAttributesRequest.builder()
    .queueUrl(subscriptionQueueUrl)
    .attributeNames(QueueAttributeName.QUEUE_ARN)
    .build()).attributes().get(QueueAttributeName.QUEUE_ARN);

// Subscribe SQS to SNS
String subscriptionArn = snsClient.subscribe(SubscribeRequest.builder()
    .protocol("sqs")
    .endpoint(queueArn)
    .topicArn(topicArn)
    .build()).subscriptionArn();
```

### Spring Boot Integration Example

```java
@Service
@RequiredArgsConstructor
public class OrderNotificationService {

    private final SnsClient snsClient;
    private final ObjectMapper objectMapper;

    @Value("${aws.sns.order-topic-arn}")
    private String orderTopicArn;

    public void sendOrderNotification(Order order) {
        try {
            String jsonMessage = objectMapper.writeValueAsString(order);

            snsClient.publish(PublishRequest.builder()
                .topicArn(orderTopicArn)
                .subject("New Order Received")
                .message(jsonMessage)
                .messageAttributes(Map.of(
                    "orderType", MessageAttributeValue.builder()
                        .dataType("String")
                        .stringValue(order.getType())
                        .build()))
                .build());

        } catch (Exception e) {
            throw new RuntimeException("Failed to send order notification", e);
        }
    }
}
```

## Best Practices

### SQS Best Practices
- **Use long polling**: Set `waitTimeSeconds` (20-40 seconds) to reduce empty responses
- **Batch operations**: Use `sendMessageBatch` for multiple messages to reduce API calls
- **Visibility timeout**: Set appropriately based on message processing time (default 30 seconds)
- **Delete messages**: Always delete messages after successful processing
- **Handle duplicates**: Implement idempotent processing for retries
- **Implement DLQ**: Route failed messages to dead letter queues for analysis
- **Monitor queue depth**: Use CloudWatch alarms for high queue backlog
- **Use FIFO queues**: When message order and deduplication are critical

### SNS Best Practices
- **Use filter policies**: Reduce noise by filtering messages at the source
- **Message attributes**: Add metadata for subscription routing decisions
- **Retry logic**: Handle transient failures with exponential backoff
- **Monitor failed deliveries**: Set up CloudWatch alarms for failed notifications
- **Security**: Use IAM policies for access control and data encryption
- **FIFO topics**: Use when order and deduplication are critical
- **Avoid large payloads**: Keep messages under 256KB for optimal performance

### General Guidelines
- **Region consistency**: Use the same region for all AWS resources
- **Resource naming**: Use consistent naming conventions for queues and topics
- **Error handling**: Implement proper exception handling and logging
- **Testing**: Use LocalStack for local development and testing
- **Documentation**: Document subscription endpoints and message formats

## Instructions

### Setup AWS Credentials
Configure AWS credentials using environment variables, AWS CLI, or IAM roles:
```bash
export AWS_ACCESS_KEY_ID=your-access-key
export AWS_SECRET_ACCESS_KEY=your-secret-key
export AWS_REGION=us-east-1
```

### Configure Clients
```java
// Basic client configuration
SqsClient sqsClient = SqsClient.builder()
    .region(Region.US_EAST_1)
    .build();

// Advanced client with custom configuration
SnsClient snsClient = SnsClient.builder()
    .region(Region.US_EAST_1)
    .credentialsProvider(DefaultCredentialsProvider.create())
    .httpClient(UrlConnectionHttpClient.create())
    .build();
```

### Implement Message Processing
1. **Connect** to SQS/SNS using the AWS SDK clients
2. **Create** queues and topics as needed
3. **Send/receive** messages with appropriate timeout settings
4. **Process** messages in batches for efficiency
5. **Delete** messages after successful processing
6. **Handle** failures with proper error handling and retries

### Integrate with Spring Boot
1. **Configure** beans for `SqsClient` and `SnsClient` in `@Configuration` classes
2. **Use** `@Value` to inject queue URLs and topic ARNs from properties
3. **Create** service classes with business logic for messaging operations
4. **Implement** error handling with `@Retryable` or custom retry logic
5. **Test** integration using Testcontainers or LocalStack

### Monitor and Debug
- Use AWS CloudWatch for monitoring queue depth and message metrics
- Enable AWS SDK logging for debugging client operations
- Implement proper logging for message processing activities
- Use AWS X-Ray for distributed tracing in production environments

## Troubleshooting

### Common Issues
- **Queue does not exist**: Verify queue URL and permissions
- **Message not received**: Check visibility timeout and consumer logic
- **Permission denied**: Verify IAM policies and credentials
- **Connection timeout**: Check network connectivity and region configuration
- **Rate limiting**: Implement retry logic with exponential backoff

### Performance Optimization
- Use long polling to reduce empty responses
- Batch message operations to minimize API calls
- Adjust visibility timeout based on processing time
- Implement connection pooling and reuse clients
- Use appropriate message sizes to avoid fragmentation

## Detailed References

For comprehensive API documentation and advanced patterns, see:

- [references/detailed-sqs-operations.md](references/detailed-sqs-operations.md) - Complete SQS operations reference
- [references/detailed-sns-operations.md](references/detailed-sns-operations.md) - Complete SNS operations reference
- [references/spring-boot-integration.md](references/spring-boot-integration.md) - Spring Boot integration patterns
- [references/aws-official-documentation.md](references/aws-official-documentation.md) - Official AWS documentation and best practices

## Constraints and Warnings

- **Message Size**: SQS and SNS messages limited to 256KB
- **Visibility Timeout**: SQS messages become visible again after timeout if not deleted
- **FIFO Naming**: FIFO queues and topics must end with `.fifo` suffix
- **FIFO Throughput**: FIFO queues have lower throughput limits (300 msg/sec)
- **Message Retention**: SQS messages retained maximum 14 days
- **Dead Letter Queues**: Configure DLQ to prevent message loss
- **Subscription Limits**: SNS topics have limits on number of subscriptions
- **Filter Policies**: SNS filter policies have complexity limits
- **Cross-Region**: SQS queues are region-specific; SNS topics can be cross-region
- **Cost**: Both services charge per API call and data transfer

Overview

This skill provides ready-to-use AWS messaging patterns implemented with the AWS SDK for Java 2.x, covering SQS queues and SNS topics. It includes client setup, queue/topic management, message send/receive, FIFO and DLQ support, subscription wiring, and Spring Boot integration examples. Use it to speed up production-grade message-driven and pub/sub architectures with clear best practices.

How this skill works

The skill supplies code examples and patterns for creating SQS queues and SNS topics, sending and receiving messages, configuring FIFO behavior and dead-letter queues, and subscribing endpoints (SQS, Lambda, email, SMS). It shows client configuration for the SDK, long-polling and batch operations for SQS, publish/subscribe flows for SNS, and Spring Boot service wiring for seamless integration. It also covers testing approaches using LocalStack or Testcontainers and recommendations for monitoring and retries.

When to use it

  • Building queue-based processing with Amazon SQS (standard or FIFO)
  • Publishing events and notifications via Amazon SNS with multiple subscribers
  • Implementing DLQs and retry strategies for robust message handling
  • Integrating AWS messaging into Spring Boot microservices
  • Creating SNS-to-SQS subscriptions for durable pub/sub pipelines
  • Testing messaging flows locally with LocalStack or Testcontainers

Best practices

  • Use long polling (waitTimeSeconds) to reduce empty receives and API costs
  • Batch send/receive operations to improve throughput and lower call volume
  • Make processing idempotent to handle duplicate deliveries and retries
  • Configure appropriate visibility timeouts and set up DLQs for failures
  • Apply SNS filter policies and message attributes to route notifications
  • Keep region consistency and secure access with IAM and encryption

Example use cases

  • Order processing: publish orders to SNS and fan out to multiple SQS queues for different microservices
  • Async task processing: push jobs to an SQS queue, consume with workers using long polling and batch deletes
  • Notification system: publish alerts to an SNS topic with email, SMS, and SQS subscribers
  • FIFO workflows: enforce ordering and deduplication for transactional message streams
  • Error handling: route failed messages to DLQs for later inspection and replay

FAQ

What message size limits should I expect?

SQS and SNS payloads should be kept under 256 KB; use S3 for larger payloads and pass references in messages.

How do I test messaging locally?

Use LocalStack or Testcontainers to run SQS/SNS emulation for integration tests and CI pipelines.