Skip to content

maksymmateiuk/spring-cloud-sqs-sender

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring cloud sqs listener

EventBridge Amazon SQS

Alt text

Step 1: Implementation of consumer messages from a queue.

  • Need to add sdk dependency:
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-aws-messaging</artifactId>
</dependency>

<dependencyManagement>
<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Hoxton.SR12</version>
    <type>pom</type>
    <scope>import</scope>
  </dependency>
</dependencies>
</dependencyManagement>
  • Then we need to implement this code snippet to received messages from Amazon SQS:
@Component
@Slf4j
public class MessageConsumer {

    private static final ObjectMapper OBJECT_MAPPER = Jackson2ObjectMapperBuilder.json().build();

    @SqsListener(value = "${orders.queue.name}", deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
    public void processMessage(String message) {
        try {
            Event event = OBJECT_MAPPER.readValue(message, Event.class);
            log.info("Received new SQS message and delete from queue: {}", message);
            log.info("Convert to Event.class: {}", event);
        } catch (Exception e) {
            throw new RuntimeException("Cannot process message from SQS", e);
        }
    }

}

Step 2: Configure Amazon SQS

Go to - AmazonSQS

Create queue

  • Click widget in right side and click Queues Alt text
  • Click on button Create queue
  • Choose name of queue
  • Everything we can keep by default for demo
  • Down below click on button Create queue

Step 3: Configure EventBridge

Go to - EventBridge

Create custom event bus

  • Click on navigation panel under Events -> Event buses.
  • Click on button Create event bus
  • Choose name of event bus
  • Create

Create custom rule

  • Click on navigation panel under Events -> Rules.
  • Click on button Create rule
  • Choose the event bus that you previously created
  • Click Next
  • Choose under Event source AWS events or EventBridge partner events
  • Add event pattern Alt text
  • Click Next
  • Target 1 - select AWS Service
  • Select target type SQS queue
    • Select your created queue
  • Click on button next
  • Create rule

Step 4: Publish event to EventBridge using amazon test environment.

Go to - EventBridge

  • Click on button Send events Alt text

  • Select your event bus from the list

  • Specify source that we created in event pattern com.demo

  • Specify any detail type(*required field)

  • Specify payload that will be in event details Alt text

  • Click on button Send

Step 5: Start spring application and GET request

  • Run application
  • Expected logs will be that we are receiving message from sqs: Alt text

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages