给aws sqs发队列消息

1.引入包:

        
            org.springframework.cloud
            spring-cloud-starter-aws-messaging
        

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发队列消息)