当前位置: 首页 > 知识库问答 >
问题:

spring cloud aws注释驱动队列侦听器

孟开宇
2023-03-14

我正在尝试使用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 anotation的方法,它将hello打印到控制台:

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个答案

岑明辉
2023-03-14

我只需要在AWSSQSListner类中添加@Component注释就可以了,也不需要XML AWS bean

 类似资料:
  • 我当前将我的rabbit listener注释设置为: @RabbitListener(queues=“my queue”) 是否无法从我的yaml文件中提取队列名称。我想这样做的原因是,我可以将队列更改为集成测试的测试队列,只需更改yaml文件中的队列名称即可。看来注释必须接受常量字符串,有什么办法吗?谢谢

  • 我有一个rabbitListener,它连续异步地侦听队列“用户消息”的用户消息。除非队列中加载了大量消息,否则一切都正常。当消息批量发布到队列时,同一用户的消息首先被处理,从而其他用户的消息等待轮到他们。 我无法使用优先级队列,因为所有用户的优先级都相同。所以我想创建新的队列,并在运行时监听它们。一旦消息被使用,所有队列都将是短暂的。(队列将被删除) 在浏览时,我发现可以使用RabbitAdmi

  • 在这篇文章中,加里·拉塞尔解释了如何通过编程创建多个Kafka列斯汀来聆听多个主题。。Kafka·斯普林:如何动态或在循环中创建监听器? 现在我想有一个类似的设置也适用于JMSListeners-在那里我可以有一个带有一个@JMSListener的类,并且我可以以编程方式创建该JMSListener的多个实例,每个实例都注入了自己的queueName。 我发现这篇文章Spring JMS开始根据请

  • 我试图让队列在laravel 5中工作,队列侦听器正在输出: 未定义索引:表 存在"作业"和"failed_jobs"表,config.php设置为"数据库"。 搜索laravel论坛和google都没有找到解决办法,艾米的想法去哪里找?

  • 兔子配置: 应用概述:每当gitRepository连接到我们的应用程序时,存储库名称就会成为交换名称,在这种情况下,然后存储库的每个分支都会创建自己的队列,这里有两个队列和。现在每次在开发分支中创建拉取请求时,我都需要将信息传递给开发队列,并且应该由特定的侦听器侦听,该侦听器应该仅注册用于开发。我看到了动态队列的示例,但我似乎找不到任何关于如何创建将使用不同线程执行的动态侦听器的示例,我如何实现