0

我正在尝试使用spring cloud AWS注释驱动的队列侦听器编写一个使用AWS SQS的Web应用程序,这是我的代码的样子:

XML AWS Bean:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aws-context="http://www.springframework.org/schema/cloud/aws/context"
       xmlns:aws-messaging="http://www.springframework.org/schema/cloud/aws/messaging"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/cloud/aws/context
		http://www.springframework.org/schema/cloud/aws/context/spring-cloud-aws-context.xsd
		http://www.springframework.org/schema/cloud/aws/messaging
	   	http://www.springframework.org/schema/cloud/aws/messaging/spring-cloud-aws-messaging">

    <!-- Define global credentials for all the AWS clients -->
    <aws-context:context-credentials>
        <aws-context:instance-profile-credentials/>
        <aws-context:simple-credentials access-key="${accessKey:}"
                                        secret-key="${secretKey:}"/>
    </aws-context:context-credentials>

    <!-- Define global region -->
    <aws-context:context-region region="EU_WEST_1"/>

    <!-- Cloud Formation Stack -->
    <aws-context:stack-configuration stack-name="StackName"/>
    <aws-messaging:annotation-driven-queue-listener />
</beans>

然后我写了这个类,它有一个带有 SqsListener 注释的方法,它在控制台打印你好:

package com.example.demo;

import org.springframework.cloud.aws.messaging.listener.annotation.SqsListener;

public class AWSSQSListner {

    @SqsListener("queue-name")
    public void queueListener(Person person) {
        System.out.print("\"Hello\"");
    }
}

这是我的 gradle 构建文件:

Gradle build file:

    buildscript {
    ext {
        springBootVersion = '2.0.5.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


ext {
    springCloudVersion = 'Finchley.SR1'
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web-services')
    compile('org.springframework.cloud:spring-cloud-starter-aws')
    compile('org.springframework.cloud:spring-cloud-starter-aws-messaging')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

但是当我运行应用程序时没有发生任何事情,我是 java 和 spring boot 的新手,我做错了什么吗

4

1 回答 1

0

我只需要向 AWSSQSListner 类添加 @Component 注释,它就可以工作,也不需要 XML AWS bean

于 2018-10-01T09:04:46.977 回答