【问题标题】:spring core aws messaging Notification template not binding the message properlyspring core aws消息通知模板未正确绑定消息
【发布时间】:2020-05-20 19:49:25
【问题描述】:

我正在使用 spring-boot-2.2.1 以及 AWS SNS 和 SQS。我正在从 SNS 向 SQS 发布消息。我可以使用 String 接收消息,但 @NotificationMessage 返回 null 而不是消息正文。当我使用 spring-boot-1.5.19 时,相同的代码运行良好

pom.xml

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-aws-messaging</artifactId>
        </dependency>

主文件

        @SpringBootApplication
public class AwsApplication {

    public static void main(String[] args) {
        SpringApplication.run(AwsApplication.class, args);
    }

    @Bean
    public QueueMessagingTemplate queueMessagingTemplate(AmazonSQSAsync amazonSqs) {
        return new QueueMessagingTemplate(amazonSqs);
    }

    @Bean
    public NotificationMessagingTemplate notificationMessagingTemplate(
            AmazonSNS amazonSNS) {
        return new NotificationMessagingTemplate(amazonSNS);
    }

    @Bean
    public IotClient getIotClient() {
        return IotClient.builder()
                .credentialsProvider(() -> AwsBasicCredentials.create(AWS_ACCESSKEY, AWS_SECRETKEY))
                .credentialsProvider(() -> InstanceProfileCredentialsProvider.create().resolveCredentials())
                .region(Region.US_EAST_2)
                .build();
    }
   }

SQS 监听类

@SqsListener(value = "${sqs.consumer.name}")
public void receiveSnsSqs(String message, @NotificationMessage Employee employee) {
    System.out.println("SNS Consumer received the message::"+message);
    System.out.println("SNS Consumer received the notificationMessage::"+employee);
    }

在上面的侦听器类中,我能够接收到消息,但员工却是 null。我直接从 AWS SNS 发送消息。

正如我提到的,它在将 Spring 版本迁移到 2.2.1 版本后停止工作之前工作正常。

任何帮助将不胜感激。

【问题讨论】:

    标签: java spring spring-boot amazon-sqs amazon-sns


    【解决方案1】:

    我使用的是2.3.0.RELEASE spring 版本,但遇到了同样的问题。 在我把MappingJackson2MessageConverter 放到QueueMessageHandlerFactory 之后,问题就解决了。 这是我的 QueueMessageHandlerFactory bean。

      @Bean
      public QueueMessageHandlerFactory queueMessageHandlerFactory() {
        MappingJackson2MessageConverter messageConverter = new MappingJackson2MessageConverter();
        messageConverter.setStrictContentTypeMatch(false);
        messageConverter.getObjectMapper().registerModule(new JavaTimeModule());
    
    
        QueueMessageHandlerFactory factory = new QueueMessageHandlerFactory();
        factory.setMessageConverters(Collections.singletonList(messageConverter));
        return factory;
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 2021-08-20
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多