Spring Boot,Spring Cloud AWS和AWS SQS无法从队列中读取 [英] Spring Boot, Spring Cloud AWS and AWS SQS not reading from the queue

查看:116
本文介绍了Spring Boot,Spring Cloud AWS和AWS SQS无法从队列中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Spring Boot和Spring Cloud AWS SQS构建最小的Gradle Java项目,但无法从队列中读取它.

I am trying a to build a minimal gradle java project with Spring Boot and Spring Cloud AWS SQS but I can't get it to read from the queue.

这些是我的项目文件:

build.gradle:

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")
}

Application.java:

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:

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:

application.yaml in src/main/resources:

cloud:
    aws:
        credentials:
            accessKey: **********************
            secretKey: **********************
        region:
            static: us-west-2

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

The application throws an exception when starting (but you can see the exception just in log debug mode!):

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文件中提取了我的凭据:

but always in the log I can see it did pick up my credentials from the yaml file:

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 '***'

所以我不确定为什么在其他地方找它?

So I am not sure why it is looking somewhere else?

它还会引发此异常(仅在处于调试模式时才可见):

Also it's throwing this exception (always visible just if you are in logging debug mode):

    java.lang.ClassNotFoundException: org.springframework.data.web.config.EnableSpringDataWebSupport

所以我必须添加build.gradle

So I had to add in build.gradle

compile("org.springframework.data:spring-data-commons:1.12.1.RELEASE")

但是现在不再有异常了,但是程序不执行任何操作就开始和结束了,并且不再打印日志了!

But now the exception is not there anymore but the program starts and ends without doing anything, and it's not printing logs anymore!

其他一些事实:

  • Application.java位于正确的包中,以使组件扫描正常工作,因此Spring应该看到QueueListener类,
  • 正确读取了application.yaml,如果抱怨的区域错误,则
  • 如果我输入了错误的凭证(错误的accessKey或/和错误的secretKey),它不会发牢骚,因此我认为它根本没有尝试连接到AWS.

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

I am not sure if line 34 of build.gradle:

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")

可能是问题的征兆,我希望cloud-starter-aws会自动加载所有需要的库.

could be a symptom of the issue, I would expect for all the needed libraries to be loaded by the cloud-starter-aws automatically.

我想念什么?谢谢!

推荐答案

发现了问题,这当然很愚蠢.

Found the issue, and it was something stupid of course.

Spring没有加载QueueListener类,因为它没有Service/Component批注,因此:

Spring was not loading the QueueListener class because it didn't have the Service / Component annotation, so:

@Service
public class SqsQueueSender
{
...
}

解决了该问题.

这篇关于Spring Boot,Spring Cloud AWS和AWS SQS无法从队列中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