Spring Cloud AWS注释驱动的队列侦听器

时间:2018-09-22 20:42:49

标签: spring spring-cloud spring-cloud-aws

我正在尝试编写一个使用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的新手,我做错了什么吗

1 个答案:

答案 0 :(得分:0)

我只需要向AWSSQSListner类添加@Component注释即可,并且可以正常工作,也不需要XML AWS Bean