How to avoid connection to AWS region when @SQSListener is configured












0















I have an issue while testing my application in localstack. The requirement is simple. I would like to subscribe to a topic in AWS-SNS with SQS subscription. In localstack when I am executing when I configure @SQSListener it tries to validate the credentials with AWS and I get the below error message:



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 com.amazonaws.SdkClientException: Unable to load AWS credentials from any provider in the chain:



I tried setting up a dummy credentials for localstack that did not help either. I tried searching on net but could not find any relevant posts on this.



Code



@Slf4j
@Configuration
@Profile("local")
@EnableSns
@EnableSqs
public class LocalStackConfiguration {


@Value("${sqsClassifierQueueName}")
private String sqsClassifierQueueName;

@Value("${sqsConsQueueNames}")
private List<String> sqsConsQueueNames;

@Bean(name = "amazonSqs")
public AmazonSQS configureSQSClient()
throws MalformedURLException
{
log.info("Loading local SQS client");
BasicAWSCredentials awsCreds = new BasicAWSCredentials("user", "password");

AmazonSQS sqsClient = AmazonSQSClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:4576", "us-east-1"))
.build();

sqsClient.createQueue(sqsClassifierQueueName);
sqsConsQueueNames.forEach(queueName -> sqsClient.createQueue(queueName));
//log.info("Device Registry queue Created: " + sqsClient.createQueue(REGISTRY_QUEUE_URL.substring(REGISTRY_QUEUE_URL.lastIndexOf('/') + 1)));
return sqsClient;
}

@Bean(name = "amazonSns")
public AmazonSNS configureSNSClient() {
AmazonSNS snsClient= AmazonSNSClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("user", "password")))
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:4575", "us-east-1"))
.build();
return snsClient;
}



}


@Component
@DependsOn(value = {"amazonSns", "amazonSqs"})
public class SNSSubscription {

@Qualifier("amazonSns")
@Autowired
private AmazonSNS sns;

@Qualifier("amazonSqs")
@Autowired
private AmazonSQS sqs;

@Value("${snsClassifierTopicName}")
private String snsClassifierTopicName;

@Value("${sqsClassifierQueueName}")
private String sqsClassifierQueueName;

@PostConstruct
public void subscribeToTopics() {
subscribeToClassifierTopics();
}

private void subscribeToClassifierTopics() {
SNSSubscriptionUtils.subscribeToSQS(sns, sqs, snsClassifierTopicName, sqsClassifierQueueName);

}

@SqsListener(value="${snsClassifierTopicName}")
public void receiveMessage(Object obj){
System.out.println(obj);
}
}



public class SNSSubscriptionUtils {

public static void subscribeToSQS(AmazonSNS sns, AmazonSQS sqs, String topicName, String queueName) {
String snsTopicArn = getSnsTopicArn(sns, topicName);
String queueUrl = SQSUtils.getQueueUrl(sqs, queueName);
Topics.subscribeQueue(sns, sqs, snsTopicArn, queueUrl);
}

public static String getSnsTopicArn(AmazonSNS sns, String topicName) {
String topicArn = sns.createTopic(topicName).getTopicArn();
return topicArn;
}
}


public class SQSUtils {

public static String getQueueUrl(AmazonSQS sqs, String queueName) {
GetQueueUrlRequest queueUrlRequest = new GetQueueUrlRequest(queueName);
return sqs.getQueueUrl(queueUrlRequest).getQueueUrl();
}
}









