Last active
April 22, 2023 16:27
-
-
Save wrschneider/42407cc2ea70799362cc5b044ebcfabb to your computer and use it in GitHub Desktop.
Spring Boot listener for Amazon SQS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bill.boottest; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.boot.CommandLineRunner; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.cloud.aws.messaging.listener.annotation.SqsListener; | |
import com.fasterxml.jackson.annotation.JsonCreator; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
@SpringBootApplication | |
public class App implements CommandLineRunner { | |
private static Logger LOG = LoggerFactory.getLogger(App.class); | |
public static void main(String[] args) { | |
SpringApplication.run(App.class, args); | |
} | |
@SqsListener("bill-poc") | |
public void listen(DataObject message) { | |
LOG.info("!!!! received message {} {}", message.getFoo(), message.getBar()); | |
} | |
@Override | |
public void run(String... args) throws Exception { | |
} | |
public static class DataObject { | |
private String foo; | |
private String bar; | |
@JsonCreator | |
public DataObject(@JsonProperty("foo") String foo, @JsonProperty("bar") String bar) { | |
this.foo = foo; | |
this.bar = bar; | |
} | |
public String getFoo() { | |
return foo; | |
} | |
public String getBar() { | |
return bar; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>bill</groupId> | |
<artifactId>boottest</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>boottest</name> | |
<url>http://maven.apache.org</url> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>2.0.5.RELEASE</version> | |
</parent> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<maven.compiler.target>1.8</maven.compiler.target> | |
<maven.compiler.source>1.8</maven.compiler.source> | |
</properties> | |
<dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.cloud</groupId> | |
<artifactId>spring-cloud-aws</artifactId> | |
<version>2.0.0.RELEASE</version> | |
<type>pom</type> | |
<scope>import</scope> | |
</dependency> | |
</dependencies> | |
</dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>3.8.1</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.cloud</groupId> | |
<artifactId>spring-cloud-starter-aws</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.cloud</groupId> | |
<artifactId>spring-cloud-aws-messaging</artifactId> | |
</dependency> | |
</dependencies> | |
</project> |
@EnableSqs
For me it's working without the annotation if I define the SimpleMessageListenerContainer and QueueMessageHandler beans. This gist is a great resource to get it up and running thanks yall!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@JoniSykes for what it's worth now - yes, this config means it's not using any of the local files, all config is configured here. I'm running
alpine-sqs
as well and have not had the problem with https. I also found a simpler way to get it working, if you don't need explicit control over thread pool here:I don't why any other tutorial doesn't mention it, but if I don't use
@EnableSqs
the@SqsListener
annotation does not work and I'm not getting any messages.