This issue表明@SendTo注释支持属性占位符,但我无法使其工作。这是我尝试做的一些简化的代码片段(比试图用文字解释更容易)。我在spring-cloud-aws版本1.2.1上。
这有效:
@Component
public class InputQueueListener {
@Value("${replyQueueProperty}")
private String replyQueue;
@Autowired
private QueueMessagingTemplate messagingTemplate;
@SqsListener(value = "${inputQueueProperty}", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
private void receiveMessage(final Message message, final Acknowledgment acknowledgment, @Header("ApproximateReceiveCount") final int receiveCount) throws Exception {
final Reply reply = doStuff(message);
messagingTemplate.convertAndSend(replyQueue, reply);
}
}
这有效:
@Component
public class InputQueueListener {
@SqsListener(value = "${inputQueueProperty}", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
@SendTo("replyQueueActualName")
private Reply receiveMessage(final Message message, final Acknowledgment acknowledgment, @Header("ApproximateReceiveCount") final int receiveCount) throws Exception {
final Reply reply = doStuff(message);
return reply;
}
}
但这不起作用:
@Component
public class InputQueueListener {
@SqsListener(value = "${inputQueueProperty}", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
@SendTo("${replyQueueProperty}")
private Reply receiveMessage(final Message message, final Acknowledgment acknowledgment, @Header("ApproximateReceiveCount") final int receiveCount) throws Exception {
final Reply reply = doStuff(message);
return reply;
}
}
此失败并出现NonExistentQueue异常。但队列存在,前两个方法发送消息就好了。 我错过了什么?!我已经检查了一百万次拼写错误,我很确定不是这样:)
以防万一,这是我的配置:
@Bean
public QueueMessagingTemplate queueMessagingTemplate(final AmazonSQSAsync amazonSqs, final ResourceIdResolver resourceIdResolver) {
final QueueMessagingTemplate queueMessagingTemplate = new QueueMessagingTemplate(amazonSqs, resourceIdResolver);
return queueMessagingTemplate;
}
@Lazy
@Bean(name = "amazonSQS", destroyMethod = "shutdown")
public AmazonSQSAsync amazonSQSClient() {
final AmazonSQSAsync awsSQSAsyncClient;
awsSQSAsyncClient = AmazonSQSAsyncClientBuilder
.standard()
.withRegion(Regions.fromName(System.getProperty("cloud.aws.region.static")))
.withCredentials(new DefaultAWSCredentialsProviderChain())
.build();
return awsSQSAsyncClient;
}
@Bean
public QueueMessageHandler queueMessageHandler(final AmazonSQSAsync amazonSqs) {
final QueueMessageHandlerFactory queueMsgHandlerFactory = new QueueMessageHandlerFactory();
queueMsgHandlerFactory.setAmazonSqs(amazonSqs);
final QueueMessageHandler queueMessageHandler = queueMsgHandlerFactory.createQueueMessageHandler();
return queueMessageHandler;
}
@Bean
public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(final AmazonSQSAsync amazonSqs) {
final SimpleMessageListenerContainerFactory msgListenerContainerFactory = new SimpleMessageListenerContainerFactory();
msgListenerContainerFactory.setAmazonSqs(amazonSqs);
msgListenerContainerFactory.setMaxNumberOfMessages(5);
msgListenerContainerFactory.setWaitTimeOut(20);
return msgListenerContainerFactory;
}
@Bean
public SimpleMessageListenerContainer simpleMessageListenerContainer(final QueueMessageHandler messageHandler, final SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory) {
final SimpleMessageListenerContainer msgListenerContainer = simpleMessageListenerContainerFactory.createSimpleMessageListenerContainer();
msgListenerContainer.setMessageHandler(messageHandler);
return msgListenerContainer;
}
@Bean
public DynamicQueueUrlDestinationResolver destinationResolver(final AmazonSQSAsync amazonSqs, final ResourceIdResolver resourceIdResolver) {
DynamicQueueUrlDestinationResolver destinationResolver = new DynamicQueueUrlDestinationResolver(amazonSqs, resourceIdResolver);
return destinationResolver;
}
@Bean
public QueueMessageHandlerFactory queueMessageHandlerFactory(final AmazonSQSAsync amazonSqs) {
QueueMessageHandlerFactory factory = new QueueMessageHandlerFactory();
factory.setAmazonSqs(amazonSqs);
return factory;
}