share|improve this question



























    0















    I have an issue while testing my application in localstack. The requirement is simple. I would like to subscribe to a topic in AWS-SNS with SQS subscription. In localstack when I am executing when I configure @SQSListener it tries to validate the credentials with AWS and I get the below error message:



    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 com.amazonaws.SdkClientException: Unable to load AWS credentials from any provider in the chain:



    I tried setting up a dummy credentials for localstack that did not help either. I tried searching on net but could not find any relevant posts on this.



    Code



    @Slf4j
    @Configuration
    @Profile("local")
    @EnableSns
    @EnableSqs
    public class LocalStackConfiguration {


    @Value("${sqsClassifierQueueName}")
    private String sqsClassifierQueueName;

    @Value("${sqsConsQueueNames}")
    private List<String> sqsConsQueueNames;

    @Bean(name = "amazonSqs")
    public AmazonSQS configureSQSClient()
    throws MalformedURLException
    {
    log.info("Loading local SQS client");
    BasicAWSCredentials awsCreds = new BasicAWSCredentials("user", "password");

    AmazonSQS sqsClient = AmazonSQSClientBuilder.standard()
    .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
    .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:4576", "us-east-1"))
    .build();

    sqsClient.createQueue(sqsClassifierQueueName);
    sqsConsQueueNames.forEach(queueName -> sqsClient.createQueue(queueName));
    //log.info("Device Registry queue Created: " + sqsClient.createQueue(REGISTRY_QUEUE_URL.substring(REGISTRY_QUEUE_URL.lastIndexOf('/') + 1)));
    return sqsClient;
    }

    @Bean(name = "amazonSns")
    public AmazonSNS configureSNSClient() {
    AmazonSNS snsClient= AmazonSNSClientBuilder.standard()
    .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("user", "password")))
    .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:4575", "us-east-1"))
    .build();
    return snsClient;
    }



    }


    @Component
    @DependsOn(value = {"amazonSns", "amazonSqs"})
    public class SNSSubscription {

    @Qualifier("amazonSns")
    @Autowired
    private AmazonSNS sns;

    @Qualifier("amazonSqs")
    @Autowired
    private AmazonSQS sqs;

    @Value("${snsClassifierTopicName}")
    private String snsClassifierTopicName;

    @Value("${sqsClassifierQueueName}")
    private String sqsClassifierQueueName;

    @PostConstruct
    public void subscribeToTopics() {
    subscribeToClassifierTopics();
    }

    private void subscribeToClassifierTopics() {
    SNSSubscriptionUtils.subscribeToSQS(sns, sqs, snsClassifierTopicName, sqsClassifierQueueName);

    }

    @SqsListener(value="${snsClassifierTopicName}")
    public void receiveMessage(Object obj){
    System.out.println(obj);
    }
    }



    public class SNSSubscriptionUtils {

    public static void subscribeToSQS(AmazonSNS sns, AmazonSQS sqs, String topicName, String queueName) {
    String snsTopicArn = getSnsTopicArn(sns, topicName);
    String queueUrl = SQSUtils.getQueueUrl(sqs, queueName);
    Topics.subscribeQueue(sns, sqs, snsTopicArn, queueUrl);
    }

    public static String getSnsTopicArn(AmazonSNS sns, String topicName) {
    String topicArn = sns.createTopic(topicName).getTopicArn();
    return topicArn;
    }
    }


    public class SQSUtils {

    public static String getQueueUrl(AmazonSQS sqs, String queueName) {
    GetQueueUrlRequest queueUrlRequest = new GetQueueUrlRequest(queueName);
    return sqs.getQueueUrl(queueUrlRequest).getQueueUrl();
    }
    }









    share|improve this question

























      0












      0








      0








      I have an issue while testing my application in localstack. The requirement is simple. I would like to subscribe to a topic in AWS-SNS with SQS subscription. In localstack when I am executing when I configure @SQSListener it tries to validate the credentials with AWS and I get the below error message:



      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 com.amazonaws.SdkClientException: Unable to load AWS credentials from any provider in the chain:



      I tried setting up a dummy credentials for localstack that did not help either. I tried searching on net but could not find any relevant posts on this.



      Code



      @Slf4j
      @Configuration
      @Profile("local")
      @EnableSns
      @EnableSqs
      public class LocalStackConfiguration {


      @Value("${sqsClassifierQueueName}")
      private String sqsClassifierQueueName;

      @Value("${sqsConsQueueNames}")
      private List<String> sqsConsQueueNames;

      @Bean(name = "amazonSqs")
      public AmazonSQS configureSQSClient()
      throws MalformedURLException
      {
      log.info("Loading local SQS client");
      BasicAWSCredentials awsCreds = new BasicAWSCredentials("user", "password");

      AmazonSQS sqsClient = AmazonSQSClientBuilder.standard()
      .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
      .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:4576", "us-east-1"))
      .build();

      sqsClient.createQueue(sqsClassifierQueueName);
      sqsConsQueueNames.forEach(queueName -> sqsClient.createQueue(queueName));
      //log.info("Device Registry queue Created: " + sqsClient.createQueue(REGISTRY_QUEUE_URL.substring(REGISTRY_QUEUE_URL.lastIndexOf('/') + 1)));
      return sqsClient;
      }

      @Bean(name = "amazonSns")
      public AmazonSNS configureSNSClient() {
      AmazonSNS snsClient= AmazonSNSClientBuilder.standard()
      .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("user", "password")))
      .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:4575", "us-east-1"))
      .build();
      return snsClient;
      }



      }


      @Component
      @DependsOn(value = {"amazonSns", "amazonSqs"})
      public class SNSSubscription {

      @Qualifier("amazonSns")
      @Autowired
      private AmazonSNS sns;

      @Qualifier("amazonSqs")
      @Autowired
      private AmazonSQS sqs;

      @Value("${snsClassifierTopicName}")
      private String snsClassifierTopicName;

      @Value("${sqsClassifierQueueName}")
      private String sqsClassifierQueueName;

      @PostConstruct
      public void subscribeToTopics() {
      subscribeToClassifierTopics();
      }

      private void subscribeToClassifierTopics() {
      SNSSubscriptionUtils.subscribeToSQS(sns, sqs, snsClassifierTopicName, sqsClassifierQueueName);

      }

      @SqsListener(value="${snsClassifierTopicName}")
      public void receiveMessage(Object obj){
      System.out.println(obj);
      }
      }



      public class SNSSubscriptionUtils {

      public static void subscribeToSQS(AmazonSNS sns, AmazonSQS sqs, String topicName, String queueName) {
      String snsTopicArn = getSnsTopicArn(sns, topicName);
      String queueUrl = SQSUtils.getQueueUrl(sqs, queueName);
      Topics.subscribeQueue(sns, sqs, snsTopicArn, queueUrl);
      }

      public static String getSnsTopicArn(AmazonSNS sns, String topicName) {
      String topicArn = sns.createTopic(topicName).getTopicArn();
      return topicArn;
      }
      }


      public class SQSUtils {

      public static String getQueueUrl(AmazonSQS sqs, String queueName) {
      GetQueueUrlRequest queueUrlRequest = new GetQueueUrlRequest(queueName);
      return sqs.getQueueUrl(queueUrlRequest).getQueueUrl();
      }
      }









      share|improve this question














      I have an issue while testing my application in localstack. The requirement is simple. I would like to subscribe to a topic in AWS-SNS with SQS subscription. In localstack when I am executing when I configure @SQSListener it tries to validate the credentials with AWS and I get the below error message:



      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 com.amazonaws.SdkClientException: Unable to load AWS credentials from any provider in the chain:



      I tried setting up a dummy credentials for localstack that did not help either. I tried searching on net but could not find any relevant posts on this.



      Code



      @Slf4j
      @Configuration
      @Profile("local")
      @EnableSns
      @EnableSqs
      public class LocalStackConfiguration {


      @Value("${sqsClassifierQueueName}")
      private String sqsClassifierQueueName;

      @Value("${sqsConsQueueNames}")
      private List<String> sqsConsQueueNames;

      @Bean(name = "amazonSqs")
      public AmazonSQS configureSQSClient()
      throws MalformedURLException
      {
      log.info("Loading local SQS client");
      BasicAWSCredentials awsCreds = new BasicAWSCredentials("user", "password");

      AmazonSQS sqsClient = AmazonSQSClientBuilder.standard()
      .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
      .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:4576", "us-east-1"))
      .build();

      sqsClient.createQueue(sqsClassifierQueueName);
      sqsConsQueueNames.forEach(queueName -> sqsClient.createQueue(queueName));
      //log.info("Device Registry queue Created: " + sqsClient.createQueue(REGISTRY_QUEUE_URL.substring(REGISTRY_QUEUE_URL.lastIndexOf('/') + 1)));
      return sqsClient;
      }

      @Bean(name = "amazonSns")
      public AmazonSNS configureSNSClient() {
      AmazonSNS snsClient= AmazonSNSClientBuilder.standard()
      .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("user", "password")))
      .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:4575", "us-east-1"))
      .build();
      return snsClient;
      }



      }


      @Component
      @DependsOn(value = {"amazonSns", "amazonSqs"})
      public class SNSSubscription {

      @Qualifier("amazonSns")
      @Autowired
      private AmazonSNS sns;

      @Qualifier("amazonSqs")
      @Autowired
      private AmazonSQS sqs;

      @Value("${snsClassifierTopicName}")
      private String snsClassifierTopicName;

      @Value("${sqsClassifierQueueName}")
      private String sqsClassifierQueueName;

      @PostConstruct
      public void subscribeToTopics() {
      subscribeToClassifierTopics();
      }

      private void subscribeToClassifierTopics() {
      SNSSubscriptionUtils.subscribeToSQS(sns, sqs, snsClassifierTopicName, sqsClassifierQueueName);

      }

      @SqsListener(value="${snsClassifierTopicName}")
      public void receiveMessage(Object obj){
      System.out.println(obj);
      }
      }



      public class SNSSubscriptionUtils {

      public static void subscribeToSQS(AmazonSNS sns, AmazonSQS sqs, String topicName, String queueName) {
      String snsTopicArn = getSnsTopicArn(sns, topicName);
      String queueUrl = SQSUtils.getQueueUrl(sqs, queueName);
      Topics.subscribeQueue(sns, sqs, snsTopicArn, queueUrl);
      }

      public static String getSnsTopicArn(AmazonSNS sns, String topicName) {
      String topicArn = sns.createTopic(topicName).getTopicArn();
      return topicArn;
      }
      }


      public class SQSUtils {

      public static String getQueueUrl(AmazonSQS sqs, String queueName) {
      GetQueueUrlRequest queueUrlRequest = new GetQueueUrlRequest(queueName);
      return sqs.getQueueUrl(queueUrlRequest).getQueueUrl();
      }
      }






      spring-cloud amazon-sqs atlassian-localstack






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 30 '18 at 0:01









      ParanthamanParanthaman

      113




      113
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53974287%2fhow-to-avoid-connection-to-aws-region-when-sqslistener-is-configured%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53974287%2fhow-to-avoid-connection-to-aws-region-when-sqslistener-is-configured%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Mossoró

          Error while reading .h5 file using the rhdf5 package in R

          Pushsharp Apns notification error: 'InvalidToken'