簡體   English   中英

問題測試spring cloud SQS Listener

[英]Issue testing spring cloud SQS Listener

環境

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

問題

我正在嘗試為 SQS 編寫集成測試。

我有一個在TCP/4576上運行 SQS 的本地運行localstack docker容器

在我的測試代碼中,我定義了一個 SQS 客戶端,其端點設置為本地 4576,並且可以成功連接並創建隊列、發送消息和刪除隊列。 我還可以使用 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());
    }
}

測試

@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());
        }
    }
}

在測試日志中,我看到:

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”映射到公共 void MyListener.receive(MyMsg)

其次是:

正在運行:真

活動:真

isRunningOnQueue:false

得到消息:1

這表明,在發送消息之間的 30 秒暫停中,容器沒有接收到它,當我手動輪詢消息時,它在隊列中,我可以使用它。

我的問題是,為什么不調用偵聽器,為什么isRunningOnQueue:false行暗示它不是為該隊列自動啟動的?

請注意,我還嘗試設置我自己的SimpleMessageListenerContainer bean,並將 autostart 顯式設置為 true(無論如何都是默認值)並且觀察到行為沒有變化。 我認為由@EnableSqs 設置的org.springframework.cloud.aws.messaging.config.annotation.SqsConfiguration#simpleMessageListenerContainer @EnableSqs配置一個自動啟動的SimpleMessageListenerContainer應該輪詢我的消息。

我也設置了

logging.level.org.apache.http=DEBUG
logging.level.org.springframework.cloud=DEBUG

在我的測試屬性中,可以看到 HTTP 調用創建隊列、發送消息和刪除等,但沒有接收 HTTP 調用(除了我在測試結束時的手動調用)。

經過一番修修補補,我發現了這一點。

即使簡單的消息容器工廠設置為不自動啟動,它似乎無論如何都會進行初始化,這涉及到確定隊列是否存在。

在這種情況下,隊列是在我的測試中的 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;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM