问题测试 spring cloud SQS 监听器

Dav*_*vid 5 spring-cloud spring-cloud-aws

环境

  • Spring Boot:1.5.13.RELEASE
  • 云:Edgware.SR3
  • 云 AWS:1.2.2.RELEASE
  • 爪哇 8
  • OSX 10.13.4

问题

我正在尝试为 SQS 编写集成测试。

我有一个本地运行的localstack docker容器,其中运行 SQSTCP/4576

在我的测试代码中,我定义了一个端点设置为本地 4576 的 SQS 客户端,并且可以成功连接并创建队列、发送消息和删除队列。我还可以使用 SQS 客户端来接收消息并提取我发送的消息。

我的问题是,如果我删除手动接收消息的代码以允许另一个组件获取消息,似乎什么也没有发生。我有一个弹簧组件注释如下:

听众

@Component
public class MyListener {
@SqsListener(value = "my_queue", deletionPolicy = ON_SUCCESS)
    public void receive(final MyMsg msg) {
        System.out.println("GOT THE MESSAGE: "+ msg.toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

测试

@RunWith(SpringRunner.class)
@SpringBootTest(properties = "spring.profiles.active=test")
public class MyTest {

    @Autowired
    private AmazonSQSAsync amazonSQS;

    @Autowired
    private SimpleMessageListenerContainer container;

    private String queueUrl;

    @Before
    public void setUp() {
        queueUrl = amazonSQS.createQueue("my_queue").getQueueUrl();
    }

    @After
    public void tearDown() {
        amazonSQS.deleteQueue(queueUrl);
    }

    @Test
    public void name() throws InterruptedException {
        amazonSQS.sendMessage(new SendMessageRequest(queueUrl, "hello"));
        System.out.println("isRunning:" + container.isRunning());
        System.out.println("isActive:" + container.isActive());
        System.out.println("isRunningOnQueue:" + container.isRunning("my_queue"));
        Thread.sleep(30_000);
        System.out.println("GOT MESSAGE: " + amazonSQS.receiveMessage(queueUrl).getMessages().size());
    }

    @TestConfiguration
    @EnableSqs
    public static class SQSConfiguration {

        @Primary
        @Bean(destroyMethod = "shutdown")
        public AmazonSQSAsync amazonSQS() {
            final AwsClientBuilder.EndpointConfiguration endpoint = new AwsClientBuilder.EndpointConfiguration("http://127.0.0.1:4576", "eu-west-1");
            return new AmazonSQSBufferedAsyncClient(AmazonSQSAsyncClientBuilder
                    .standard()
                    .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("key", "secret")))
                    .withEndpointConfiguration(endpoint)
                    .build());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在测试日志中,我看到:

oscamlistener.QueueMessageHandler :在类 MyListener 上找到的 1 个消息处理程序方法:{public void MyListener.receive(MyMsg)=org.springframework.cloud.aws.messaging.listener.QueueMessageHandler$MappingInformation@1cd4082a} 2018-05-31 22:50: 39.582 信息 16329 ---

oscamlistener.QueueMessageHandler :将“org.springframework.cloud.aws.messaging.listener.QueueMessageHandler$MappingInformation@1cd4082a”映射到 public void MyListener.receive(MyMsg)

其次是:

正在运行:真

是活动的:真

isRunningOnQueue:false

收到消息:1

这表明在发送消息之间的 30 秒暂停中,容器没有接收它,当我手动轮询消息时,它在队列中,我可以使用它。

我的问题是,为什么没有调用侦听器,为什么该isRunningOnQueue:false行表明它不是为该队列自动启动的?

请注意,我还尝试将我自己的SimpleMessageListenerContainerbean 设置为 autostart 显式设置为 true(无论如何都是默认值),并且没有观察到行为的变化。我认为org.springframework.cloud.aws.messaging.config.annotation.SqsConfiguration#simpleMessageListenerContainer设置的@EnableSqs应该配置一个自动启动的SimpleMessageListenerContainer,应该轮询我的消息。

我也设置了

logging.level.org.apache.http=DEBUG
logging.level.org.springframework.cloud=DEBUG
Run Code Online (Sandbox Code Playgroud)

在我的测试属性中,可以看到 HTTP 调用创建队列、发送消息和删除等,但没有要接收的 HTTP 调用(除了我在测试结束时的手动调用)。

Dav*_*vid 8

经过一番修补后我发现了这一点。

即使简单消息容器工厂设置为不自动启动,它似乎仍然会进行初始化,这涉及确定队列是否存在。

在这种情况下,队列是在我的测试中的 setup 方法中创建的 - 但遗憾的是这是在 spring 上下文设置之后,这意味着发生了异常。

我通过简单地将队列创建移至 SQS 客户端的上下文创建(这发生在创建消息容器之前)来修复此问题。IE:

@Bean(destroyMethod = "shutdown")
        public AmazonSQSAsync amazonSQS() {
            final AwsClientBuilder.EndpointConfiguration endpoint = new AwsClientBuilder.EndpointConfiguration("http://localhost:4576", "eu-west-1");
            final AmazonSQSBufferedAsyncClient client = new AmazonSQSBufferedAsyncClient(AmazonSQSAsyncClientBuilder
                    .standard()
                    .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("dummyKey", "dummySecret")))
                    .withEndpointConfiguration(endpoint)
                    .build());
            client.createQueue("test-queue");
            return client;
        }
Run Code Online (Sandbox Code Playgroud)