Skip to content

Template command

José Vieira Neto edited this page Jul 13, 2019 · 3 revisions

The template command is intended to configure libs/frameworks in an existing spring boot project very quickly and easily.

Documentation

command argument goal
spring template --list list all templates avaliable
spring template -t ${TEMPLATE_NAME} apply template in your project

Templates content

  • jms-aws-sqs

    CREATED src/main/java/com/example/cloudawsmessaging/consumer/MessageListener.java

    public interface MessageListener {
      void queueListener(String String);
    }

    CREATED src/main/java/com/example/cloudawsmessaging/consumer/EntryPointMessage.java

     public class EntryPointMessage implements MessageListener {
    
       private static final Logger log = LogManager.getLogger(EntryPointMessage.class);
    
       @Override
       @SqsListener(value = "${cloud.aws.sqs.queue-name}", deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
       public void queueListener(String String) {
    
       }
     }

    CREATED src/main/java/com/example/cloudawsmessaging/consumer/ProducerMessage.java

     @Component
     public class ProducerMessage {
    
         @Value("${cloud.aws.sqs.queue-name}")
         private String queueName;
    
         private QueueMessagingTemplate queueMessagingTemplate;
    
         @Autowired
         public ProducerMessage(AmazonSQSAsync amazonSqs) {
            this.queueMessagingTemplate = new QueueMessagingTemplate(amazonSqs);
         }
    
         public void sendMessage() {
            this.queueMessagingTemplate.convertAndSend(queueName, "YOUR_MESSAGE_OBJECT", getHeader());
         }
    
         private Map<String, Object> getHeader() {
            Map<String, Object> headers = new HashMap<>();
            headers.put(SqsMessageHeaders.SQS_GROUP_ID_HEADER, UUID.randomUUID().toString());
            headers.put(SqsMessageHeaders.SQS_DEDUPLICATION_ID_HEADER, UUID.randomUUID().toString());
            headers.put(SqsMessageHeaders.CONTENT_TYPE, "application/json");
            return headers;
         }
      }

    Add dependencies in pom.xml

     <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-aws-messaging</artifactId>
        <version>2.1.1.RELEASE</version>
     </dependency>
     <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-aws</artifactId>
        <version>2.1.1.RELEASE</version>
     </dependency>

    Add properties in application.properties

    cloud.aws.credentials.accessKey=xxxxxx
    cloud.aws.credentials.secretKey=xxxxxx
    cloud.aws.region.static=us-east-1
    cloud.aws.stack.auto=false
    cloud.aws.sqs.queue-name=my-queue.fifo
    
Clone this wiki locally