Skip to content

Commit dba132f

Browse files
vivekkr12pivovarit
authored andcommitted
BAEL-1173 - Add spring-cloud-aws module (eugenp#3046)
1 parent 6211154 commit dba132f

18 files changed

Lines changed: 832 additions & 0 deletions

‎spring-cloud/pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<module>spring-cloud-gateway</module>
1919
<module>spring-cloud-stream</module>
2020
<module>spring-cloud-connectors-heroku</module>
21+
<module>spring-cloud-aws</module>
2122
</modules>
2223
<packaging>pom</packaging>
2324

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Spring Cloud AWS
2+
3+
#### Running the Integration Tests
4+
5+
To run the Integration Tests, we need to have an AWS account and have API keys generated for programmatic access. Edit
6+
the `application.properties` file to add the following properties:
7+
8+
```
9+
cloud.aws.credentials.accessKey=YourAccessKey
10+
cloud.aws.credentials.secretKey=YourSecretKey
11+
cloud.aws.region.static=us-east-1
12+
```
13+
14+
To test automatic DataSource creation from RDS instance, we also need to create an RDS instance in the AWS account.
15+
Let's say that the RDS instance is called `spring-cloud-test-db` having the master password `se3retpass`, then we need
16+
to write the following in `application.properties`:
17+
18+
```
19+
cloud.aws.rds.spring-cloud-test-db
20+
cloud.aws.rds.spring-cloud-test-db.password=se3retpass
21+
```
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.baeldung.spring.cloud</groupId>
7+
<artifactId>spring-cloud-aws</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Spring Cloud AWS</name>
12+
<description>Spring Cloud AWS Examples</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.8.RELEASE</version>
18+
<relativePath /> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
<spring-cloud.version>Dalston.SR4</spring-cloud.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-web</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.cloud</groupId>
35+
<artifactId>spring-cloud-starter-aws</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.cloud</groupId>
39+
<artifactId>spring-cloud-starter-aws-jdbc</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework.cloud</groupId>
43+
<artifactId>spring-cloud-starter-aws-messaging</artifactId>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-test</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>org.postgresql</groupId>
54+
<artifactId>postgresql</artifactId>
55+
</dependency>
56+
57+
</dependencies>
58+
59+
<dependencyManagement>
60+
<dependencies>
61+
<dependency>
62+
<groupId>org.springframework.cloud</groupId>
63+
<artifactId>spring-cloud-dependencies</artifactId>
64+
<version>${spring-cloud.version}</version>
65+
<type>pom</type>
66+
<scope>import</scope>
67+
</dependency>
68+
</dependencies>
69+
</dependencyManagement>
70+
71+
<build>
72+
<plugins>
73+
<plugin>
74+
<groupId>org.springframework.boot</groupId>
75+
<artifactId>spring-boot-maven-plugin</artifactId>
76+
</plugin>
77+
78+
<plugin>
79+
<artifactId>maven-surefire-plugin</artifactId>
80+
<configuration>
81+
<excludes>
82+
<exclude>**/*IntegrationTest.java</exclude>
83+
</excludes>
84+
</configuration>
85+
</plugin>
86+
87+
</plugins>
88+
</build>
89+
90+
91+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.spring.cloud.aws;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringCloudAwsApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringCloudAwsApplication.class, args);
11+
}
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.spring.cloud.aws.config;
2+
3+
import com.amazonaws.services.sns.AmazonSNS;
4+
import com.amazonaws.services.sqs.AmazonSQSAsync;
5+
import org.springframework.cloud.aws.messaging.core.NotificationMessagingTemplate;
6+
import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
@Configuration
11+
public class SpringCloudAwsConfig {
12+
13+
@Bean
14+
public QueueMessagingTemplate queueMessagingTemplate(AmazonSQSAsync amazonSQSAsync) {
15+
return new QueueMessagingTemplate(amazonSQSAsync);
16+
}
17+
18+
@Bean
19+
public NotificationMessagingTemplate notificationMessagingTemplate(AmazonSNS amazonSNS) {
20+
return new NotificationMessagingTemplate(amazonSNS);
21+
}
22+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.spring.cloud.aws.s3;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.core.io.Resource;
5+
import org.springframework.core.io.ResourceLoader;
6+
import org.springframework.core.io.WritableResource;
7+
import org.springframework.core.io.support.ResourcePatternResolver;
8+
import org.springframework.stereotype.Component;
9+
10+
import java.io.File;
11+
import java.io.IOException;
12+
import java.io.InputStream;
13+
import java.io.OutputStream;
14+
import java.nio.file.Files;
15+
import java.nio.file.StandardCopyOption;
16+
17+
@Component
18+
public class SpringCloudS3 {
19+
20+
@Autowired
21+
ResourceLoader resourceLoader;
22+
23+
@Autowired
24+
ResourcePatternResolver resourcePatternResolver;
25+
26+
public void downloadS3Object(String s3Url) throws IOException {
27+
Resource resource = resourceLoader.getResource(s3Url);
28+
File downloadedS3Object = new File(resource.getFilename());
29+
try (InputStream inputStream = resource.getInputStream()) {
30+
Files.copy(inputStream, downloadedS3Object.toPath(), StandardCopyOption.REPLACE_EXISTING);
31+
}
32+
}
33+
34+
public void uploadFileToS3(File file, String s3Url) throws IOException {
35+
WritableResource resource = (WritableResource) resourceLoader.getResource(s3Url);
36+
try (OutputStream outputStream = resource.getOutputStream()) {
37+
Files.copy(file.toPath(), outputStream);
38+
}
39+
}
40+
41+
public void downloadMultipleS3Objects(String s3UrlPattern) throws IOException {
42+
Resource[] allFileMatchingPatten = this.resourcePatternResolver.getResources(s3UrlPattern);
43+
for (Resource resource : allFileMatchingPatten) {
44+
String fileName = resource.getFilename();
45+
fileName = fileName.substring(0, fileName.lastIndexOf("/") + 1);
46+
File downloadedS3Object = new File(fileName);
47+
try (InputStream inputStream = resource.getInputStream()) {
48+
Files.copy(inputStream, downloadedS3Object.toPath(), StandardCopyOption.REPLACE_EXISTING);
49+
}
50+
}
51+
}
52+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.spring.cloud.aws.sns;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.cloud.aws.messaging.config.annotation.NotificationMessage;
6+
import org.springframework.cloud.aws.messaging.config.annotation.NotificationSubject;
7+
import org.springframework.cloud.aws.messaging.endpoint.NotificationStatus;
8+
import org.springframework.cloud.aws.messaging.endpoint.annotation.NotificationMessageMapping;
9+
import org.springframework.cloud.aws.messaging.endpoint.annotation.NotificationSubscriptionMapping;
10+
import org.springframework.cloud.aws.messaging.endpoint.annotation.NotificationUnsubscribeConfirmationMapping;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
@RestController
15+
@RequestMapping("/topic-subscriber")
16+
public class SNSEndpointController {
17+
18+
private static final Logger logger = LoggerFactory.getLogger(SNSEndpointController.class);
19+
20+
@NotificationMessageMapping
21+
public void receiveNotification(@NotificationMessage String message, @NotificationSubject String subject) {
22+
logger.info("Received message: {}, having subject: {}", message, subject);
23+
}
24+
25+
@NotificationUnsubscribeConfirmationMapping
26+
public void confirmSubscriptionMessage(NotificationStatus notificationStatus) {
27+
logger.info("Unsubscribed from Topic");
28+
notificationStatus.confirmSubscription();
29+
}
30+
31+
@NotificationSubscriptionMapping
32+
public void confirmUnsubscribeMessage(NotificationStatus notificationStatus) {
33+
logger.info("Subscribed to Topic");
34+
notificationStatus.confirmSubscription();
35+
}
36+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.spring.cloud.aws.sns;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.cloud.aws.messaging.core.NotificationMessagingTemplate;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class SNSMessageSender {
9+
10+
@Autowired
11+
NotificationMessagingTemplate notificationMessagingTemplate;
12+
13+
public void send(String topicName, Object message, String subject) {
14+
notificationMessagingTemplate.sendNotification(topicName, message, subject);
15+
}
16+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.spring.cloud.aws.sqs;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate;
7+
import org.springframework.cloud.aws.messaging.listener.annotation.SqsListener;
8+
import org.springframework.context.annotation.Lazy;
9+
import org.springframework.messaging.handler.annotation.Header;
10+
import org.springframework.stereotype.Component;
11+
12+
import java.util.concurrent.CountDownLatch;
13+
14+
@Component
15+
@Lazy
16+
public class SpringCloudSQS {
17+
18+
private static final Logger logger = LoggerFactory.getLogger(SpringCloudSQS.class);
19+
20+
static final String QUEUE_NAME = "spring-cloud-test-queue";
21+
22+
/*
23+
* CountDownLatch is added to wait for messages
24+
* during integration test
25+
*/
26+
CountDownLatch countDownLatch;
27+
28+
public void setCountDownLatch(CountDownLatch countDownLatch) {
29+
this.countDownLatch = countDownLatch;
30+
}
31+
32+
@Autowired
33+
QueueMessagingTemplate queueMessagingTemplate;
34+
35+
@SqsListener(QUEUE_NAME)
36+
public void receiveMessage(String message, @Header("SenderId") String senderId) {
37+
logger.info("Received message: {}, having SenderId: {}", message, senderId);
38+
if (countDownLatch != null) {
39+
countDownLatch.countDown();
40+
}
41+
}
42+
43+
public void send(String queueName, Object message) {
44+
queueMessagingTemplate.convertAndSend(queueName, message);
45+
}
46+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cloud.aws.credentials.accessKey=YourAccessKey
2+
cloud.aws.credentials.secretKey=YourSecretKey
3+
cloud.aws.region.static=us-east-1
4+
5+
cloud.aws.rds.spring-cloud-test-db
6+
cloud.aws.rds.spring-cloud-test-db.password=se3retpass
7+
8+
# These 3 properties are optional
9+
cloud.aws.rds.spring-cloud-test-db.username=testuser
10+
cloud.aws.rds.spring-cloud-test-db.readReplicaSupport=true
11+
cloud.aws.rds.spring-cloud-test-db.databaseName=test
12+
13+
# Disable auto cloudfromation
14+
cloud.aws.stack.auto=false

0 commit comments

Comments
 (0)