给aws sqs发队列消息
2019-03-13 本文已影响0人
牧笛2017
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 );
}
}