Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringCloudAWS多sqs侦听器_Java_Spring_Spring Boot_Spring Cloud_Amazon Sqs - Fatal编程技术网

Java SpringCloudAWS多sqs侦听器

Java SpringCloudAWS多sqs侦听器,java,spring,spring-boot,spring-cloud,amazon-sqs,Java,Spring,Spring Boot,Spring Cloud,Amazon Sqs,我的项目中有2个sqs侦听器。我希望其中一个具有相同的设置,另一个具有不同的设置。我要更改的唯一值是maxNumberOfMessages 最实际的方法是什么?ı希望为其中一个侦听器设置不同的maxNumberOfMessages值 这是我的配置 @Bean public AWSCredentialsProvider awsCredentialsProvider(@Value("${cloud.aws.profile}") String profile,

我的项目中有2个sqs侦听器。我希望其中一个具有相同的设置,另一个具有不同的设置。我要更改的唯一值是maxNumberOfMessages

最实际的方法是什么?ı希望为其中一个侦听器设置不同的maxNumberOfMessages值

这是我的配置

@Bean
public AWSCredentialsProvider awsCredentialsProvider(@Value("${cloud.aws.profile}") String profile,
                                                     @Value("${cloud.aws.region.static}") String region,
                                                     @Value("${cloud.aws.roleArn}") String role,
                                                     @Value("${cloud.aws.user}") String user) {
    ...

    return new AWSStaticCredentialsProvider(sessionCredentials);
}

@Bean
@Primary
@Qualifier("amazonSQSAsync")
public AmazonSQSAsync amazonSQSAsync(@Value("${cloud.aws.region.static}") String region, AWSCredentialsProvider awsCredentialsProvider) {
    return AmazonSQSAsyncClientBuilder.standard()
            .withCredentials(awsCredentialsProvider)
            .withRegion(region)
            .build();
}

@Bean
@Primary
public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(AmazonSQSAsync amazonSqs) {
    SimpleMessageListenerContainerFactory factory = new SimpleMessageListenerContainerFactory();
    factory.setAmazonSqs(amazonSqs);
    factory.setMaxNumberOfMessages(1);
    factory.setWaitTimeOut(10);
    factory.setQueueMessageHandler(new SqsQueueMessageHandler());
    return factory;
}
这是听众

@SqsListener(value = "${messaging.queue.blabla.source}", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
public void listen(Message message, Acknowledgment acknowledgment, @Header("MessageId") String messageId) {
    log.info("Message Received");

    try {
        ....
        acknowledgment.acknowledge().get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage());
    }
}

ı找到了解决方案,并在github上分享了回购示例。


如果在侦听器类上添加@enablesync注释,并在处理程序方法中添加@Async注释,我的问题就解决了:)

下面的方法对我有效(如果每个侦听器侦听不同的队列)


不幸的是,Sushant的解决方案没有在Kotlin中为我编译(因为QueueAttributes是静态保护类),但我使用它编写了以下内容:

    @Bean
fun simpleMessageListenerContainerFactory(sqs: AmazonSQSAsync): SimpleMessageListenerContainerFactory =
    object : SimpleMessageListenerContainerFactory() {
        override fun createSimpleMessageListenerContainer(): SimpleMessageListenerContainer {
            val container = object : SimpleMessageListenerContainer() {
                override fun afterPropertiesSet() {
                    super.afterPropertiesSet()
                    registeredQueues.forEach { (queue, attributes) ->
                        if (queue.contains(QUEUE_NAME)) {
                            FieldUtils.writeField(
                                attributes,
                                "maxNumberOfMessages",
                                NEW_MAX_NUMBER_OF_MESSAGES,
                                true
                            )
                        }
                    }
                }
            }

            container.setWaitTimeOut(waitTimeOut)
            container.setMaxNumberOfMessages(maxNumberOfMessages)
            container.setAmazonSqs(sqs)
            return container
        }
    }

你能进一步解释一下你是如何解决这个问题的吗?github链接不清楚你是如何在监听器上配置不同的max消息的。这对我来说是不可编译的,因为QueueAttributes是受保护的静态类。
    @Bean
fun simpleMessageListenerContainerFactory(sqs: AmazonSQSAsync): SimpleMessageListenerContainerFactory =
    object : SimpleMessageListenerContainerFactory() {
        override fun createSimpleMessageListenerContainer(): SimpleMessageListenerContainer {
            val container = object : SimpleMessageListenerContainer() {
                override fun afterPropertiesSet() {
                    super.afterPropertiesSet()
                    registeredQueues.forEach { (queue, attributes) ->
                        if (queue.contains(QUEUE_NAME)) {
                            FieldUtils.writeField(
                                attributes,
                                "maxNumberOfMessages",
                                NEW_MAX_NUMBER_OF_MESSAGES,
                                true
                            )
                        }
                    }
                }
            }

            container.setWaitTimeOut(waitTimeOut)
            container.setMaxNumberOfMessages(maxNumberOfMessages)
            container.setAmazonSqs(sqs)
            return container
        }
    }