Skip to content

Not able to initialize SQSClient with aws sdk v2

0

I'm trying to initialize SQSClient like this for using it further to call sendMessage():

    @PostConstruct
private SqsClient getAWSSQSClient() {
    try {           
            AwsBasicCredentials credentials = AwsBasicCredentials.create(accessKeyId, secretKey);
            StaticCredentialsProvider staticCredentialsProvider = StaticCredentialsProvider.create(credentials);
            sqsClient = SqsClient.builder().credentialsProvider(staticCredentialsProvider).region(Region.of(region)).build();
            logger.info("AmazonSQS initialized successfully");          
            return sqsClient;
    } catch (Exception e) {
        logger.error("Exception occurred while initializing AmazonSQS :: ", e);
        return null;
    }
}

But getting below exception:

 Error  starting ApplicationContext. To display the condition evaluation report re-run
 your application with 'debug' enabled. 
 2024-Dec-24 12:59:43.198 [main] ERROR o.s.b.SpringApplication - Application run failed 
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
 'SQSService': Invocation of init method failed
at 
 org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postPr ocessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:222)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:421)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1767)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:523)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:325)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:946)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:616)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:753)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:455)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:323)
at xx.xx.xx.xx.Application.main(Application.java:53)
 Caused by: java.lang.IncompatibleClassChangeError: class software.amazon.awssdk.services.sqs.DefaultSqsBaseClientBuilder overrides final method software.amazon.awssdk.awscore.client.builder.AwsDefaultClientBuilder.setOverrides(Lsoftware/amazon/awssdk/core/client/config/SdkClientConfiguration;)Lsoftware/am

Below is the dependency for SQS:

        <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>sqs</artifactId>
        <version>2.29.37</version>
        </dependency>
2 Answers
2
Accepted Answer

The issue you're experiencing appears to be related to a dependency conflict or version incompatibility in your project. The IncompatibleClassChangeError suggests that there's a mismatch between the compiled code and the runtime classes, which often occurs when different versions of a library are present on the classpath.

To resolve this, try the following steps:

  1. Ensure that you're using compatible versions of all AWS SDK components. It's recommended to use the AWS SDK BOM (Bill of Materials) to manage AWS SDK dependencies. Add the following to your pom.xml:
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>bom</artifactId>
            <version>2.29.37</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Then, you can specify the SQS dependency without a version:

<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>sqs</artifactId>
</dependency>
  1. Check for any conflicting AWS SDK dependencies in your project. Make sure you're not inadvertently including multiple versions of AWS SDK components.

  2. Clean and rebuild your project to ensure all dependencies are properly resolved.

  3. If the issue persists, try creating the SqsClient using the default create() method:

SqsClient sqsClient = SqsClient.create();

This uses the default credential provider chain and region provider, which can simplify the setup if your AWS credentials are properly configured in your environment.

If you need to specify the region and credentials explicitly, you can do so like this:

SqsClient sqsClient = SqsClient.builder()
    .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKeyId, secretKey)))
    .region(Region.of(region))
    .build();

Remember to handle the returned SqsClient properly, as your current method is annotated with @PostConstruct but also returns the client, which isn't typically how @PostConstruct methods are used.

If these steps don't resolve the issue, you may need to review your overall project structure and dependency management to ensure there are no conflicts or incompatibilities with other libraries you're using.
Sources
SqsClient (AWS SDK for Java - 2.21.28)
Use SendMessage with an AWS SDK or CLI - Amazon Simple Queue Service

answered 10 months ago
EXPERT
reviewed 10 months ago
0

The error you're encountering, java.lang.IncompatibleClassChangeError, suggests that there might be a compatibility issue between different versions of the AWS SDK or other dependencies in your project. This specific error occurs when a class has changed incompatibly with respect to its superclass or interfaces.

Here are a few steps you can take to resolve this issue:

  1. Check Dependency Versions: Ensure that all AWS SDK dependencies are compatible with each other. You might need to update or align the versions of the AWS SDK libraries you're using. For example, if you're using software.amazon.awssdk:sqs, make sure it matches the version of software.amazon.awssdk:core.

  2. Update AWS SDK: Sometimes, updating to the latest version of the AWS SDK can resolve such issues. Check the AWS SDK for Java 2.x documentation for the latest versions and update your pom.xml or build.gradle accordingly.

  3. Clean and Rebuild: Perform a clean build of your project to ensure that there are no stale or conflicting class files. In Maven, you can use:

    mvn clean install

    In Gradle, you can use:

    gradle clean build
  4. Check for Conflicting Dependencies: Use tools like mvn dependency:tree (for Maven) or gradle dependencies (for Gradle) to inspect your project's dependency tree and identify any potential conflicts.

Here's an example of how you might update your pom.xml for Maven:

<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>sqs</artifactId>
    <version>2.20.0</version> <!-- Ensure this version is compatible with other AWS SDK dependencies -->
</dependency>
<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>core</artifactId>
    <version>2.20.0</version> <!-- Match the version with sqs -->
</dependency>

And for Gradle:

dependencies {
    implementation 'software.amazon.awssdk:sqs:2.20.0'
    implementation 'software.amazon.awssdk:core:2.20.0'
}

After making these changes, clean and rebuild your project. If the issue persists, please share more details about your project setup and dependencies, and we can further troubleshoot the problem.

answered 10 months ago
EXPERT
reviewed 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.