Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while connecting to AWS SQS from spring boot

I am trying to integrate AWS SQS into my springboot app using spring cloud AWS, but keep getting this error(posted below), can someone help?

Here are my files.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'simpleMessageListenerContainer' defined in class path resource [org/springframework/cloud/aws/messaging/config/annotation/SqsConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: com.amazonaws.http.ExecutionContext.setCredentials(Lcom/amazonaws/auth/AWSCredentials;)V

@Configuration
public class AWSConfig {
    @Value("${amazon.dynamodb.endpoint}")
    private String amazonDynamoDBEndpoint;

    @Value("${amazon.aws.accesskey}")
    private String amazonAWSAccessKey;

    @Value("${amazon.aws.secretkey}")
    private String amazonAWSSecretKey;

    @Value("${amazon.sqs.endpoint}")
    private String amazonSqsEndpoint;
    @Bean
    @Primary
    public AmazonSQSAsyncClient amazonSQSAsyncClient() {

        AmazonSQSAsyncClient amazonSQSAsyncClient = new AmazonSQSAsyncClient(amazonAWSCredentials());
        if (!StringUtils.isEmpty(amazonSqsEndpoint)) {
            amazonSQSAsyncClient.setEndpoint(amazonSqsEndpoint);
        }

        return amazonSQSAsyncClient;

    }

    @Bean
    public AWSCredentials amazonAWSCredentials() {
        return new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey);
    }
}

I am able to work with dynamodb with this but not able to connect to SQS. I have given the correct access key, secret access key and end point in application.properties file.

@Component
    @EnableSqs
    public class SQSDao {
        private static final Logger logger = LoggerFactory.getLogger(SQSDao.class);

        private  QueueMessagingTemplate queueMessagingTemplate;

        @Autowired
        public SQSDao(AmazonSQSAsync amazonSqs) {
            this.queueMessagingTemplate = new QueueMessagingTemplate(amazonSqs);
        }

        public void send(String message) {
            System.out.println(queueMessagingTemplate.getDefaultDestination());
            queueMessagingTemplate.convertAndSend("test-queue",  MessageBuilder.withPayload(message).build());
        }

        @SqsListener(value = "test-queue", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
        public void receive(String message)
        {
            System.out.println("message: " + message);
        }

    }
like image 743
Bhargav Avatar asked Apr 28 '17 07:04

Bhargav


1 Answers

I was facing the same issue as described. My solution requeried implement some extra methods for the Config class:

imports [...]

@Configuration
@RefreshScope
public class SpringCloudSQSConfig {

@Value("${cloud.aws.credentials.accessKeyId:default}")
private String accessKeyId;

@Value("${cloud.aws.credentials.secretKey:default}")
private String secretKey;

@Value("${cloud.aws.region.static:default}")
private String region;

private Logger logger = LoggerFactory.getLogger(this.getClass());

@Bean
public QueueMessagingTemplate queueMessagingTemplate() {
    return new QueueMessagingTemplate(amazonSQSAsync());
}

public AmazonSQSAsync amazonSQSAsync() {
    return AmazonSQSAsyncClientBuilder.standard().withRegion(Regions.US_EAST_2)
            .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKeyId, secretKey)))
            .build();
}

@Bean
public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory() {
    SimpleMessageListenerContainerFactory msgListenerContainerFactory = new SimpleMessageListenerContainerFactory();
    msgListenerContainerFactory.setAmazonSqs(amazonSQSAsync());
    return msgListenerContainerFactory;
}

@Bean
public QueueMessageHandler queueMessageHandler() {
    QueueMessageHandlerFactory queueMsgHandlerFactory = new QueueMessageHandlerFactory();
    queueMsgHandlerFactory.setAmazonSqs(amazonSQSAsync());
    QueueMessageHandler queueMessageHandler = queueMsgHandlerFactory.createQueueMessageHandler();
    List<HandlerMethodArgumentResolver> list = new ArrayList<>();
    HandlerMethodArgumentResolver resolver = new PayloadArgumentResolver(new MappingJackson2MessageConverter());
    list.add(resolver);
    queueMessageHandler.setArgumentResolvers(list);
    return queueMessageHandler;
}
}

And for the dependencies implemented:

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

and adding the next property in the properties file:

spring.main.allow-bean-definition-overriding=true

like image 84
Martin Pacheco Avatar answered Sep 30 '22 16:09

Martin Pacheco