首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring、和AWS没有从队列中读取

Spring、和AWS没有从队列中读取
EN

Stack Overflow用户
提问于 2016-05-18 11:02:40
回答 1查看 3.4K关注 0票数 2

我试图用Spring和构建一个最小的gradle java项目,但是我无法让它从队列中读取。

这些是我的项目文件:

build.gradle:

代码语言:javascript
复制
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")
}

Application.java:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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());
    }
}

application.yaml在src/main/resources中:

代码语言:javascript
复制
cloud:
    aws:
        credentials:
            accessKey: **********************
            secretKey: **********************
        region:
            static: us-west-2

应用程序启动时抛出异常(但您可以在日志调试模式下看到异常!):

代码语言:javascript
复制
org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.amazonaws.auth.profile.ProfilesConfigFile]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: AWS credential profiles file not found in the given path: C:\src\collector\default

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

代码语言:javascript
复制
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 '***'

所以我不知道它为什么要找别的地方?

此外,它还抛出此异常(如果您处于日志调试模式,总是可见的):

代码语言:javascript
复制
    java.lang.ClassNotFoundException: org.springframework.data.web.config.EnableSpringDataWebSupport

所以我不得不加上build.gradle

代码语言:javascript
复制
compile("org.springframework.data:spring-data-commons:1.12.1.RELEASE")

但是现在例外不再存在了,但是程序开始和结束时什么都不做,也不再打印日志了!

其他一些事实:

  • Application.java在正确的包中,可以让组件扫描工作,所以Spring应该看到QueueListener类,
  • application.yaml被正确读取,如果它抱怨一个放置错误的区域,
  • 如果我输入了错误的凭证(错误的accessKey或/和错误的secretKey),它不会抱怨,所以我认为它根本没有试图连接到AWS。

我不确定build.gradle的第34行是否:

代码语言:javascript
复制
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")

这可能是问题的一个症状,我希望所有所需的库都能被云启动器-aws自动加载。

我遗漏了什么?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-19 12:08:18

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

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

代码语言:javascript
复制
@Service
public class SqsQueueSender
{
...
}

解决了这个问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37297688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档