Skip to content

Commit

Permalink
Use AWS SDK v2 for SQS (#329)
Browse files Browse the repository at this point in the history
* Switch AWS-SDK-V1 for AWS-SDK-V2

* update SQS docs to remove unnecessary config
  • Loading branch information
charlie-harvey authored Mar 2, 2023
1 parent 5ce9ba2 commit f5bb8bc
Show file tree
Hide file tree
Showing 17 changed files with 463 additions and 227 deletions.
13 changes: 8 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ micronaut = "4.0.0-SNAPSHOT"
micronaut-test = "4.0.0-SNAPSHOT"

activemq = '5.17.3'
amazon-sqs-messaging = '1.1.2'
amazon-sqs-messaging = '2.0.3'
artemis-client = '2.28.0'
awaitility = '4.2.0'
aws-sqs = '1.12.366'
aws-sqs = '2.19.26'
commons-pool2 = '2.11.1'
graal-svm = "22.0.0.2"
groovy = "4.0.6"
Expand All @@ -19,8 +19,9 @@ kotlin = '1.7.22'
spock = "2.3-groovy-4.0"
spotbugs = "4.7.1"
testcontainers = '1.17.6'
lombok = '1.18.26'

micronaut-aws-v1 = '3.9.3'
micronaut-aws-v2 = '3.13.1'
micronaut-gradle-plugin = "3.6.6"

[libraries]
Expand All @@ -30,18 +31,20 @@ activemq-client = { module = 'org.apache.activemq:activemq-client', version.ref
amazon-sqs-messaging = { module = 'com.amazonaws:amazon-sqs-java-messaging-lib', version.ref = 'amazon-sqs-messaging' }
artemis-client = { module = 'org.apache.activemq:artemis-jms-client', version.ref = 'artemis-client' }
awaitility = { module = 'org.awaitility:awaitility', version.ref = 'awaitility' }
aws-sqs = { module = 'com.amazonaws:aws-java-sdk-sqs', version.ref = 'aws-sqs' }
aws-sqs = { module = 'software.amazon.awssdk:sqs', version.ref = 'aws-sqs' }
commons-pool2 = { module = 'org.apache.commons:commons-pool2', version.ref = 'commons-pool2' }
graal-svm = { module = "org.graalvm.nativeimage:svm", version.ref = "graal-svm" }
jcl-slf4j = { module = 'org.slf4j:jcl-over-slf4j', version.ref = 'jcl-slf4j' }
jms-api = { module = 'javax.jms:javax.jms-api', version.ref = 'jms-api' }
jsr305 = {group = "com.google.code.findbugs", name = "jsr305", version.ref = "jsr305" }
micronaut-aws-v1 = { module = 'io.micronaut.aws:micronaut-aws-sdk-v1', version.ref = 'micronaut-aws-v1' }
micronaut-aws-v2 = { module = 'io.micronaut.aws:micronaut-aws-sdk-v2', version.ref = 'micronaut-aws-v2' }
spotbugs = { module = "com.github.spotbugs:spotbugs-annotations", version.ref = "spotbugs" }
testcontainers-localstack = { module = 'org.testcontainers:localstack', version.ref = 'testcontainers' }
testcontainers-spock = { module = 'org.testcontainers:spock', version.ref = 'testcontainers' }
javax-json-api = { module = 'javax.json:javax.json-api', version.ref = 'json-api' }

lombok = { module = 'org.projectlombok:lombok', version.ref = 'lombok' }

#plugins

micronaut-gradle-plugin = { module = 'io.micronaut.gradle:micronaut-gradle-plugin', version.ref = 'micronaut-gradle-plugin' }
Expand Down
2 changes: 1 addition & 1 deletion jms-sqs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ dependencies {
api projects.jmsCore
api libs.amazon.sqs.messaging
api libs.aws.sqs
api libs.micronaut.aws.v1
api libs.micronaut.aws.v2
compileOnly libs.graal.svm
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@

import com.amazon.sqs.javamessaging.ProviderConfiguration;
import com.amazon.sqs.javamessaging.SQSConnectionFactory;
import com.amazonaws.services.sqs.AmazonSQS;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;
import io.micronaut.jms.annotations.JMSConnectionFactory;
import io.micronaut.jms.sqs.configuration.properties.SqsConfigurationProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.SqsClientBuilder;

import javax.jms.ConnectionFactory;

Expand Down Expand Up @@ -54,17 +55,37 @@ public class SqsConfiguration {
* configured with properties from {@link SqsConfigurationProperties}.
*
* @param config config settings for SQS
* @param sqs a configured AmazonSQS instance, typically built with
* {@link com.amazonaws.services.sqs.AmazonSQSClientBuilder}.
* @param sqs a configured SqsClient instance, typically built with
* {@link software.amazon.awssdk.services.sqs.SqsClientBuilder}.
* @return the {@link SQSConnectionFactory} defined by the {@code config}.
*/
@JMSConnectionFactory(CONNECTION_FACTORY_BEAN_NAME)
public ConnectionFactory sqsJmsConnectionFactory(SqsConfigurationProperties config,
AmazonSQS sqs) {
SqsClient sqs) {
logger.debug("created ConnectionFactory bean '{}' (SQSConnectionFactory)",
CONNECTION_FACTORY_BEAN_NAME);
CONNECTION_FACTORY_BEAN_NAME);
return new SQSConnectionFactory(
new ProviderConfiguration().withNumberOfMessagesToPrefetch(config.getNumberOfMessagesToPrefetch()),
sqs);
new ProviderConfiguration().withNumberOfMessagesToPrefetch(config.getNumberOfMessagesToPrefetch()),
sqs);
}

/**
* Generates a {@link JMSConnectionFactory} bean in the application context.
* <p>
* The bean is a {@link SQSConnectionFactory}
* configured with properties from {@link SqsConfigurationProperties}.
*
* @param config config settings for SQS
* @param builder a configured {@link software.amazon.awssdk.services.sqs.SqsClientBuilder}.
* @return the {@link SQSConnectionFactory} defined by the {@code config}.
*/
@JMSConnectionFactory(CONNECTION_FACTORY_BEAN_NAME)
public ConnectionFactory sqsJmsConnectionFactory(SqsConfigurationProperties config,
SqsClientBuilder builder) {
logger.debug("created ConnectionFactory bean '{}' (SQSConnectionFactory)",
CONNECTION_FACTORY_BEAN_NAME);
return new SQSConnectionFactory(
new ProviderConfiguration().withNumberOfMessagesToPrefetch(config.getNumberOfMessagesToPrefetch()),
builder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
Args = --allow-incomplete-classpath --enable-url-protocols=https -H:IncludeResourceBundles=com.sun.org.apache.xml.internal.res.XMLErrorResources
Args = --enable-url-protocols=https -H:IncludeResourceBundles=com.sun.org.apache.xml.internal.res.XMLErrorResources
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
[
[
"org.apache.http.conn.HttpClientConnectionManager",
"org.apache.http.pool.ConnPoolControl",
"com.amazonaws.http.conn.Wrapped"
"org.apache.http.pool.ConnPoolControl"
],
[
"org.apache.http.conn.ConnectionRequest",
"com.amazonaws.http.conn.Wrapped"
]
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,6 @@
"allDeclaredMethods": true,
"allDeclaredClasses": true
},
{
"name": "com.amazonaws.services.sqs.model.QueueDoesNotExistException",
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredClasses": true
},
{
"name": "com.amazonaws.services.sqs.MessageMD5ChecksumHandler",
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredClasses": true
},
{
"name": "com.amazonaws.services.sqs.QueueUrlHandler",
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredClasses": true
},
{
"name": "com.amazonaws.jmx.SdkMBeanRegistrySupport",
"allDeclaredFields": true,
Expand Down Expand Up @@ -146,104 +125,18 @@
"allDeclaredMethods": true,
"allDeclaredClasses": true
},
{
"name": "com.amazonaws.partitions.model.Partitions",
"allPublicMethods": true,
"allDeclaredConstructors": true
},
{
"name": "com.amazonaws.partitions.model.Partition",
"allPublicMethods": true,
"allDeclaredConstructors": true
},
{
"name": "com.amazonaws.partitions.model.Endpoint",
"allPublicMethods": true,
"allDeclaredConstructors": true
},
{
"name": "com.amazonaws.partitions.model.Region",
"allPublicMethods": true,
"allDeclaredConstructors": true
},
{
"name": "com.amazonaws.partitions.model.Service",
"allPublicMethods": true,
"allDeclaredConstructors": true
},
{
"name": "com.amazonaws.partitions.model.CredentialScope",
"allPublicMethods": true,
"allDeclaredConstructors": true
},
{
"name": "java.util.HashSet",
"allPublicMethods": true,
"allDeclaredConstructors": true
},
{
"name": "com.amazonaws.internal.config.InternalConfigJsonHelper",
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredClasses": true
},
{
"name": "com.amazonaws.internal.config.SignerConfig",
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredClasses": true
},
{
"name": "com.amazonaws.internal.config.SignerConfigJsonHelper",
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredClasses": true
},
{
"name": "com.amazonaws.internal.config.HttpClientConfig",
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredClasses": true
},
{
"name": "com.amazonaws.internal.config.HttpClientConfigJsonHelper",
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredClasses": true
},
{
"name": "com.amazonaws.internal.config.HostRegexToRegionMappingJsonHelper",
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredClasses": true
},
{
"name": "com.amazonaws.internal.config.JsonIndex",
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredClasses": true
},
{
"name": "sun.security.ssl.SSLContextImpl$TLSContext",
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredClasses": true
},
{
"name": "com.amazonaws.auth.AWS4Signer",
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredClasses": true
},
{
"name": "com.sun.crypto.provider.HmacCore$HmacSHA256",
"allDeclaredFields": true,
Expand Down Expand Up @@ -453,19 +346,5 @@
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredClasses": true
},
{
"name":"com.amazonaws.metrics.MetricAdmin",
"allPublicConstructors":true
},
{
"name":"com.amazonaws.metrics.MetricAdminMBean",
"allPublicMethods":true
},
{
"name":"com.amazonaws.services.sqs.internal.SQSRequestHandler",
"allDeclaredFields":true,
"allDeclaredMethods":true,
"allDeclaredConstructors":true
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@
"includes": [
{
"pattern": "org/joda/time/tz/data/.*"
},
{
"pattern": "com/amazonaws/partitions/endpoints.json"
},
{
"pattern": "com/amazonaws/internal/config/awssdk_config_default.json"
},
{
"pattern": "com/amazonaws/sdk/versionInfo.properties"
}
]
}
}
}
29 changes: 1 addition & 28 deletions src/main/docs/guide/configuration/sqs.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,9 @@ and the following to your `build.gradle` or `pom.xml`

dependency:io.micronaut.jms:micronaut-jms-sqs[]

Additionally you'll need to configure an instance of `com.amazonaws.services.sqs.AmazonSQS` as a bean for AWS authentication, for example:

[source,java]
----
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClientBuilder;
import io.micronaut.aws.sdk.v1.EnvironmentAWSCredentialsProvider;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.env.Environment;
import javax.inject.Singleton;
import static com.amazonaws.regions.Regions.US_EAST_2;
@Factory
public class SqsClientFactory {
@Singleton
AmazonSQS sqsClient(Environment environment) {
return AmazonSQSClientBuilder
.standard()
.withRegion(US_EAST_2)
.withCredentials(new EnvironmentAWSCredentialsProvider(environment))
.build();
}
}
----

See the link:https://micronaut-projects.github.io/micronaut-aws/latest/guide/index.html[Micronaut AWS] docs for more information.

Note that it's not necessary to add a dependency on `io.micronaut.aws:micronaut-aws-sdk-v1` as it's already a transitive dependency of `io.micronaut.jms:micronaut-jms-sqs`.
Note that it's not necessary to add a dependency on `io.micronaut.aws:micronaut-aws-sdk-v2` as it's already a transitive dependency of `io.micronaut.jms:micronaut-jms-sqs`.

The JMS link:{apijms}ConnectionFactory.html[ConnectionFactory] will be an instance of `com.amazon.sqs.javamessaging.SQSConnectionFactory`; you can customize that with a link:{apimicronaut}context/event/BeanCreatedEventListener.html[BeanCreatedEventListener].
2 changes: 2 additions & 0 deletions tests/tasks-sqs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ plugins {
dependencies {
implementation projects.jmsSqs
testImplementation libs.testcontainers.localstack
testCompileOnly libs.lombok
testAnnotationProcessor libs.lombok
}
2 changes: 1 addition & 1 deletion tests/tasks-sqs/src/main/java/example/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ public class Application {
public static void main(String[] args) {
Micronaut.run(Application.class);
}
}
}
Loading

0 comments on commit f5bb8bc

Please sign in to comment.