BAEL-1173 - Add spring-cloud-aws module (#3046)
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
6211154062
commit
dba132f7de
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.spring.cloud.aws;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringCloudAwsApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringCloudAwsApplication.class, args);
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.spring.cloud.aws.config;
|
||||
|
||||
import com.amazonaws.services.sns.AmazonSNS;
|
||||
import com.amazonaws.services.sqs.AmazonSQSAsync;
|
||||
import org.springframework.cloud.aws.messaging.core.NotificationMessagingTemplate;
|
||||
import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class SpringCloudAwsConfig {
|
||||
|
||||
@Bean
|
||||
public QueueMessagingTemplate queueMessagingTemplate(AmazonSQSAsync amazonSQSAsync) {
|
||||
return new QueueMessagingTemplate(amazonSQSAsync);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NotificationMessagingTemplate notificationMessagingTemplate(AmazonSNS amazonSNS) {
|
||||
return new NotificationMessagingTemplate(amazonSNS);
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.spring.cloud.aws.s3;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.io.WritableResource;
|
||||
import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
@Component
|
||||
public class SpringCloudS3 {
|
||||
|
||||
@Autowired
|
||||
ResourceLoader resourceLoader;
|
||||
|
||||
@Autowired
|
||||
ResourcePatternResolver resourcePatternResolver;
|
||||
|
||||
public void downloadS3Object(String s3Url) throws IOException {
|
||||
Resource resource = resourceLoader.getResource(s3Url);
|
||||
File downloadedS3Object = new File(resource.getFilename());
|
||||
try (InputStream inputStream = resource.getInputStream()) {
|
||||
Files.copy(inputStream, downloadedS3Object.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
}
|
||||
|
||||
public void uploadFileToS3(File file, String s3Url) throws IOException {
|
||||
WritableResource resource = (WritableResource) resourceLoader.getResource(s3Url);
|
||||
try (OutputStream outputStream = resource.getOutputStream()) {
|
||||
Files.copy(file.toPath(), outputStream);
|
||||
}
|
||||
}
|
||||
|
||||
public void downloadMultipleS3Objects(String s3UrlPattern) throws IOException {
|
||||
Resource[] allFileMatchingPatten = this.resourcePatternResolver.getResources(s3UrlPattern);
|
||||
for (Resource resource : allFileMatchingPatten) {
|
||||
String fileName = resource.getFilename();
|
||||
fileName = fileName.substring(0, fileName.lastIndexOf("/") + 1);
|
||||
File downloadedS3Object = new File(fileName);
|
||||
try (InputStream inputStream = resource.getInputStream()) {
|
||||
Files.copy(inputStream, downloadedS3Object.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.spring.cloud.aws.sns;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.aws.messaging.config.annotation.NotificationMessage;
|
||||
import org.springframework.cloud.aws.messaging.config.annotation.NotificationSubject;
|
||||
import org.springframework.cloud.aws.messaging.endpoint.NotificationStatus;
|
||||
import org.springframework.cloud.aws.messaging.endpoint.annotation.NotificationMessageMapping;
|
||||
import org.springframework.cloud.aws.messaging.endpoint.annotation.NotificationSubscriptionMapping;
|
||||
import org.springframework.cloud.aws.messaging.endpoint.annotation.NotificationUnsubscribeConfirmationMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/topic-subscriber")
|
||||
public class SNSEndpointController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SNSEndpointController.class);
|
||||
|
||||
@NotificationMessageMapping
|
||||
public void receiveNotification(@NotificationMessage String message, @NotificationSubject String subject) {
|
||||
logger.info("Received message: {}, having subject: {}", message, subject);
|
||||
}
|
||||
|
||||
@NotificationUnsubscribeConfirmationMapping
|
||||
public void confirmSubscriptionMessage(NotificationStatus notificationStatus) {
|
||||
logger.info("Unsubscribed from Topic");
|
||||
notificationStatus.confirmSubscription();
|
||||
}
|
||||
|
||||
@NotificationSubscriptionMapping
|
||||
public void confirmUnsubscribeMessage(NotificationStatus notificationStatus) {
|
||||
logger.info("Subscribed to Topic");
|
||||
notificationStatus.confirmSubscription();
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.spring.cloud.aws.sns;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.aws.messaging.core.NotificationMessagingTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SNSMessageSender {
|
||||
|
||||
@Autowired
|
||||
NotificationMessagingTemplate notificationMessagingTemplate;
|
||||
|
||||
public void send(String topicName, Object message, String subject) {
|
||||
notificationMessagingTemplate.sendNotification(topicName, message, subject);
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.spring.cloud.aws.sqs;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate;
|
||||
import org.springframework.cloud.aws.messaging.listener.annotation.SqsListener;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.messaging.handler.annotation.Header;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
@Component
|
||||
@Lazy
|
||||
public class SpringCloudSQS {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SpringCloudSQS.class);
|
||||
|
||||
static final String QUEUE_NAME = "spring-cloud-test-queue";
|
||||
|
||||
/*
|
||||
* CountDownLatch is added to wait for messages
|
||||
* during integration test
|
||||
*/
|
||||
CountDownLatch countDownLatch;
|
||||
|
||||
public void setCountDownLatch(CountDownLatch countDownLatch) {
|
||||
this.countDownLatch = countDownLatch;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
QueueMessagingTemplate queueMessagingTemplate;
|
||||
|
||||
@SqsListener(QUEUE_NAME)
|
||||
public void receiveMessage(String message, @Header("SenderId") String senderId) {
|
||||
logger.info("Received message: {}, having SenderId: {}", message, senderId);
|
||||
if (countDownLatch != null) {
|
||||
countDownLatch.countDown();
|
||||
}
|
||||
}
|
||||
|
||||
public void send(String queueName, Object message) {
|
||||
queueMessagingTemplate.convertAndSend(queueName, message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
cloud.aws.credentials.accessKey=YourAccessKey
|
||||
cloud.aws.credentials.secretKey=YourSecretKey
|
||||
cloud.aws.region.static=us-east-1
|
||||
|
||||
cloud.aws.rds.spring-cloud-test-db
|
||||
cloud.aws.rds.spring-cloud-test-db.password=se3retpass
|
||||
|
||||
# These 3 properties are optional
|
||||
cloud.aws.rds.spring-cloud-test-db.username=testuser
|
||||
cloud.aws.rds.spring-cloud-test-db.readReplicaSupport=true
|
||||
cloud.aws.rds.spring-cloud-test-db.databaseName=test
|
||||
|
||||
# Disable auto cloudfromation
|
||||
cloud.aws.stack.auto=false
|
||||
Reference in New Issue
Block a user