Я пытаюсь создать минимальный проект Java Gradle с помощью Spring Boot и Spring Cloud AWS SQS, но я не могу заставить его читать из очереди.
Это мои файлы проекта:
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:
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());
}
}
application.yaml в src / main / resources:
cloud:
aws:
credentials:
accessKey: **********************
secretKey: **********************
region:
static: us-west-2
Приложение выдает исключение при запуске (но вы можете увидеть исключение только в режиме отладки журнала!):
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:
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 '***'
Так что я не уверен, почему он ищет где-то еще?
Также он выбрасывает это исключение (всегда отображается, только если вы находитесь в режиме отладки журнала):
java.lang.ClassNotFoundException: org.springframework.data.web.config.EnableSpringDataWebSupport
Поэтому мне пришлось добавить build.gradle
compile("org.springframework.data:spring-data-commons:1.12.1.RELEASE")
Но теперь исключения больше нет, программа запускается и завершается без каких-либо действий, и она больше не печатает журналы!
Еще несколько фактов:
- Application.java находится в правильном пакете, чтобы позволить сканированию компонента работать, поэтому Spring должен увидеть класс QueueListener,
- application.yaml читается правильно, если неправильно указать регион, он жалуется,
- если я введу неправильные учетные данные (неправильный ключ доступа или / и неправильный ключ secretKey), он не будет жаловаться, поэтому я не думаю, что он вообще пытается подключиться к AWS.
Я не уверен, что строка 34 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 автоматически.
Что мне не хватает? Спасибо!