Sending AWS SQS messages with JAVA
Tags: java, aws, sqs, developer
Published: 2022-12-24 21:41:39

Hello, I’d like to share with you a quick tutorial about how to send messages to a SQS queue in AWS in a simple way using a REST API with JAVA, in this tutorial I won’t get into about themes such as security and architecture, just the comprehension about the basics of access and SQS handle.

**I’m using:**
- JAVA 11 with Spring 2.3.3
- IntelliJ
- Maven 4.0.0

It’s necessary to have a little bit of knowledge about AWS accounts and SQS queues to go with this tutorial, just a bit, really!

**you’ll need to include the following dependencies on your POM file:**

`<dependency>
   <groupId>com.amazonaws</groupId>
   <artifactId>aws-java-sdk</artifactId>
   <version>LATEST</version>
</dependency>`

Now we have to put some access information in our properties file to authentication in AWS and to tell which SQS queue we want to use (aws.queue). The access and secret key are informed in AWS console when you created it:

`aws.access_key_id=
aws.secret_access_key=
aws.queue=
aws.region=sa-east-1`

**ATTENTION:** aws.region=sa-east-1 indicates the region where I am, check the correct region for you in [AWS website](https://docs.aws.amazon.com/pt_br/AWSEC2/latest/UserGuide/using-regions-availability-zones.html#concepts-available-regions).

With a configuration class we make the reference to the information that we put in our properties file above, and then we get authenticated in AWS using a Bean sqsClient:

```
@Configuration
public class AWSConfig {

    @Value("${aws.access_key_id}")
    private String awsId;

    @Value("${aws.secret_access_key}")
    private String awsKey;

    @Value("${aws.region}")
    private String region;

    @Bean
    public AmazonSQS sqsClient(){
        BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(awsId, awsKey);
        return AmazonSQSClientBuilder.standard().withRegion(Regions.fromName(region)).withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials)).build();
    }
}
```

Once our configuration is done we create then a service to send messages to the queue:

```
@Service
public class SQSService {

    @Autowired
    private AmazonSQS sqsClient;

    @Value("${sqs.queue}")
    private String queueUrl;

    public void sendMessage(String msg) {
        SendMessageRequest send_msg_request = new SendMessageRequest()
                .withQueueUrl(queueUrl)
                .withMessageBody(msg)
                .withDelaySeconds(5);
        sqsClient.sendMessage(send_msg_request);
    }
}
```

I am indicating the queue which I want to access in my properties file and using an amazon interface (AmazonSQS), so I can invoke the method sendMessage which receives as an argument a SendMessageRequest object. The method withDelaySeconds tells in seconds the delay to deliver the message to the queue.

To access our method I’ve created an specific service to AWS tests, this one will be called by our Controller class:

```
@Service
public class AWSTestService {

    @Autowired
    private SQSService sqsService;

    public void sendMessage(SQSMessage sqsMessage) {
        String msg = sqsMessage.getMsg();
        sqsService.sendMessage(msg);
    }
}
```

In this service I’m injecting the service we’ve created to send the message

To receive the message as a RequestBody I’ve created a basic domain class:

```
public class SQSMessage {
    private String msg;

    public SQSMessage(){}

    public SQSMessage(String msg) {
        this.msg = msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getMsg() {
        return msg;
    }
}
```

Lastly, using the domain class the Controller can call the methods we’ve created and send the message that we are receiving in our request body:

```
@RestController
@RequestMapping("/awstest")
public class AWSTestController {

    @Autowired
    private AWSTestService service;

    @PostMapping(value="/message")
    public ResponseEntity<Void> sendMessage(@RequestBody SQSMessage sqsMessage){
        service.sendMessage(sqsMessage);
        return ResponseEntity.ok().build();
    }

}
```

Now you can test it using postman and checking the amazon console in SQS section the messages you are producing coming.

**This was my first write here. I hope you like it. :-)**