美文网首页
给aws sqs发队列消息

给aws sqs发队列消息

作者: 牧笛2017 | 来源:发表于2019-03-13 23:21 被阅读0次

    1.引入包:

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

    2.在配置文件yml中定义队列的名字,并且定义一个参数类,在这个类中定义priceChangedSqs为aws里的队列名字。

    3.配置一个QueueMessagingTemplate类引用这个sqs队列

    @Configuration
    public class ApplicationConfiguration {
    
        private final PricingProperties pricingProperties;
    
        @Autowired
        public ApplicationConfiguration(
                        final PricingProperties pricingProperties ) {
            this.pricingProperties = pricingProperties;
        }
    
        @Bean
        public QueueMessagingTemplate queueMessagingTemplate( final AmazonSQSAsync amazonSQSAsync,
                                                              final ResourceIdResolver resourceIdResolver ) {
            final QueueMessagingTemplate queueMessagingTemplate = new QueueMessagingTemplate( amazonSQSAsync, resourceIdResolver );
            queueMessagingTemplate.setDefaultDestinationName( pricingProperties.getPriceChangedSqs() ); // 通过第二步,获取yml里的队列名
    
            return queueMessagingTemplate;
        }
    
    }
    

    4.使用 queueMessagingTemplate 发送消息

    public class PriceSendToSqs {
        private final QueueMessagingTemplate queueMessagingTemplate;
        public void sendToSqs(  SupplierArticlePrice price ) {
          ExchangeableArticlePrice exchangeableArticlePrice = new ExchangeableArticlePrice( price );
                queueMessagingTemplate.convertAndSend( exchangeableArticlePrice );
            }
    }
    

    相关文章

      网友评论

          本文标题:给aws sqs发队列消息

          本文链接:https://www.haomeiwen.com/subject/qgaxmqtx.html