Skip to content

Commit 0bee011

Browse files
Use AWS SQS to update the initial order status (#5)
2 parents 87cff38 + ace2fe6 commit 0bee011

File tree

6 files changed

+129
-4
lines changed

6 files changed

+129
-4
lines changed

pom.xml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<description>FIAP SOAT1 2023 - Group 63 - Phase 5 - Production microservice </description>
1717
<properties>
1818
<java.version>17</java.version>
19+
<aws.sdk.version>1.12.674</aws.sdk.version>
1920
<sonar.coverage.exclusions>
2021
**/config/*,
2122
**/dto/*DTO.*,
@@ -27,6 +28,17 @@
2728
<url>https://jitpack.io</url>
2829
</repository>
2930
</repositories>
31+
<dependencyManagement>
32+
<dependencies>
33+
<dependency>
34+
<groupId>io.awspring.cloud</groupId>
35+
<artifactId>spring-cloud-aws-dependencies</artifactId>
36+
<version>3.0.0</version>
37+
<type>pom</type>
38+
<scope>import</scope>
39+
</dependency>
40+
</dependencies>
41+
</dependencyManagement>
3042
<dependencies>
3143
<dependency>
3244
<groupId>org.springframework.boot</groupId>
@@ -40,6 +52,14 @@
4052
<groupId>org.springframework.boot</groupId>
4153
<artifactId>spring-boot-starter-validation</artifactId>
4254
</dependency>
55+
<dependency>
56+
<groupId>io.awspring.cloud</groupId>
57+
<artifactId>spring-cloud-aws-starter</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>io.awspring.cloud</groupId>
61+
<artifactId>spring-cloud-aws-starter-sqs</artifactId>
62+
</dependency>
4363
<dependency>
4464
<groupId>org.springframework.boot</groupId>
4565
<artifactId>spring-boot-devtools</artifactId>
@@ -89,7 +109,12 @@
89109
<dependency>
90110
<groupId>com.amazonaws</groupId>
91111
<artifactId>aws-java-sdk-dynamodb</artifactId>
92-
<version>1.12.641</version>
112+
<version>${aws.sdk.version}</version>
113+
</dependency>
114+
<dependency>
115+
<groupId>com.amazonaws</groupId>
116+
<artifactId>aws-java-sdk-sqs</artifactId>
117+
<version>${aws.sdk.version}</version>
93118
</dependency>
94119
<dependency>
95120
<groupId>com.github.derjust</groupId>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package br.com.grupo63.serviceproduction.api.controller.status;
2+
3+
import br.com.grupo63.serviceproduction.controller.StatusController;
4+
import br.com.grupo63.techchallenge.common.exception.NotFoundException;
5+
import br.com.grupo63.techchallenge.common.exception.ValidationException;
6+
import com.amazonaws.services.sqs.model.Message;
7+
import io.awspring.cloud.sqs.annotation.SqsListener;
8+
import lombok.RequiredArgsConstructor;
9+
import org.springframework.stereotype.Component;
10+
11+
import java.util.Arrays;
12+
13+
@RequiredArgsConstructor
14+
15+
@Component
16+
public class StatusQueueController {
17+
private final StatusController controller;
18+
19+
@SqsListener(value = "approvedPayments.fifo")
20+
public void processMessage(int orderId) throws ValidationException, NotFoundException {
21+
controller.advanceStatus(orderId);
22+
}
23+
}

src/main/java/br/com/grupo63/serviceproduction/config/DynamoDBConfig.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public class DynamoDBConfig {
3232
private String awsSessionToken;
3333

3434
@Bean
35-
public AmazonDynamoDB amazonDynamoDB() {
35+
public AmazonDynamoDB amazonDynamoDB(AWSCredentials awsCredentials) {
3636
AmazonDynamoDB amazonDynamoDB
37-
= new AmazonDynamoDBClient(awsCredentials());
37+
= new AmazonDynamoDBClient(awsCredentials);
3838

3939
if (!StringUtils.isEmpty(awsDynamoDBEndpoint)) {
4040
amazonDynamoDB.setEndpoint(awsDynamoDBEndpoint);
@@ -44,7 +44,7 @@ public AmazonDynamoDB amazonDynamoDB() {
4444
}
4545

4646
@Bean
47-
public AWSCredentials awsCredentials() {
47+
public AWSCredentials AWSCredentials() {
4848
if (awsSessionToken == null || awsSessionToken.isBlank()) {
4949
return new BasicAWSCredentials(
5050
awsAccessKey, awsSecretKey);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package br.com.grupo63.serviceproduction.config;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
7+
import software.amazon.awssdk.auth.credentials.AwsCredentials;
8+
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
9+
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
10+
import software.amazon.awssdk.regions.Region;
11+
import software.amazon.awssdk.services.sqs.SqsAsyncClient;
12+
13+
@Configuration
14+
class SQSClientConfig {
15+
16+
@Value("${spring.cloud.aws.credentials.access-key}")
17+
private String accessKey;
18+
19+
@Value("${spring.cloud.aws.credentials.secret-key}")
20+
private String secretKey;
21+
22+
@Value("${app.aws.session-token}")
23+
private String sessionToken;
24+
25+
@Value("${spring.cloud.aws.region.static}")
26+
private String region;
27+
28+
@Bean
29+
SqsAsyncClient sqsAsyncClient(AwsCredentials awsCredentials) {
30+
return SqsAsyncClient
31+
.builder()
32+
.region(Region.of(region))
33+
.credentialsProvider(StaticCredentialsProvider
34+
.create(awsCredentials))
35+
.build();
36+
}
37+
38+
@Bean
39+
public AwsCredentials awsCredentials() {
40+
if (sessionToken == null || sessionToken.isBlank()) {
41+
return AwsBasicCredentials.create(accessKey, secretKey);
42+
}
43+
return AwsSessionCredentials.create(accessKey, secretKey, sessionToken);
44+
}
45+
}

src/main/resources/application-dev.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,30 @@ spring:
1313
flyway:
1414
locations: classpath:db/migrations/{vendor}/
1515
enabled: true
16+
cloud:
17+
aws:
18+
credentials:
19+
access-key: "${AWS_ACCESS_KEY:accesskey}"
20+
secret-key: "${AWS_SECRET_KEY:secretkey}"
21+
region:
22+
static: "${AWS_REGION:us-east-2}"
23+
sqs:
24+
endpoint: "${AWS_SQS_ENDPOINT:endpoint}"
1625
jwt:
1726
token:
1827
key:
1928
public: "${JWT_PUBLIC_KEY:MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqStd8n4SGNM0eZhV/hzU+urHA5/IMZPoP9YQ9ZcLKWiX33nI6bSuZMCrLZcJExf63xS+uxDpGxM8Mnk2zOdl+lPwANXLzP1us5P1PyA3YPycW9J7C5YTQW0GiEL3M93ZX7vMJiVoBYblP3JPlYnoYlBORuc0JPk33KtfEZP+78qXpPHM8imYrJLe8ceiDLLFDU/nh5KC2dWAy3ci1ahoJ1Q9ELhp3IZLvOTX57H/T2VKOYOya5+ST41h+JjzI+qGTVnLcKaW+k25YLlVnkSspvdx98+yQDi7kbOTS6yRZHUPD6wPk/nUozpD0nZKccoH4W+zMwmQVtsAA6JCA9gfGwIDAQAB}"
29+
30+
app:
31+
docs-api-url: "${DOCS_API_URL:(no value)}"
32+
aws:
33+
access-key: "${AWS_ACCESS_KEY:no_access_key}"
34+
secret-key: "${AWS_SECRET_KEY:no_secret_key}"
35+
session-token: "${AWS_SESSION_TOKEN:}"
36+
dynamodb:
37+
endpoint: "${AWS_DYNAMODB_ENDPOINT:dynamodb.us-east-1.amazonaws.com}"
38+
39+
server:
40+
port: 8082
41+
servlet:
42+
context-path: "/production"

src/main/resources/application.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ spring:
1414
flyway:
1515
locations: classpath:db/migrations/{vendor}/
1616
enabled: true
17+
cloud:
18+
aws:
19+
credentials:
20+
access-key: "${AWS_ACCESS_KEY:accesskey}"
21+
secret-key: "${AWS_SECRET_KEY:secretkey}"
22+
region:
23+
static: "${AWS_REGION:us-east-2}"
24+
sqs:
25+
endpoint: "${AWS_SQS_ENDPOINT:endpoint}"
1726

1827
springdoc:
1928
swagger-ui:

0 commit comments

Comments
 (0)