3

我无法使用 Spring Boot 和 SQS 制作工作队列侦听器(消息已发送并出现在 SQS ui 中)

@MessageMapping@SqsListener工作

Java:11
Spring Boot:2.1.7
依赖:spring-cloud-aws-messaging

这是我的配置

@Configuration
@EnableSqs
public class SqsConfig {

    @Value("#{'${env.name:DEV}'}")
    private String envName;

    @Value("${cloud.aws.region.static}")
    private String region;

    @Value("${cloud.aws.credentials.access-key}")
    private String awsAccessKey;

    @Value("${cloud.aws.credentials.secret-key}")
    private String awsSecretKey;

    @Bean
    public Headers headers() {
        return new Headers();
    }

    @Bean
    public MessageQueue queueMessagingSqs(Headers headers,
                                          QueueMessagingTemplate queueMessagingTemplate) {
        Sqs queue = new Sqs();
        queue.setQueueMessagingTemplate(queueMessagingTemplate);
        queue.setHeaders(headers);
        return queue;
    }

    private ResourceIdResolver getResourceIdResolver() {
        return queueName -> envName + "-" + queueName;
    }

    @Bean
    public DestinationResolver destinationResolver(AmazonSQSAsync amazonSQSAsync) {
        DynamicQueueUrlDestinationResolver destinationResolver = new DynamicQueueUrlDestinationResolver(
                amazonSQSAsync,
                getResourceIdResolver());
        destinationResolver.setAutoCreate(true);
        return destinationResolver;
    }

    @Bean
    public QueueMessagingTemplate queueMessagingTemplate(AmazonSQSAsync amazonSQSAsync,
                                                         DestinationResolver destinationResolver) {
        return new QueueMessagingTemplate(amazonSQSAsync, destinationResolver, null);
    }

    @Bean
    public QueueMessageHandlerFactory queueMessageHandlerFactory() {
        QueueMessageHandlerFactory factory = new QueueMessageHandlerFactory();
        MappingJackson2MessageConverter messageConverter = new MappingJackson2MessageConverter();
        messageConverter.setStrictContentTypeMatch(false);
        factory.setArgumentResolvers(Collections.singletonList(new PayloadArgumentResolver(messageConverter)));
        return factory;
    }

    @Bean
    public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(AmazonSQSAsync amazonSqs) {
        SimpleMessageListenerContainerFactory factory = new SimpleMessageListenerContainerFactory();
        factory.setAmazonSqs(amazonSqs);
        factory.setMaxNumberOfMessages(10);
        factory.setWaitTimeOut(2);
        return factory;
    }

}

我也注意到org.springframework.cloud.aws.messaging.config.SimpleMessageListenerContainerFactoryorg.springframework.cloud.aws.messaging.config.annotation.SqsConfiguration在启动时运行

还有我的测试

@RunWith(SpringJUnit4ClassRunner.class)
public class ListenTest {

    @Autowired
    private MessageQueue queue;

    private final String queueName = "test-queue-receive";

    private String result = null;

    @Test
    public void test_listen() {
        // given
        String data = "abc";

        // when
        queue.send(queueName, data).join();

        // then
        Awaitility.await()
                .atMost(10, TimeUnit.SECONDS)
                .until(() -> Objects.nonNull(result));

        Assertions.assertThat(result).equals(data);
    }

    @MessageMapping(value = queueName)
    public void receive(String data) {
        this.result = data;
    }
}

你觉得有什么不对吗?

我为示例创建了一个存储库:(https://github.com/mmaryo/java-sqs-test
在测试文件夹中,更改 'application.yml' 中的 aws 凭据,
然后运行测试

4

2 回答 2

4

我在使用 spring-cloud-aws-messaging 包时遇到了同样的问题,但后来我在 @SqsListener 注释中使用了队列 URL 而不是队列名称,并且它起作用了。

@SqsListener(value = { "https://full-queue-URL" }, deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
public void receive(String message) {
     // do something
}

使用 spring-cloud-starter-aws-messaging 包时,您似乎可以使用队列名称。如果您不想使用入门包,我相信有一些配置允许使用队列名称而不是 URL。

编辑:尽管我在属性文件中列出了 us-east-1,但我注意到该区域默认为 us-west-2。然后我创建了一个 RegionProvider bean 并将区域设置为 us-east-1 ,现在当我在 @SqsMessaging 中使用队列名称时,它被找到并正确解析为框架代码中的 URL。

于 2019-09-19T15:38:09.740 回答
0

您需要利用 @Primary 注释,这对我有用:

@Autowired(required = false)
private AWSCredentialsProvider awsCredentialsProvider;

@Autowired
private AppConfig appConfig;

@Bean
public QueueMessagingTemplate getQueueMessagingTemplate() {
    return new QueueMessagingTemplate(sqsClient());
}

@Primary
@Bean
public AmazonSQSAsync sqsClient() {
    AmazonSQSAsyncClientBuilder builder = AmazonSQSAsyncClientBuilder.standard();

    if (this.awsCredentialsProvider != null) {
        builder.withCredentials(this.awsCredentialsProvider);
    }

    if (appConfig.getSqsRegion() != null) {
        builder.withRegion(appConfig.getSqsRegion());
    } else {
        builder.withRegion(Regions.DEFAULT_REGION);
    }

    return builder.build();
}

build.gradle 需要这些部门:

implementation("org.springframework.cloud:spring-cloud-starter-aws:2.2.0.RELEASE")
implementation("org.springframework.cloud:spring-cloud-aws-messaging:2.2.0.RELEASE")
于 2019-12-17T21:27:42.427 回答