├── .coveralls.yml ├── spring-boot-starter-mock-sample2 ├── build.gradle └── src │ ├── main │ ├── resources │ │ └── application.yml │ └── java │ │ └── com │ │ └── github │ │ └── jojoldu │ │ └── sample2 │ │ ├── Sample2Application.java │ │ └── SampleListener.java │ └── test │ ├── resources │ └── application.yml │ └── java │ └── com │ └── github │ └── jojoldu │ └── sample2 │ └── Sample2Test.java ├── spring-boot-starter-mock-sample ├── build.gradle └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── jojoldu │ │ │ └── sample │ │ │ ├── domain │ │ │ ├── PointRepository.java │ │ │ └── Point.java │ │ │ ├── SampleApplication.java │ │ │ ├── controller │ │ │ ├── SampleController.java │ │ │ ├── Sample2Controller.java │ │ │ └── Sample3Controller.java │ │ │ ├── dto │ │ │ └── PointDto.java │ │ │ └── listener │ │ │ ├── Sample2Listener.java │ │ │ └── Sample3Listener.java │ └── resources │ │ └── application.yml │ └── test │ ├── resources │ └── application.yml │ └── java │ └── com │ └── github │ └── jojoldu │ └── sample │ └── controller │ ├── Sample3ControllerTest.java │ └── Sample2ControllerTest.java ├── images └── log.png ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── jitpack.yml ├── settings.gradle ├── http └── sample.http ├── mock-sqs-spring-boot-starter ├── src │ ├── main │ │ ├── resources │ │ │ └── META-INF │ │ │ │ ├── spring.factories │ │ │ │ └── additional-spring-configuration-metadata.json │ │ └── java │ │ │ └── com │ │ │ └── github │ │ │ └── jojoldu │ │ │ └── sqs │ │ │ ├── annotation │ │ │ ├── server │ │ │ │ ├── MockServerConstant.java │ │ │ │ ├── MockServerMessageType.java │ │ │ │ ├── ConditionalOnMockSqs.java │ │ │ │ ├── ConditionalOnMockSqsServer.java │ │ │ │ ├── ConditionalOnMissingMockSqsServer.java │ │ │ │ ├── OnMockSqsServerCondition.java │ │ │ │ ├── OnMissingMockSqsServerCondition.java │ │ │ │ ├── OnMockSqsCondition.java │ │ │ │ └── OnMockSqsServerBaseCondition.java │ │ │ ├── ApplicationStartup.java │ │ │ └── utils │ │ │ │ └── RandomPortFinder.java │ │ │ ├── exception │ │ │ └── SqsMockException.java │ │ │ ├── AwsMockSqsAutoConfiguration.java │ │ │ ├── MockQueueGenerator.java │ │ │ ├── config │ │ │ ├── SqsProperties.java │ │ │ └── SqsQueues.java │ │ │ └── AwsMockSqsServerAutoConfiguration.java │ └── test │ │ └── java │ │ └── com │ │ └── github │ │ └── jojoldu │ │ └── sqs │ │ ├── config │ │ └── SqsQueueTest.java │ │ └── annotation │ │ ├── utils │ │ └── RandomPortFinderTest.java │ │ └── server │ │ └── OnMissingMockSqsServerConditionTest.java └── build.gradle ├── .gitignore ├── .travis.yml ├── LICENSE ├── gradlew.bat ├── README.md └── gradlew /.coveralls.yml: -------------------------------------------------------------------------------- 1 | repo_token: muy6Yg5mFE2lGf3mybBCXNr40O84sPrTa -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample2/build.gradle: -------------------------------------------------------------------------------- 1 | dependencies { 2 | } 3 | -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample/build.gradle: -------------------------------------------------------------------------------- 1 | 2 | dependencies { 3 | } 4 | -------------------------------------------------------------------------------- /images/log.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jojoldu/spring-boot-aws-mock/HEAD/images/log.png -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jojoldu/spring-boot-aws-mock/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /jitpack.yml: -------------------------------------------------------------------------------- 1 | jdk: 2 | - oraclejdk8 3 | install: 4 | - echo ">>>>>>>>>> Running a custom install command" 5 | - ./gradlew clean build install -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'spring-boot-aws-mock' 2 | 3 | include 'mock-sqs-spring-boot-starter', 4 | 'spring-boot-starter-mock-sample', 5 | 'spring-boot-starter-mock-sample2' 6 | -------------------------------------------------------------------------------- /http/sample.http: -------------------------------------------------------------------------------- 1 | GET localhost:8080/sample 2 | 3 | ### 4 | POST localhost:8080/point 5 | Content-Type: application/json 6 | 7 | { 8 | "userId":0, 9 | "savePoint":0, 10 | "description": "str" 11 | } 12 | ### -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | zipStoreBase=GRADLE_USER_HOME 4 | zipStorePath=wrapper/dists 5 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-bin.zip 6 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | # Auto Configure 2 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 3 | com.github.jojoldu.sqs.AwsMockSqsAutoConfiguration,\ 4 | com.github.jojoldu.sqs.AwsMockSqsServerAutoConfiguration -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample2/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: local 4 | cloud: 5 | aws: 6 | region: 7 | static: ap-northeast-2 8 | server: 9 | port: 8090 10 | --- 11 | spring: 12 | profiles: local 13 | sqs: 14 | mock: 15 | enabled: true 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample2/src/test/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: local 4 | cloud: 5 | aws: 6 | region: 7 | static: ap-northeast-2 8 | stack: 9 | auto: false 10 | --- 11 | spring: 12 | profiles: local 13 | sqs: 14 | mock: 15 | enabled: true 16 | queues: 17 | - 18 | name: 'sample' 19 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/build.gradle: -------------------------------------------------------------------------------- 1 | bootJar { enabled = false } 2 | jar { enabled = true } 3 | 4 | dependencies { 5 | 6 | implementation ('com.amazonaws:aws-java-sdk-sqs:1.11.681') 7 | 8 | // ElasticMQ (for Mocking SQS) 9 | implementation ('org.elasticmq:elasticmq-core_2.13:0.15.3') 10 | implementation ('org.elasticmq:elasticmq-rest-sqs_2.13:0.15.3') 11 | } 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /build/ 3 | !gradle/wrapper/gradle-wrapper.jar 4 | out 5 | credential-application.yml 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | 15 | ### IntelliJ IDEA ### 16 | .idea 17 | *.iws 18 | *.iml 19 | *.ipr 20 | 21 | ### NetBeans ### 22 | nbproject/private/ 23 | build/ 24 | nbbuild/ 25 | dist/ 26 | nbdist/ 27 | .nb-gradle/ -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample/src/main/java/com/github/jojoldu/sample/domain/PointRepository.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sample.domain; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | /** 6 | * Created by jojoldu@gmail.com on 2018. 3. 15. 7 | * Blog : http://jojoldu.tistory.com 8 | * Github : https://github.com/jojoldu 9 | */ 10 | 11 | public interface PointRepository extends JpaRepository { 12 | } 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - openjdk8 4 | 5 | branches: 6 | only: 7 | - master 8 | 9 | # Travis CI 서버의 Cache 활성화 10 | cache: 11 | directories: 12 | - '$HOME/.m2/repository' 13 | - '$HOME/.gradle' 14 | 15 | # clean 후 Build (Build시 자동으로 test 수행) 16 | script: "./gradlew clean test" 17 | 18 | after_success: 19 | - ./gradlew jacocoRootReport coveralls 20 | 21 | notifications: 22 | webhooks: https://fathomless-fjord-24024.herokuapp.com/notify -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/java/com/github/jojoldu/sqs/annotation/server/MockServerConstant.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs.annotation.server; 2 | 3 | /** 4 | * Created by jojoldu@gmail.com on 2018. 3. 22. 5 | * Blog : http://jojoldu.tistory.com 6 | * Github : https://github.com/jojoldu 7 | */ 8 | 9 | public interface MockServerConstant { 10 | String SQS_SERVER_PORT = "sqs.mock.port"; 11 | String SQS_SERVER_RANDOM_PORT_ENABLED = "sqs.mock.random-port-enabled"; 12 | 13 | } 14 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/java/com/github/jojoldu/sqs/exception/SqsMockException.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs.exception; 2 | 3 | import lombok.Getter; 4 | 5 | /** 6 | * Created by jojoldu@gmail.com on 2018. 5. 4. 7 | * Blog : http://jojoldu.tistory.com 8 | * Github : https://github.com/jojoldu 9 | */ 10 | 11 | @Getter 12 | public class SqsMockException extends RuntimeException { 13 | 14 | public SqsMockException(String message) { 15 | super(message); 16 | } 17 | 18 | public SqsMockException(String message, Throwable cause) { 19 | super(message, cause); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: local 4 | cloud: 5 | aws: 6 | region: 7 | static: ap-northeast-2 8 | 9 | --- 10 | spring: 11 | profiles: local 12 | sqs: 13 | mock: 14 | enabled: true 15 | queues: 16 | - 17 | name: 'sample' 18 | - 19 | name: 'sample2' 20 | - 21 | name: 'sample3-dlq' 22 | - 23 | name: 'sample3' 24 | defaultVisibilityTimeout: 1 25 | delay: 0 26 | receiveMessageWait: 0 27 | deadLettersQueue: 28 | name: "sample3-dlq" 29 | maxReceiveCount: 1 30 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/java/com/github/jojoldu/sqs/annotation/server/MockServerMessageType.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs.annotation.server; 2 | 3 | import lombok.Getter; 4 | import lombok.RequiredArgsConstructor; 5 | 6 | /** 7 | * Created by jojoldu@gmail.com on 2018. 5. 4. 8 | * Blog : http://jojoldu.tistory.com 9 | * Github : https://github.com/jojoldu 10 | */ 11 | 12 | @Getter 13 | @RequiredArgsConstructor 14 | public enum MockServerMessageType { 15 | USE_SERVER("Use Created Mock Sqs Server"), 16 | CREATE_SERVER("Create Mock Sqs Server"); 17 | 18 | private final String message; 19 | } 20 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/java/com/github/jojoldu/sqs/annotation/server/ConditionalOnMockSqs.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs.annotation.server; 2 | 3 | import org.springframework.context.annotation.Conditional; 4 | 5 | import java.lang.annotation.*; 6 | 7 | /** 8 | * Created by jojoldu@gmail.com on 2018. 3. 17. 9 | * Blog : http://jojoldu.tistory.com 10 | * Github : https://github.com/jojoldu 11 | */ 12 | 13 | @Retention(RetentionPolicy.RUNTIME) 14 | @Target({ ElementType.TYPE, ElementType.METHOD }) 15 | @Documented 16 | @Conditional(OnMockSqsCondition.class) 17 | public @interface ConditionalOnMockSqs { 18 | } 19 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/java/com/github/jojoldu/sqs/annotation/server/ConditionalOnMockSqsServer.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs.annotation.server; 2 | 3 | import org.springframework.context.annotation.Conditional; 4 | 5 | import java.lang.annotation.*; 6 | 7 | /** 8 | * Created by jojoldu@gmail.com on 2018. 3. 17. 9 | * Blog : http://jojoldu.tistory.com 10 | * Github : https://github.com/jojoldu 11 | */ 12 | 13 | @Retention(RetentionPolicy.RUNTIME) 14 | @Target({ ElementType.TYPE, ElementType.METHOD }) 15 | @Documented 16 | @Conditional(OnMockSqsServerCondition.class) 17 | public @interface ConditionalOnMockSqsServer { 18 | } 19 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/java/com/github/jojoldu/sqs/annotation/server/ConditionalOnMissingMockSqsServer.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs.annotation.server; 2 | 3 | import org.springframework.context.annotation.Conditional; 4 | 5 | import java.lang.annotation.*; 6 | 7 | /** 8 | * Created by jojoldu@gmail.com on 2018. 3. 17. 9 | * Blog : http://jojoldu.tistory.com 10 | * Github : https://github.com/jojoldu 11 | */ 12 | 13 | @Retention(RetentionPolicy.RUNTIME) 14 | @Target({ ElementType.TYPE, ElementType.METHOD }) 15 | @Documented 16 | @Conditional(OnMissingMockSqsServerCondition.class) 17 | public @interface ConditionalOnMissingMockSqsServer { 18 | } 19 | -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample/src/test/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: local 4 | cloud: 5 | aws: 6 | region: 7 | static: ap-northeast-2 8 | stack: 9 | auto: false 10 | --- 11 | spring: 12 | profiles: local 13 | sqs: 14 | mock: 15 | enabled: true 16 | randomPortEnabled: true 17 | queues: 18 | - 19 | name: 'sample' 20 | - 21 | name: 'sample2' 22 | - 23 | name: 'sample3-dlq' 24 | - 25 | name: 'sample3' 26 | defaultVisibilityTimeout: 1 27 | delay: 0 28 | receiveMessageWait: 0 29 | deadLettersQueue: 30 | name: "sample3-dlq" 31 | maxReceiveCount: 1 32 | 33 | 34 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/test/java/com/github/jojoldu/sqs/config/SqsQueueTest.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs.config; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.assertj.core.api.Assertions.assertThat; 6 | 7 | /** 8 | * Created by jojoldu@gmail.com on 2018. 3. 19. 9 | * Blog : http://jojoldu.tistory.com 10 | * Github : https://github.com/jojoldu 11 | */ 12 | 13 | public class SqsQueueTest { 14 | 15 | @Test 16 | public void SqsQueue_default_attribute() { 17 | SqsQueues.SqsQueue queueData = new SqsQueues.SqsQueue(); 18 | 19 | assertThat(queueData.getDefaultVisibilityTimeout()).isEqualTo(3L); 20 | assertThat(queueData.getDelay()).isEqualTo(0L); 21 | assertThat(queueData.getReceiveMessageWait()).isEqualTo(0L); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample2/src/main/java/com/github/jojoldu/sample2/Sample2Application.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sample2; 2 | 3 | import com.amazonaws.services.sqs.AmazonSQSAsync; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate; 7 | import org.springframework.context.annotation.Bean; 8 | 9 | @SpringBootApplication 10 | public class Sample2Application { 11 | 12 | @Bean 13 | public QueueMessagingTemplate queueMessagingTemplate(AmazonSQSAsync amazonSqs) { 14 | return new QueueMessagingTemplate(amazonSqs); 15 | } 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(Sample2Application.class, args); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/test/java/com/github/jojoldu/sqs/annotation/utils/RandomPortFinderTest.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs.annotation.utils; 2 | 3 | import org.junit.Test; 4 | 5 | import java.net.ServerSocket; 6 | 7 | import static org.junit.Assert.assertTrue; 8 | 9 | /** 10 | * Created by jojoldu@gmail.com on 2018. 5. 21. 11 | * Blog : http://jojoldu.tistory.com 12 | * Github : https://github.com/jojoldu 13 | */ 14 | 15 | public class RandomPortFinderTest { 16 | 17 | @Test 18 | public void get_available_port() throws Exception { 19 | //given 20 | int usePort = 10000; 21 | ServerSocket serverSocket = new ServerSocket(usePort); 22 | 23 | //when 24 | int availablePort = RandomPortFinder.findAvailablePort(); 25 | 26 | //then 27 | assertTrue(availablePort != usePort); 28 | serverSocket.close(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/java/com/github/jojoldu/sqs/annotation/ApplicationStartup.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs.annotation; 2 | 3 | import org.springframework.boot.context.event.ApplicationPreparedEvent; 4 | import org.springframework.context.ApplicationListener; 5 | import org.springframework.core.env.ConfigurableEnvironment; 6 | import org.springframework.stereotype.Component; 7 | 8 | /** 9 | * Created by jojoldu@gmail.com on 2018. 5. 23. 10 | * Blog : http://jojoldu.tistory.com 11 | * Github : https://github.com/jojoldu 12 | */ 13 | 14 | @Component 15 | public class ApplicationStartup implements ApplicationListener { 16 | 17 | @Override 18 | public void onApplicationEvent(ApplicationPreparedEvent event) { 19 | ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment(); 20 | environment.setRequiredProperties("sqs.mock.port=20000"); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample/src/main/java/com/github/jojoldu/sample/SampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sample; 2 | 3 | import com.amazonaws.services.sqs.AmazonSQSAsync; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate; 8 | import org.springframework.context.annotation.Bean; 9 | 10 | @SpringBootApplication 11 | public class SampleApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(SampleApplication.class, args); 15 | } 16 | 17 | @Bean 18 | public QueueMessagingTemplate queueMessagingTemplate(AmazonSQSAsync amazonSqs) { 19 | return new QueueMessagingTemplate(amazonSqs); 20 | } 21 | 22 | @Bean 23 | public ObjectMapper objectMapper(){ 24 | return new ObjectMapper(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample/src/main/java/com/github/jojoldu/sample/domain/Point.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sample.domain; 2 | 3 | import lombok.Builder; 4 | import lombok.Getter; 5 | import lombok.NoArgsConstructor; 6 | 7 | import javax.persistence.Entity; 8 | import javax.persistence.GeneratedValue; 9 | import javax.persistence.Id; 10 | 11 | /** 12 | * Created by jojoldu@gmail.com on 2018. 3. 15. 13 | * Blog : http://jojoldu.tistory.com 14 | * Github : https://github.com/jojoldu 15 | */ 16 | 17 | @Getter 18 | @NoArgsConstructor 19 | @Entity 20 | public class Point { 21 | 22 | @Id 23 | @GeneratedValue 24 | private Long id; 25 | 26 | private Long userId; 27 | private Long point; 28 | private String description; 29 | 30 | @Builder 31 | public Point(Long userId, Long point, String description) { 32 | this.userId = userId; 33 | this.point = point; 34 | this.description = description; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample/src/main/java/com/github/jojoldu/sample/controller/SampleController.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sample.controller; 2 | 3 | import lombok.RequiredArgsConstructor; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.time.LocalDate; 10 | 11 | /** 12 | * Created by jojoldu@gmail.com on 2018. 3. 23. 13 | * Blog : http://jojoldu.tistory.com 14 | * Github : https://github.com/jojoldu 15 | */ 16 | @Slf4j 17 | @RequiredArgsConstructor 18 | @RestController 19 | public class SampleController { 20 | 21 | private final QueueMessagingTemplate messagingTemplate; 22 | 23 | @GetMapping("/sample") 24 | public String save(){ 25 | messagingTemplate.convertAndSend("sample", "sample: "+ LocalDate.now().toString()); 26 | return "success"; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/java/com/github/jojoldu/sqs/annotation/server/OnMockSqsServerCondition.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs.annotation.server; 2 | 3 | import org.springframework.boot.autoconfigure.condition.ConditionOutcome; 4 | import org.springframework.context.annotation.ConditionContext; 5 | import org.springframework.core.Ordered; 6 | import org.springframework.core.annotation.Order; 7 | import org.springframework.core.type.AnnotatedTypeMetadata; 8 | 9 | /** 10 | * Created by jojoldu@gmail.com on 2018. 3. 17. 11 | * Blog : http://jojoldu.tistory.com 12 | * Github : https://github.com/jojoldu 13 | */ 14 | 15 | /** 16 | * 실행중인 Mock SQS 서버가 없을 경우 17 | */ 18 | @Order(Ordered.HIGHEST_PRECEDENCE + 40) 19 | class OnMockSqsServerCondition extends OnMockSqsServerBaseCondition { 20 | 21 | @Override 22 | public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { 23 | boolean isRunning = isRunning(context); 24 | return new ConditionOutcome(!isRunning, createMessage(isRunning)); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample/src/main/java/com/github/jojoldu/sample/controller/Sample2Controller.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sample.controller; 2 | 3 | import com.github.jojoldu.sample.dto.PointDto; 4 | import lombok.RequiredArgsConstructor; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate; 7 | import org.springframework.web.bind.annotation.PostMapping; 8 | import org.springframework.web.bind.annotation.RequestBody; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | /** 12 | * Created by jojoldu@gmail.com on 2018. 3. 15. 13 | * Blog : http://jojoldu.tistory.com 14 | * Github : https://github.com/jojoldu 15 | */ 16 | 17 | @Slf4j 18 | @RequiredArgsConstructor 19 | @RestController 20 | public class Sample2Controller { 21 | private final QueueMessagingTemplate messagingTemplate; 22 | 23 | @PostMapping("/sample2") 24 | public String save(@RequestBody PointDto requestDto){ 25 | messagingTemplate.convertAndSend("sample2", requestDto); 26 | return "success"; 27 | } 28 | 29 | 30 | } 31 | -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample/src/main/java/com/github/jojoldu/sample/dto/PointDto.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sample.dto; 2 | 3 | import com.github.jojoldu.sample.domain.Point; 4 | import lombok.Builder; 5 | import lombok.Getter; 6 | import lombok.NoArgsConstructor; 7 | import lombok.Setter; 8 | 9 | /** 10 | * Created by jojoldu@gmail.com on 2018. 3. 16. 11 | * Blog : http://jojoldu.tistory.com 12 | * Github : https://github.com/jojoldu 13 | */ 14 | 15 | @Getter 16 | @Setter 17 | @NoArgsConstructor 18 | public class PointDto { 19 | 20 | private Long userId; 21 | private Long savePoint; 22 | private String description; 23 | 24 | @Builder 25 | public PointDto(Long userId, Long savePoint, String description) { 26 | this.userId = userId; 27 | this.savePoint = savePoint; 28 | this.description = description; 29 | } 30 | 31 | public Point toEntity() { 32 | return Point.builder() 33 | .userId(userId) 34 | .point(savePoint) 35 | .description(description) 36 | .build(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/java/com/github/jojoldu/sqs/AwsMockSqsAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs; 2 | 3 | import com.github.jojoldu.sqs.annotation.server.ConditionalOnMockSqs; 4 | import com.github.jojoldu.sqs.config.SqsProperties; 5 | import com.github.jojoldu.sqs.config.SqsQueues; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.boot.context.properties.ConfigurationProperties; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | 11 | /** 12 | * Created by jojoldu@gmail.com on 2018. 3. 17. 13 | * Blog : http://jojoldu.tistory.com 14 | * Github : https://github.com/jojoldu 15 | */ 16 | 17 | @Slf4j 18 | @Configuration 19 | @ConditionalOnMockSqs 20 | public class AwsMockSqsAutoConfiguration { 21 | 22 | @Bean 23 | @ConfigurationProperties("sqs") 24 | public SqsQueues sqsQueues() { 25 | return new SqsQueues(); 26 | } 27 | 28 | @Bean 29 | @ConfigurationProperties("sqs.mock") 30 | public SqsProperties sqsProperties() { 31 | return new SqsProperties(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/java/com/github/jojoldu/sqs/annotation/server/OnMissingMockSqsServerCondition.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs.annotation.server; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.boot.autoconfigure.condition.ConditionOutcome; 5 | import org.springframework.context.annotation.ConditionContext; 6 | import org.springframework.core.Ordered; 7 | import org.springframework.core.annotation.Order; 8 | import org.springframework.core.type.AnnotatedTypeMetadata; 9 | 10 | /** 11 | * Created by jojoldu@gmail.com on 2018. 3. 17. 12 | * Blog : http://jojoldu.tistory.com 13 | * Github : https://github.com/jojoldu 14 | */ 15 | 16 | /** 17 | * 이미 Mock SQS 서버가 실행중인 경우 18 | */ 19 | @Slf4j 20 | @Order(Ordered.HIGHEST_PRECEDENCE + 40) 21 | class OnMissingMockSqsServerCondition extends OnMockSqsServerBaseCondition { 22 | 23 | @Override 24 | public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { 25 | boolean isRunning = isRunning(context); 26 | return new ConditionOutcome(isRunning, createMessage(isRunning)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 JitPack.io 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample/src/main/java/com/github/jojoldu/sample/controller/Sample3Controller.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sample.controller; 2 | 3 | import com.github.jojoldu.sample.dto.PointDto; 4 | import lombok.AllArgsConstructor; 5 | import lombok.RequiredArgsConstructor; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate; 8 | import org.springframework.web.bind.annotation.PostMapping; 9 | import org.springframework.web.bind.annotation.RequestBody; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | /** 13 | * Created by jojoldu@gmail.com on 2018. 3. 15. 14 | * Blog : http://jojoldu.tistory.com 15 | * Github : https://github.com/jojoldu 16 | */ 17 | 18 | @Slf4j 19 | @RequiredArgsConstructor 20 | @RestController 21 | public class Sample3Controller { 22 | private final QueueMessagingTemplate messagingTemplate; 23 | 24 | @PostMapping("/sample3") 25 | public String save(@RequestBody PointDto requestDto){ 26 | messagingTemplate.convertAndSend("sample3", requestDto); 27 | return "success"; 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json: -------------------------------------------------------------------------------- 1 | { 2 | "groups": [{ 3 | "name": "sqs.mock", 4 | "type": "com.github.jojoldu.sqs.config.SqsProperties", 5 | "sourceType": "com.github.jojoldu.sqs.config.SqsProperties" 6 | }], 7 | "properties": [ 8 | { 9 | "name": "sqs.mock.enabled", 10 | "type": "java.lang.Boolean", 11 | "description": "true or false.", 12 | "defaultValue": false 13 | }, 14 | { 15 | "name": "sqs.mock.randomPortEnabled", 16 | "type": "java.lang.Boolean", 17 | "description": "true or false.", 18 | "defaultValue": false 19 | }, 20 | { 21 | "name": "sqs.mock.host", 22 | "type": "java.lang.String", 23 | "description": "Your Mock Sqs Server Host.", 24 | "defaultValue": "localhost" 25 | }, 26 | { 27 | "name": "sqs.mock.port", 28 | "type": "java.lang.Integer", 29 | "description": "Your Mock Sqs Server Port.", 30 | "defaultValue": 9324 31 | }, 32 | { 33 | "name": "sqs.mock.queues", 34 | "type": "java.util.List" 35 | } 36 | ] 37 | } -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/test/java/com/github/jojoldu/sqs/annotation/server/OnMissingMockSqsServerConditionTest.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs.annotation.server; 2 | 3 | import com.github.jojoldu.sqs.config.SqsProperties; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | import static org.assertj.core.api.Assertions.assertThat; 8 | 9 | /** 10 | * Created by jojoldu@gmail.com on 2018. 5. 4. 11 | * Blog : http://jojoldu.tistory.com 12 | * Github : https://github.com/jojoldu 13 | */ 14 | 15 | public class OnMissingMockSqsServerConditionTest { 16 | 17 | OnMissingMockSqsServerCondition condition; 18 | 19 | @Before 20 | public void setUp() throws Exception { 21 | condition = new OnMissingMockSqsServerCondition(); 22 | } 23 | 24 | @Test 25 | public void 윈도우OS가_아닌것을_확인할수있다() { 26 | boolean isWindow = condition.isWindowsOS(); 27 | assertThat(isWindow).isEqualTo(false); 28 | } 29 | 30 | @Test 31 | public void 현재_실행중인_프로세스가있는지_확인할수있다() { 32 | String port = String.valueOf(SqsProperties.DEFAULT_PORT); 33 | boolean isRunning = condition.isRunning(port); 34 | assertThat(isRunning).isEqualTo(false); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/java/com/github/jojoldu/sqs/annotation/server/OnMockSqsCondition.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs.annotation.server; 2 | 3 | import org.springframework.boot.autoconfigure.condition.ConditionOutcome; 4 | import org.springframework.boot.autoconfigure.condition.SpringBootCondition; 5 | import org.springframework.context.annotation.ConditionContext; 6 | import org.springframework.core.Ordered; 7 | import org.springframework.core.annotation.Order; 8 | import org.springframework.core.type.AnnotatedTypeMetadata; 9 | 10 | /** 11 | * Created by jojoldu@gmail.com on 2018. 3. 17. 12 | * Blog : http://jojoldu.tistory.com 13 | * Github : https://github.com/jojoldu 14 | */ 15 | 16 | @Order(Ordered.HIGHEST_PRECEDENCE + 40) 17 | class OnMockSqsCondition extends SpringBootCondition { 18 | 19 | private static final String MOCK_ENABLED = "sqs.mock.enabled"; 20 | 21 | @Override 22 | public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { 23 | String mockEnabled = context.getEnvironment().getProperty(MOCK_ENABLED); 24 | boolean match = "true".equals(mockEnabled); 25 | return new ConditionOutcome(match, createMessage(match)); 26 | } 27 | 28 | private String createMessage(boolean match){ 29 | return match? "Execute Mock Sqs" : "Execute AWS SQS"; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample2/src/main/java/com/github/jojoldu/sample2/SampleListener.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sample2; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.cloud.aws.messaging.listener.Acknowledgment; 7 | import org.springframework.cloud.aws.messaging.listener.SqsMessageDeletionPolicy; 8 | import org.springframework.cloud.aws.messaging.listener.annotation.SqsListener; 9 | import org.springframework.messaging.handler.annotation.Header; 10 | import org.springframework.stereotype.Component; 11 | 12 | import java.io.IOException; 13 | import java.util.concurrent.CountDownLatch; 14 | import java.util.concurrent.ExecutionException; 15 | 16 | /** 17 | * Created by jojoldu@gmail.com on 2018. 3. 23. 18 | * Blog : http://jojoldu.tistory.com 19 | * Github : https://github.com/jojoldu 20 | */ 21 | @Slf4j 22 | @Component 23 | public class SampleListener { 24 | 25 | @Getter 26 | @Setter 27 | private CountDownLatch countDownLatch = new CountDownLatch(1); 28 | 29 | @SqsListener(value = "sample", deletionPolicy = SqsMessageDeletionPolicy.NEVER) 30 | public void receive(String message, @Header("SenderId") String senderId, Acknowledgment ack) throws IOException, ExecutionException, InterruptedException { 31 | log.info("senderId: {}, message: {}", senderId, message); 32 | ack.acknowledge(); 33 | countDownLatch.countDown(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample2/src/test/java/com/github/jojoldu/sample2/Sample2Test.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sample2; 2 | 3 | import org.junit.After; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate; 9 | import org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer; 10 | import org.springframework.test.context.junit4.SpringRunner; 11 | 12 | import java.time.LocalDate; 13 | import java.util.concurrent.CountDownLatch; 14 | import java.util.concurrent.TimeUnit; 15 | 16 | import static org.junit.Assert.assertTrue; 17 | 18 | /** 19 | * Created by jojoldu@gmail.com on 2018. 3. 23. 20 | * Blog : http://jojoldu.tistory.com 21 | * Github : https://github.com/jojoldu 22 | */ 23 | 24 | @RunWith(SpringRunner.class) 25 | @SpringBootTest 26 | public class Sample2Test { 27 | 28 | @Autowired 29 | private SampleListener listener; 30 | 31 | @Autowired 32 | private QueueMessagingTemplate messagingTemplate; 33 | 34 | @Test 35 | public void default_listener_test() throws Exception { 36 | //given 37 | this.listener.setCountDownLatch(new CountDownLatch(1)); 38 | 39 | //when 40 | messagingTemplate.convertAndSend("sample", "sample: "+ LocalDate.now().toString()); 41 | 42 | //then 43 | assertTrue(this.listener.getCountDownLatch().await(15, TimeUnit.SECONDS)); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/java/com/github/jojoldu/sqs/MockQueueGenerator.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs; 2 | 3 | import com.amazonaws.auth.AWSStaticCredentialsProvider; 4 | import com.amazonaws.auth.AnonymousAWSCredentials; 5 | import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration; 6 | import com.amazonaws.services.sqs.AmazonSQSAsync; 7 | import com.amazonaws.services.sqs.AmazonSQSAsyncClientBuilder; 8 | import com.github.jojoldu.sqs.annotation.server.MockServerMessageType; 9 | import com.github.jojoldu.sqs.config.SqsQueues; 10 | import lombok.RequiredArgsConstructor; 11 | import lombok.extern.slf4j.Slf4j; 12 | 13 | /** 14 | * Created by jojoldu@gmail.com on 24/11/2019 15 | * Blog : http://jojoldu.tistory.com 16 | * Github : http://github.com/jojoldu 17 | */ 18 | @Slf4j 19 | @RequiredArgsConstructor 20 | public class MockQueueGenerator { 21 | 22 | public static void generate(boolean isMockServerEnabled, String sqsEndPoint, SqsQueues queues) { 23 | AmazonSQSAsync sqsClient = getSqsClient(sqsEndPoint); 24 | 25 | if(isMockServerEnabled) { 26 | log.info(MockServerMessageType.CREATE_SERVER.getMessage()); 27 | queues.getQueues().forEach(queue -> sqsClient.createQueue(queue.createQueueRequest())); 28 | } else { 29 | log.info(MockServerMessageType.USE_SERVER.getMessage()); 30 | } 31 | 32 | } 33 | 34 | public static AmazonSQSAsync getSqsClient(String sqsEndPoint) { 35 | return AmazonSQSAsyncClientBuilder.standard() 36 | .withCredentials(new AWSStaticCredentialsProvider(new AnonymousAWSCredentials())) 37 | .withEndpointConfiguration(new EndpointConfiguration(sqsEndPoint, "")) 38 | .build(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/java/com/github/jojoldu/sqs/config/SqsProperties.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs.config; 2 | 3 | import com.github.jojoldu.sqs.annotation.utils.RandomPortFinder; 4 | import lombok.Getter; 5 | import lombok.NoArgsConstructor; 6 | import lombok.Setter; 7 | import lombok.extern.slf4j.Slf4j; 8 | import org.springframework.util.Assert; 9 | import org.springframework.util.StringUtils; 10 | 11 | import static com.github.jojoldu.sqs.annotation.utils.RandomPortFinder.findAvailablePort; 12 | 13 | /** 14 | * Created by jojoldu@gmail.com on 2018. 3. 15. 15 | * Blog : http://jojoldu.tistory.com 16 | * Github : https://github.com/jojoldu 17 | */ 18 | 19 | @Slf4j 20 | @Getter 21 | @Setter 22 | @NoArgsConstructor 23 | public class SqsProperties { 24 | 25 | public static final String DEFAULT_HOST = "localhost"; 26 | public static final Integer DEFAULT_PORT = 9324; 27 | 28 | private String host = DEFAULT_HOST; 29 | private Integer port = DEFAULT_PORT; 30 | private Boolean enabled = false; 31 | private Boolean randomPortEnabled = false; 32 | 33 | public String getEndPoint() { 34 | return String.format("http://%s:%s", getHost(), getPort()); 35 | } 36 | /** 37 | * properties에 값이 없을 경우 setter를 호출하지 않음 38 | */ 39 | public void setRandomPortEnabled(Boolean randomPortEnabled) { 40 | this.randomPortEnabled = randomPortEnabled; 41 | this.setRandomPort(); 42 | } 43 | 44 | private void setRandomPort() { 45 | if(isRandomPortEnabled()) { 46 | this.port = findAvailablePort(); 47 | } 48 | } 49 | 50 | public boolean isRandomPortEnabled(){ 51 | return getRandomPortEnabled(); 52 | } 53 | public boolean isEnabled() { 54 | return getEnabled(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample/src/main/java/com/github/jojoldu/sample/listener/Sample2Listener.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sample.listener; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.github.jojoldu.sample.domain.PointRepository; 5 | import com.github.jojoldu.sample.dto.PointDto; 6 | import lombok.Getter; 7 | import lombok.RequiredArgsConstructor; 8 | import lombok.Setter; 9 | import lombok.extern.slf4j.Slf4j; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.cloud.aws.messaging.listener.Acknowledgment; 12 | import org.springframework.cloud.aws.messaging.listener.SqsMessageDeletionPolicy; 13 | import org.springframework.cloud.aws.messaging.listener.annotation.SqsListener; 14 | import org.springframework.messaging.handler.annotation.Header; 15 | import org.springframework.stereotype.Component; 16 | 17 | import java.io.IOException; 18 | import java.util.concurrent.CountDownLatch; 19 | 20 | /** 21 | * Created by jojoldu@gmail.com on 2018. 5. 19. 22 | * Blog : http://jojoldu.tistory.com 23 | * Github : https://github.com/jojoldu 24 | */ 25 | 26 | @Slf4j 27 | @RequiredArgsConstructor 28 | @Component 29 | public class Sample2Listener { 30 | 31 | private final PointRepository pointRepository; 32 | private final ObjectMapper objectMapper; 33 | 34 | @Getter 35 | @Setter 36 | private CountDownLatch countDownLatch = new CountDownLatch(1); 37 | 38 | @SqsListener(value = "sample2", deletionPolicy = SqsMessageDeletionPolicy.NEVER) 39 | public void receive(String message, @Header("SenderId") String senderId, Acknowledgment ack) throws IOException { 40 | log.info("[sample2][Queue] senderId: {}, message: {}", senderId, message); 41 | PointDto messageObject = objectMapper.readValue(message, PointDto.class); 42 | pointRepository.save(messageObject.toEntity()); 43 | ack.acknowledge(); 44 | countDownLatch.countDown(); 45 | log.info("[sample2] Success Ack"); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/java/com/github/jojoldu/sqs/annotation/utils/RandomPortFinder.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs.annotation.utils; 2 | 3 | import com.github.jojoldu.sqs.exception.SqsMockException; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.springframework.util.StringUtils; 6 | 7 | import java.io.BufferedReader; 8 | import java.io.IOException; 9 | import java.io.InputStreamReader; 10 | 11 | /** 12 | * Created by jojoldu@gmail.com on 2018. 5. 21. 13 | * Blog : http://jojoldu.tistory.com 14 | * Github : https://github.com/jojoldu 15 | */ 16 | 17 | @Slf4j 18 | public class RandomPortFinder { 19 | 20 | public static int findAvailablePort() { 21 | for (int i=0; i<1000; i++) { 22 | try { 23 | int port = getRandomPort(); 24 | if(!isRunning(executeGrepProcessCommand(port))){ 25 | return port; 26 | } 27 | } catch (IOException ex) { 28 | } 29 | } 30 | 31 | String message = "Not Found Available port: 10000 ~ 65535"; 32 | log.error(message); 33 | throw new SqsMockException(message); 34 | } 35 | 36 | public static int getRandomPort() { 37 | return (int) (Math.random() * 50000) + 10000; 38 | } 39 | 40 | private static boolean isRunning(Process p){ 41 | String line; 42 | StringBuilder pidInfo = new StringBuilder(); 43 | 44 | try (BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()))) { 45 | 46 | while ((line = input.readLine()) != null) { 47 | pidInfo.append(line); 48 | } 49 | 50 | } catch (Exception e) { 51 | } 52 | 53 | return !StringUtils.isEmpty(pidInfo.toString()); 54 | } 55 | 56 | private static Process executeGrepProcessCommand(int port) throws IOException { 57 | String command = String.format("netstat -nat | grep LISTEN|grep %d", port); 58 | String[] shell = {"/bin/sh", "-c", command}; 59 | return Runtime.getRuntime().exec(shell); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/java/com/github/jojoldu/sqs/AwsMockSqsServerAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs; 2 | 3 | import com.amazonaws.services.sqs.AmazonSQSAsync; 4 | import com.github.jojoldu.sqs.annotation.server.ConditionalOnMockSqs; 5 | import com.github.jojoldu.sqs.annotation.server.ConditionalOnMockSqsServer; 6 | import com.github.jojoldu.sqs.config.SqsProperties; 7 | import com.github.jojoldu.sqs.config.SqsQueues; 8 | import lombok.RequiredArgsConstructor; 9 | import lombok.extern.slf4j.Slf4j; 10 | import org.elasticmq.rest.sqs.SQSRestServer; 11 | import org.elasticmq.rest.sqs.SQSRestServerBuilder; 12 | import org.springframework.context.annotation.Bean; 13 | import org.springframework.context.annotation.Configuration; 14 | import org.springframework.context.annotation.DependsOn; 15 | import org.springframework.context.annotation.Primary; 16 | 17 | import static com.github.jojoldu.sqs.MockQueueGenerator.generate; 18 | import static com.github.jojoldu.sqs.MockQueueGenerator.getSqsClient; 19 | 20 | /** 21 | * Created by jojoldu@gmail.com on 2018. 3. 17. 22 | * Blog : http://jojoldu.tistory.com 23 | * Github : https://github.com/jojoldu 24 | */ 25 | 26 | @Slf4j 27 | @RequiredArgsConstructor 28 | @Configuration 29 | @ConditionalOnMockSqs 30 | public class AwsMockSqsServerAutoConfiguration { 31 | private final SqsProperties sqsProperties; 32 | private final SqsQueues sqsQueues; 33 | 34 | 35 | @Bean 36 | @Primary 37 | @DependsOn("sqsRestServer") 38 | @ConditionalOnMockSqsServer 39 | public AmazonSQSAsync amazonSqs() { 40 | return getSqsClient(sqsProperties.getEndPoint()); 41 | } 42 | 43 | @Bean(destroyMethod="stopAndWait") 44 | @Primary 45 | @ConditionalOnMockSqsServer 46 | public SQSRestServer sqsRestServer() { 47 | SQSRestServer sqsServer = createMockSqsServer(); 48 | 49 | generate(sqsProperties.isEnabled(), sqsProperties.getEndPoint(), sqsQueues); 50 | 51 | return sqsServer; 52 | } 53 | 54 | private SQSRestServer createMockSqsServer() { 55 | return SQSRestServerBuilder 56 | .withInterface(sqsProperties.getHost()) 57 | .withPort(sqsProperties.getPort()) 58 | .start(); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample/src/main/java/com/github/jojoldu/sample/listener/Sample3Listener.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sample.listener; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.github.jojoldu.sample.domain.PointRepository; 5 | import com.github.jojoldu.sample.dto.PointDto; 6 | import lombok.Getter; 7 | import lombok.RequiredArgsConstructor; 8 | import lombok.Setter; 9 | import lombok.extern.slf4j.Slf4j; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.cloud.aws.messaging.listener.Acknowledgment; 12 | import org.springframework.cloud.aws.messaging.listener.SqsMessageDeletionPolicy; 13 | import org.springframework.cloud.aws.messaging.listener.annotation.SqsListener; 14 | import org.springframework.messaging.handler.annotation.Header; 15 | import org.springframework.stereotype.Component; 16 | 17 | import java.io.IOException; 18 | import java.util.concurrent.CountDownLatch; 19 | 20 | /** 21 | * Created by jojoldu@gmail.com on 2018. 5. 19. 22 | * Blog : http://jojoldu.tistory.com 23 | * Github : https://github.com/jojoldu 24 | */ 25 | 26 | @Slf4j 27 | @RequiredArgsConstructor 28 | @Component 29 | public class Sample3Listener { 30 | 31 | private final PointRepository pointRepository; 32 | private final ObjectMapper objectMapper; 33 | 34 | @Getter 35 | @Setter 36 | private CountDownLatch countDownLatch = new CountDownLatch(1); 37 | 38 | @SqsListener(value = "sample3", deletionPolicy = SqsMessageDeletionPolicy.NEVER) 39 | public void receive(String message, @Header("SenderId") String senderId, Acknowledgment ack) throws IOException { 40 | log.info("[sample3][Queue] senderId: {}, message: {}", senderId, message); 41 | PointDto messageObject = objectMapper.readValue(message, PointDto.class); 42 | try{ 43 | pointRepository.save(messageObject.toEntity()); 44 | ack.acknowledge(); 45 | countDownLatch.countDown(); 46 | log.info("[sample3] Success Ack"); 47 | } catch (Exception e){ 48 | log.error("[sample3] Point Save Fail: "+ message, e); 49 | } 50 | } 51 | 52 | @SqsListener(value = "sample3-dlq") 53 | public void receiveDlq(String message) throws IOException { 54 | log.info("[sample3][DLQ] message: {}", message); 55 | countDownLatch.countDown(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample/src/test/java/com/github/jojoldu/sample/controller/Sample3ControllerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sample.controller; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.github.jojoldu.sample.domain.PointRepository; 5 | import com.github.jojoldu.sample.dto.PointDto; 6 | import com.github.jojoldu.sample.listener.Sample3Listener; 7 | import org.junit.After; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.boot.test.mock.mockito.MockBean; 13 | import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate; 14 | import org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer; 15 | import org.springframework.test.context.junit4.SpringRunner; 16 | 17 | import java.util.concurrent.CountDownLatch; 18 | import java.util.concurrent.TimeUnit; 19 | 20 | import static org.junit.Assert.assertTrue; 21 | import static org.mockito.ArgumentMatchers.any; 22 | import static org.mockito.BDDMockito.given; 23 | 24 | /** 25 | * Created by jojoldu@gmail.com on 2018. 5. 19. 26 | * Blog : http://jojoldu.tistory.com 27 | * Github : https://github.com/jojoldu 28 | */ 29 | 30 | @RunWith(SpringRunner.class) 31 | @SpringBootTest 32 | public class Sample3ControllerTest{ 33 | 34 | @MockBean 35 | PointRepository pointRepository; 36 | 37 | @Autowired 38 | ObjectMapper objectMapper; 39 | 40 | @Autowired 41 | Sample3Listener pointListener; 42 | 43 | @Autowired 44 | private QueueMessagingTemplate messagingTemplate; 45 | 46 | @Test 47 | public void Ack_fails_Go_to_the_dlq() throws Exception { 48 | // given: 49 | PointDto requestDto = PointDto.builder() 50 | .userId(1L) 51 | .savePoint(1000L) 52 | .description("buy laptop") 53 | .build(); 54 | 55 | given(pointRepository.save(any())) 56 | .willThrow(new IllegalArgumentException("fail")); 57 | 58 | pointListener.setCountDownLatch(new CountDownLatch(1)); 59 | 60 | // when: 61 | messagingTemplate.convertAndSend("sample3", requestDto); 62 | 63 | // then: 64 | assertTrue(this.pointListener.getCountDownLatch().await(15, TimeUnit.SECONDS)); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/java/com/github/jojoldu/sqs/config/SqsQueues.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs.config; 2 | 3 | import com.amazonaws.services.sqs.model.CreateQueueRequest; 4 | import com.fasterxml.jackson.core.JsonProcessingException; 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | import lombok.Getter; 7 | import lombok.NoArgsConstructor; 8 | import lombok.Setter; 9 | 10 | import java.util.LinkedHashMap; 11 | import java.util.List; 12 | import java.util.Map; 13 | 14 | /** 15 | * Created by jojoldu@gmail.com on 2018. 3. 17. 16 | * Blog : http://jojoldu.tistory.com 17 | * Github : https://github.com/jojoldu 18 | */ 19 | 20 | @Getter 21 | @Setter 22 | @NoArgsConstructor 23 | public class SqsQueues { 24 | private List queues; 25 | 26 | @Getter 27 | @Setter 28 | @NoArgsConstructor 29 | public static class SqsQueue { 30 | 31 | private String name; 32 | private Long defaultVisibilityTimeout = 3L; 33 | private Long delay = 0L; 34 | private Long receiveMessageWait = 0L; 35 | private DeadLetterQueue deadLettersQueue = null; 36 | 37 | public CreateQueueRequest createQueueRequest(){ 38 | return new CreateQueueRequest(name) 39 | .withAttributes(getAttributes()); 40 | } 41 | 42 | public Map getAttributes(){ 43 | Map attributes = new LinkedHashMap<>(); 44 | attributes.put("VisibilityTimeout", String.valueOf(defaultVisibilityTimeout)); 45 | attributes.put("DelaySeconds", String.valueOf(delay)); 46 | attributes.put("ReceiveMessageWaitTimeSeconds", String.valueOf(receiveMessageWait)); 47 | if(deadLettersQueue != null){ 48 | attributes.put("RedrivePolicy", deadLettersQueue.toJson()); 49 | } 50 | 51 | return attributes; 52 | } 53 | } 54 | 55 | @Getter 56 | @Setter 57 | @NoArgsConstructor 58 | public static class DeadLetterQueue { 59 | private String name; 60 | private String maxReceiveCount; 61 | private String deadLetterTargetArn; 62 | 63 | public String toJson(){ 64 | this.deadLetterTargetArn = "arn:aws:sqs:elasticmq:000000000000:"+name; 65 | try { 66 | return new ObjectMapper().writeValueAsString(this); 67 | } catch (JsonProcessingException e) { 68 | throw new IllegalArgumentException("JsonParseException", e); 69 | } 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /spring-boot-starter-mock-sample/src/test/java/com/github/jojoldu/sample/controller/Sample2ControllerTest.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sample.controller; 2 | 3 | import com.github.jojoldu.sample.domain.Point; 4 | import com.github.jojoldu.sample.domain.PointRepository; 5 | import com.github.jojoldu.sample.dto.PointDto; 6 | import com.github.jojoldu.sample.listener.Sample2Listener; 7 | import org.junit.After; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate; 13 | import org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer; 14 | import org.springframework.test.context.junit4.SpringRunner; 15 | 16 | import java.util.List; 17 | import java.util.concurrent.CountDownLatch; 18 | import java.util.concurrent.TimeUnit; 19 | 20 | import static org.assertj.core.api.Assertions.assertThat; 21 | import static org.junit.Assert.assertTrue; 22 | 23 | /** 24 | * Created by jojoldu@gmail.com on 2018. 5. 19. 25 | * Blog : http://jojoldu.tistory.com 26 | * Github : https://github.com/jojoldu 27 | */ 28 | 29 | @RunWith(SpringRunner.class) 30 | @SpringBootTest 31 | public class Sample2ControllerTest { 32 | 33 | @Autowired 34 | Sample2Listener sample2Listener; 35 | 36 | @Autowired 37 | PointRepository pointRepository; 38 | 39 | @Autowired 40 | private QueueMessagingTemplate messagingTemplate; 41 | 42 | @After 43 | public void tearDown() throws Exception { 44 | pointRepository.deleteAllInBatch(); 45 | } 46 | 47 | @Test 48 | public void Earn_points_through_the_queue() throws Exception { 49 | // given 50 | PointDto requestDto = PointDto.builder() 51 | .userId(10L) 52 | .savePoint(1000L) 53 | .description("buy laptop") 54 | .build(); 55 | 56 | sample2Listener.setCountDownLatch(new CountDownLatch(1)); 57 | 58 | // when 59 | messagingTemplate.convertAndSend("sample2", requestDto); 60 | 61 | // then 62 | assertTrue(this.sample2Listener.getCountDownLatch().await(15, TimeUnit.SECONDS)); 63 | List points = pointRepository.findAll(); 64 | assertThat(points.isEmpty()).isEqualTo(false); 65 | assertThat(points.get(0).getPoint()).isEqualTo(1000L); 66 | } 67 | 68 | @Test 69 | public void Earn_points_through_the_queue2() throws Exception { 70 | // given 71 | PointDto requestDto = PointDto.builder() 72 | .userId(1L) 73 | .savePoint(2000L) 74 | .description("buy laptop") 75 | .build(); 76 | 77 | sample2Listener.setCountDownLatch(new CountDownLatch(1)); 78 | 79 | // when 80 | messagingTemplate.convertAndSend("sample2", requestDto); 81 | 82 | // then 83 | assertTrue(this.sample2Listener.getCountDownLatch().await(15, TimeUnit.SECONDS)); 84 | List points = pointRepository.findAll(); 85 | assertThat(points.isEmpty()).isEqualTo(false); 86 | assertThat(points.get(0).getId()).isEqualTo(1L); 87 | assertThat(points.get(0).getPoint()).isEqualTo(2000L); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /mock-sqs-spring-boot-starter/src/main/java/com/github/jojoldu/sqs/annotation/server/OnMockSqsServerBaseCondition.java: -------------------------------------------------------------------------------- 1 | package com.github.jojoldu.sqs.annotation.server; 2 | 3 | import com.github.jojoldu.sqs.exception.SqsMockException; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.springframework.boot.autoconfigure.condition.SpringBootCondition; 6 | import org.springframework.context.annotation.ConditionContext; 7 | import org.springframework.core.env.Environment; 8 | import org.springframework.util.StringUtils; 9 | 10 | import java.io.BufferedReader; 11 | import java.io.IOException; 12 | import java.io.InputStreamReader; 13 | 14 | import static com.github.jojoldu.sqs.annotation.server.MockServerConstant.SQS_SERVER_PORT; 15 | import static com.github.jojoldu.sqs.annotation.server.MockServerConstant.SQS_SERVER_RANDOM_PORT_ENABLED; 16 | 17 | /** 18 | * Created by jojoldu@gmail.com on 2018. 5. 4. 19 | * Blog : http://jojoldu.tistory.com 20 | * Github : https://github.com/jojoldu 21 | */ 22 | 23 | @Slf4j 24 | public abstract class OnMockSqsServerBaseCondition extends SpringBootCondition { 25 | 26 | boolean isRunning(ConditionContext context){ 27 | Environment environment = context.getEnvironment(); 28 | if(isRandomPort(environment)){ 29 | return false; 30 | } 31 | 32 | return isRunning(getSqsServerPort(environment)); 33 | } 34 | 35 | boolean isRandomPort(Environment environment){ 36 | return "true".equals(environment.getProperty(SQS_SERVER_RANDOM_PORT_ENABLED)); 37 | } 38 | 39 | String getSqsServerPort(Environment environment) { 40 | return environment.getProperty(SQS_SERVER_PORT); 41 | } 42 | 43 | boolean isRunning(String port) { 44 | verifyOS(); // check OS 45 | 46 | String line; 47 | StringBuilder pidInfo = new StringBuilder(); 48 | Process p = executeGrepProcessCommand(port); 49 | 50 | try (BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()))) { 51 | 52 | while ((line = input.readLine()) != null) { 53 | pidInfo.append(line); 54 | } 55 | 56 | } catch (Exception e) { 57 | String message = "isRunning Check Exception"; 58 | log.error(message , e); 59 | throw new SqsMockException(message, e); 60 | } 61 | 62 | return !StringUtils.isEmpty(pidInfo.toString()); 63 | } 64 | 65 | private Process executeGrepProcessCommand(String port) { 66 | String command = String.format("netstat -nat | grep LISTEN|grep %s", port); 67 | String[] shell = {"/bin/sh", "-c", command}; 68 | try { 69 | return Runtime.getRuntime().exec(shell); 70 | } catch (IOException e) { 71 | String message = String.format("Execute Command Fail. command: %s",command); 72 | log.error(message , e); 73 | throw new SqsMockException(message, e); 74 | } 75 | } 76 | 77 | private void verifyOS() { 78 | if(isWindowsOS()){ 79 | String message = "Not available on Windows OS"; 80 | log.error(message); 81 | throw new SqsMockException(message); 82 | } 83 | } 84 | 85 | boolean isWindowsOS(){ 86 | String os = System.getProperty("os.name"); 87 | return os.contains("Windows"); 88 | } 89 | 90 | String createMessage(boolean isRunning) { 91 | MockServerMessageType messageType = isRunning ? MockServerMessageType.USE_SERVER : MockServerMessageType.CREATE_SERVER; 92 | return messageType.getMessage(); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # spring-boot-aws-mock 2 | 3 | [![Build Status](https://travis-ci.org/jojoldu/spring-boot-aws-mock.svg?branch=master)](https://travis-ci.org/jojoldu/spring-boot-aws-mock) [![Coverage Status](https://coveralls.io/repos/github/jojoldu/spring-boot-aws-mock/badge.svg?branch=master)](https://coveralls.io/github/jojoldu/spring-boot-aws-mock?branch=master) [![Release](https://jitpack.io/v/jojoldu/spring-boot-aws-mock.svg)](https://jitpack.io/#jojoldu/spring-boot-aws-mock) 4 | 5 | Spring Boot Starter support for Amazon Web Service Mocking. 6 | 7 | ## Requirements 8 | 9 | **Windows OS is not supported** 10 | 11 | ## Mock Modules 12 | 13 | * SQS 14 | * Amazon Simple Queue Service 15 | 16 | ## Install 17 | 18 | ### Mock SQS 19 | 20 | build.gradle 21 | 22 | ```groovy 23 | repositories { 24 | maven { url 'https://jitpack.io' } 25 | } 26 | 27 | dependencies { 28 | compile 'com.github.jojoldu.spring-boot-aws-mock:mock-sqs-spring-boot-starter:0.4.1' 29 | } 30 | ``` 31 | 32 | ## Usage 33 | 34 | ### Default 35 | 36 | application.yml 37 | 38 | ```yml 39 | cloud: 40 | aws: 41 | region: 42 | static: ap-northeast-2 //aws region code (required) 43 | stack: 44 | auto: false // default required 45 | sqs: 46 | mock: 47 | enabled: true 48 | queues: 49 | - 50 | name: 'key1' 51 | ``` 52 | 53 | Controller.java 54 | 55 | ```java 56 | public class SampleController { 57 | @Autowired private QueueMessagingTemplate messagingTemplate; 58 | 59 | @PostMapping("/url") 60 | public String save(@RequestBody RequestDto requestDto){ 61 | String queueName = "key1"; 62 | messagingTemplate.convertAndSend(queueName, requestDto); 63 | ... 64 | } 65 | 66 | @SqsListener(value = "key1") 67 | public void receive(String message, @Header("SenderId") String senderId) throws IOException { 68 | ... 69 | } 70 | } 71 | ``` 72 | 73 | Run Test & Show Log 74 | 75 | ![log](./images/log.png) 76 | 77 | ### Integration Test 78 | 79 | During integration testing, **you must specify any port** to prevent mock sqs server port conflicts. 80 | 81 | src/**test**/resources/application.yml or application.properteis 82 | 83 | ```yaml 84 | sqs: 85 | mock: 86 | randomPortEnabled: true 87 | ``` 88 | 89 | This will run the server with the **new port each time** the Spring context is executed. 90 | 91 | ### Options 92 | 93 | ```yml 94 | sqs: 95 | mock: 96 | enabled: true //required 97 | randomPortEnabled: true 98 | host: localhost 99 | port: 9324 100 | queues: 101 | - 102 | name: 'key1-dlq' 103 | - 104 | name: 'key1' 105 | defaultVisibilityTimeout: 1 106 | delay: 0 107 | receiveMessageWait: 0 108 | deadLettersQueue: 109 | name: "point-dlq" 110 | maxReceiveCount: 1 111 | ``` 112 | 113 | * ```sqs.mock.enabled``` 114 | * **false** (**default**, not use local mock sqs) 115 | * **true** (use local mock sqs) 116 | * ```sqs.mock.randomPortEnabled``` 117 | * **false** (**default**, not use local mock sqs) 118 | * **true** (use random port) 119 | * ```sqs.mock.host``` 120 | * sqs server host 121 | * default: localhost 122 | 123 | * ```sqs.mock.port``` 124 | * sqs server port 125 | * default: 9324 126 | 127 | 128 | | AWS SQS | MOCK SQS | Default Value | 129 | |-------------------------------|----------------------------------|---------------| 130 | | VisibilityTimeout | defaultVisibilityTimeout | 30 (s) | 131 | | DelaySeconds | delay | 0 (s) | 132 | | ReceiveMessageWaitTimeSeconds | receiveMessageWait | 0 (s) | 133 | | RedrivePolicy | deadLettersQueue | null | 134 | | RedrivePolicy.name | deadLettersQueue.name | null | 135 | | RedrivePolicy.maxReceiveCount | deadLettersQueue.maxReceiveCount | null | 136 | 137 | ## Example 138 | 139 | * [Basic Sample Project](https://github.com/jojoldu/spring-boot-aws-mock/tree/master/spring-boot-starter-mock-sample) 140 | * Divided Provider Application & Consumer Application 141 | * [Provider (create local sqs)](https://github.com/jojoldu/spring-boot-aws-mock/blob/master/spring-boot-starter-mock-sample/src/main/resources/application.yml) 142 | * [Consumer (use local sqs)](https://github.com/jojoldu/spring-boot-aws-mock/blob/master/spring-boot-starter-mock-sample2/src/main/resources/application.yml) 143 | 144 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | --------------------------------------------------------------------------------