@SQSListen导致异常,无法正常工作

时间:2020-03-15 06:16:47

标签: java amazon-web-services spring-boot amazon-sqs

我有一个非常简单的Spring cloud aws项目。我正在使用Java 11。 这是pom:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.demo.arf</groupId>
    <artifactId>testsqs-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>testsqs-boot</name>
    <description>Demo project for Spring Boot</description>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR3</version>
            <type>pom</type>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

配置类:

package com.demo.arf.testsqsboot;

import com.amazonaws.services.sqs.AmazonSQSAsync;
import com.amazonaws.services.sqs.AmazonSQSAsyncClientBuilder;
import org.springframework.beans.factory.annotation.Value;
//import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate;
import org.springframework.cloud.aws.messaging.config.SimpleMessageListenerContainerFactory;
import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
//import com.amazonaws.services.sqs.AmazonSQSAsync;
//import com.amazonaws.services.sqs.AmazonSQSAsyncClientBuilder;

@Configuration
public class SQSConfig {

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

    @Value("${cloud.aws.credentials.access-key}")
    private String awsAccessKey;

    @Value("${cloud.aws.credentials.secret-key}")
    private String awsSecretKey;

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

    public AmazonSQSAsync amazonSQSAsync() {
        return AmazonSQSAsyncClientBuilder.standard().withRegion(Regions.US_EAST_1)
                .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccessKey, awsSecretKey)))
                .build();
    }

    @Bean
    public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(AmazonSQSAsync amazonSQS){
        SimpleMessageListenerContainerFactory factory = new SimpleMessageListenerContainerFactory();
        factory.setAmazonSqs(amazonSQS);
        factory.setMaxNumberOfMessages(10);
        return factory;
    }
}

发送/接收消息的控制器类:

package com.demo.arf.testsqsboot.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate;
import org.springframework.cloud.aws.messaging.listener.annotation.SqsListener;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SQSController {
    @Autowired
    private QueueMessagingTemplate queueMessagingTemplate;

    private static final Logger LOG = LoggerFactory.getLogger(SQSController.class);
    @GetMapping("/send-sqs-message")
    public String sendMessage() {
        String sqsEndPoint= "https://sqs.us-east-2.amazonaws.com/1234567879/my_queue";
        queueMessagingTemplate.convertAndSend(sqsEndPoint, MessageBuilder.withPayload("hello from Spring Boot").build());
        return "Hello SQS";
    }

    @SqsListener("my_queue")
    public void getMessage(String message) {
      LOG.info(" *********** Message from SQS Queue - "+message);
    }
}

application.yml:

server:
  port: 9001
cloud:
  aws:
    region:
      static: us-east-1
      auto: false
    credentials:
      access-key: "asdmnasdn"
      secret-key: "sfkjsdjksdkj"
    end-point:
      uri: https://sqs.us-east-2.amazonaws.com/1234567879/my_queue

我可以正常发送。但是,当我添加侦听器时,在启动过程中出现以下错误,并且侦听器未收到消息:

2020-03-15 01:02:00.677  INFO 15423 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3853 ms
2020-03-15 01:02:01.109  INFO 15423 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
**WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.amazonaws.util.XpathUtils (file:/Users/arf/.m2/repository/com/amazonaws/aws-java-sdk-core/1.11.415/aws-java-sdk-core-1.11.415.jar) to method com.sun.org.apache.xpath.internal.XPathContext.getDTMManager()
WARNING: Please consider reporting this to the maintainers of com.amazonaws.util.XpathUtils
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2020-03-15 01:02:01.749  WARN 15423 --- [           main]**
s.c.a.m.l.SimpleMessageListenerContainer : Ignoring queue with name 'my_queue': The queue does not exist.; nested exception is com.amazonaws.services.sqs.model.QueueDoesNotExistException: The specified queue does not exist for this wsdl version. (Service: AmazonSQS; Status Code: 400; Error Code: AWS.SimpleQueueService.NonExistentQueue; Request ID: 62821505-3f34-5434-a6ee)
2020-03-15 01:02:01.749  INFO 15423 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService

还有一个基本问题。 @SQSListener如何知道在哪里可以找到aws帐户信息和sqs uri?

2 个答案:

答案 0 :(得分:1)

我通过配置类的以下更改使它起作用。但是,我想知道,没有此代码(使用withEndpointConfiguration构造AmazonSQSAsync)的大多数示例程序如何运行。

    public QueueMessagingTemplate queueMessagingTemplate(AmazonSQSAsync amazonSQS) {
        return new QueueMessagingTemplate(amazonSQS);
    }

    @Bean
    @Primary
    public AmazonSQSAsync amazonSQS(AWSCredentialsProvider credentials) {
        return AmazonSQSAsyncClientBuilder.standard()
                .withCredentials(credentials)
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(localStackSqsUrl, awsRegion))
                .build();
    }

    @Bean
    @Primary
    public AWSCredentialsProvider awsCredentialsProvider() {
        return new AWSCredentialsProviderChain(
                new AWSStaticCredentialsProvider(
                        new BasicAWSCredentials("local", "stack")));
    }```

答案 1 :(得分:0)

几件事。
-永远不要将您的AK / SK存储在这样的属性或yml文件中。我可以说那些是假值,但您总是想从〜/ .aws / credentials或实例元数据中提取它们。如果您仅调用.standard(),则像AmazonSqSAsyncClientBuilder这样的AWS客户端将自动执行。不需要凭据提供程序。
-其次,与地区相同
-第三,我相信@SqsListener将使用您之前定义的ContainerFactory bean,至少这是@JmsListener的工作方式。
-您收到的错误消息是,您在所选区域的帐户中找不到队列名称。您告诉我们us-east-1,但在发送代码中指定了us-east-2。根据您的帖子,我的猜测是,由于您的问题是关于@SqsListener而不是queueMessagingTemplate,因此您的队列位于us-east-2。