这个问题表明@SendTo注释支持属性占位符,但我不能得到它的工作。 下面是我想要做的(不是试图用言语来解释比较容易)一些简单的代码片段。 我在春天,云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;
}