Java Spring引导、Spring云AWS和AWS SQS未从队列中读取

Java Spring引导、Spring云AWS和AWS SQS未从队列中读取,java,amazon-web-services,spring-boot,aws-sdk,spring-cloud,Java,Amazon Web Services,Spring Boot,Aws Sdk,Spring Cloud,我正在尝试用Spring Boot和Spring Cloud AWS SQS构建一个最小的gradle java项目,但我无法从队列中读取它 这些是我的项目文件: build.gradle: apply plugin: "java" apply plugin: "eclipse" apply plugin: "spring-boot" apply plugin: "io.spring.dependency-management" sourceCompatibility = 1.8 target

我正在尝试用Spring Boot和Spring Cloud AWS SQS构建一个最小的gradle java项目,但我无法从队列中读取它

这些是我的项目文件:

build.gradle:

apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "spring-boot"
apply plugin: "io.spring.dependency-management"

sourceCompatibility = 1.8
targetCompatibility = 1.8

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.5.RELEASE")
        classpath("io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE")
    }
}

dependencyManagement {
     imports {
          mavenBom("org.springframework.cloud:spring-cloud-aws:1.1.0.RELEASE")
     }
}

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-actuator:1.3.5.RELEASE")
    compile("org.springframework.cloud:spring-cloud-starter-aws:1.1.0.RELEASE")
    // if I don't add the line below, the annotation @MessageMapping is not found :(
    // I would have expected that cloud-starter-aws would have taken care of it
    compile("org.springframework.cloud:spring-cloud-aws-messaging:1.1.0.RELEASE")
    // this has been added to fix an exception happening, please read below
    compile("org.springframework.data:spring-data-commons:1.12.1.RELEASE")
}
compile("org.springframework.cloud:spring-cloud-starter-aws:1.1.0.RELEASE")
// if I don't add the line below, the annotation @MessageMapping is not found :(
// I would have expected that cloud-starter-aws would have taken care of it
compile("org.springframework.cloud:spring-cloud-aws-messaging:1.1.0.RELEASE")
// this has been added to fix an exception happening, please read below
compile("org.springframework.data:spring-data-commons:1.12.1.RELEASE")
Application.java:

package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application
{    
    public static void main(String[] args)
    {
        SpringApplication.run(Application.class, args);
    }
}
QueueListener.java:

package com.test.sqs;

import java.util.logging.Logger;
import org.springframework.messaging.handler.annotation.MessageMapping;
import com.test.sqs.model.TestMessage;

public class QueueListener
{
    @MessageMapping("test_queue")
    private void receiveMessage(TestMessage testMessage)
    {
        System.out.println("Test message received: " + testMessage.getMessage());
    }
}
src/main/resources中的application.yaml:

cloud:
    aws:
        credentials:
            accessKey: **********************
            secretKey: **********************
        region:
            static: us-west-2
应用程序在启动时引发异常(但您可以仅在日志调试模式下看到异常!):

但在日志中,我总能看到它确实从yaml文件中获取了我的凭据:

2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'cloud.aws.credentials.accessKey' in [systemProperties]
2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'cloud.aws.credentials.accessKey' in [systemEnvironment]
2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'cloud.aws.credentials.accessKey' in [random]
2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'cloud.aws.credentials.accessKey' in [applicationConfigurationProperties]
2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Found key 'cloud.aws.credentials.accessKey' in [applicationConfigurationProperties] with type [String] and value '***'
2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'cloud.aws.credentials.secretKey' in [systemProperties]
2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'cloud.aws.credentials.secretKey' in [systemEnvironment]
2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'cloud.aws.credentials.secretKey' in [random]
2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Searching for key 'cloud.aws.credentials.secretKey' in [applicationConfigurationProperties]
2016-05-19 11:15:14.546 DEBUG 11704 --- [           main] o.s.c.e.PropertySourcesPropertyResolver  : Found key 'cloud.aws.credentials.secretKey' in [applicationConfigurationProperties] with type [String] and value '***'
所以我不知道它为什么要找别的地方

它还会引发此异常(仅当您处于日志调试模式时始终可见):

所以我不得不加入build.gradle

compile("org.springframework.data:spring-data-commons:1.12.1.RELEASE")
但是现在例外情况已经不存在了,但是程序开始和结束时什么都不做,并且不再打印日志了

其他一些事实:

  • java在正确的包中,可以让组件扫描工作,因此Spring应该看到QueueListener类
  • 正确读取application.yaml,如果将其投诉的区域放错
  • 如果我输入了错误的凭证(错误的accessKey/和错误的secretKey),它不会抱怨,所以我认为它根本不会尝试连接到AWS
我不确定build.gradle的第34行是否:

apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "spring-boot"
apply plugin: "io.spring.dependency-management"

sourceCompatibility = 1.8
targetCompatibility = 1.8

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.5.RELEASE")
        classpath("io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE")
    }
}

dependencyManagement {
     imports {
          mavenBom("org.springframework.cloud:spring-cloud-aws:1.1.0.RELEASE")
     }
}

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-actuator:1.3.5.RELEASE")
    compile("org.springframework.cloud:spring-cloud-starter-aws:1.1.0.RELEASE")
    // if I don't add the line below, the annotation @MessageMapping is not found :(
    // I would have expected that cloud-starter-aws would have taken care of it
    compile("org.springframework.cloud:spring-cloud-aws-messaging:1.1.0.RELEASE")
    // this has been added to fix an exception happening, please read below
    compile("org.springframework.data:spring-data-commons:1.12.1.RELEASE")
}
compile("org.springframework.cloud:spring-cloud-starter-aws:1.1.0.RELEASE")
// if I don't add the line below, the annotation @MessageMapping is not found :(
// I would have expected that cloud-starter-aws would have taken care of it
compile("org.springframework.cloud:spring-cloud-aws-messaging:1.1.0.RELEASE")
// this has been added to fix an exception happening, please read below
compile("org.springframework.data:spring-data-commons:1.12.1.RELEASE")
这可能是问题的征兆,我希望CloudStarter aws自动加载所有需要的库


我错过了什么?谢谢大家!

发现了问题,这当然是愚蠢的事情

Spring没有加载QueueListener类,因为它没有服务/组件注释,因此:

@Service
public class SqsQueueSender
{
...
}
修正了这个问题