代码之家  ›  专栏  ›  技术社区  ›  Zaid Direya

spring云aws注解驱动的队列侦听器

  •  0
  • Zaid Direya  · 技术社区  · 6 年前

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

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

    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和SpringBoot还不熟悉,有什么我做错的吗

    1 回复  |  直到 6 年前
        1
  •  0
  •   Zaid Direya    6 年前

    我只需要将@Component注释添加到AWSSQSListner类中,它就可以工作了,也不需要XML awsbean