2

这个问题表明 @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;
}
4

0 回答 0