如何正确测试ActiveMQ主题发布

时间:2018-12-19 08:34:08

标签: activemq

我正在努力测试我的代码是否正确发布了有关该主题的消息。我知道我可以JMeter或其他任何方式;但我想在代码库中进行测试,以针对嵌入式代理运行。最初,它是一个基于Spring的解决方案,但是为了简化所有操作,我提供了一个最小的可重现解决方案here

代码的要旨是:

    @Test
public void testPublisherService() {
    final String payload = "test-dummy-message";
    JmsTemplate destinationTemplate = buildJmsTemplate();
    JmsTemplate sourceTemplate = buildJmsTemplate();
    sourceTemplate.setReceiveTimeout(10000);
    JmsPublisherService service = new JmsPublisherService(destinationTemplate);

    log.info("Going to publish message");
    service.send(TOPIC_NAME, payload);
    log.info("Message is published");

    log.info("Going to retrieve message");
    Object retrieved = sourceTemplate.receiveAndConvert(new ActiveMQTopic(TOPIC_NAME));
    log.info("Message is retrieved");

    Assert.assertNotNull(retrieved);
}

,它失败了!即总是从该主题收到null!我为ActiveMQ添加了DEBUG日志记录,它也没有太大帮助:

2018-12-19 10:17:37.393  INFO   --- [main] ru.mkv.jms.JmsPublisherIntTest           : Message is published
2018-12-19 10:17:37.393  INFO   --- [main] ru.mkv.jms.JmsPublisherIntTest           : Going to retrieve message
2018-12-19 10:17:37.393 DEBUG   --- [ActiveMQ VMTransport: vm://test_broker#1-1] o.a.a.broker.jmx.ManagementContext       : Unregistering MBean org.apache.activemq:type=Broker,brokerName=test_broker,destinationType=Topic,destinationName=test_topic,endpoint=Producer,clientId=clientId4.74703049017198,producerId=ID_EEW00163-52272-1545207456632-4_1_1_1
2018-12-19 10:17:37.393 DEBUG   --- [ActiveMQ VMTransport: vm://test_broker#1-1] o.a.activemq.broker.TransportConnector   : Publishing: vm://test_broker for broker transport URI: vm://test_broker
2018-12-19 10:17:37.393 DEBUG   --- [ActiveMQ VMTransport: vm://test_broker#1-1] o.a.activemq.broker.TransportConnector   : Publishing: vm://test_broker for broker transport URI: vm://test_broker
2018-12-19 10:17:37.399 DEBUG   --- [ActiveMQ VMTransport: vm://test_broker#1-2] o.a.a.broker.region.AbstractRegion       : test_broker adding consumer: ID:EEW00163-52272-1545207456632-4:1:2:1 for destination: topic://test_topic
2018-12-19 10:17:37.400 DEBUG   --- [ActiveMQ VMTransport: vm://test_broker#1-2] o.a.activemq.broker.TransportConnector   : Publishing: vm://test_broker for broker transport URI: vm://test_broker
2018-12-19 10:17:37.400 DEBUG   --- [ActiveMQ VMTransport: vm://test_broker#1-2] o.a.activemq.broker.TransportConnector   : Publishing: vm://test_broker for broker transport URI: vm://test_broker
2018-12-19 10:17:37.400 DEBUG   --- [ActiveMQ VMTransport: vm://test_broker#1-2] o.a.a.broker.region.AbstractRegion       : test_broker adding destination: topic://ActiveMQ.Advisory.Consumer.Topic.test_topic
2018-12-19 10:17:37.402 DEBUG   --- [main] o.a.activemq.thread.TaskRunnerFactory    : Initialized TaskRunnerFactory[ActiveMQ Session Task] using ExecutorService: java.util.concurrent.ThreadPoolExecutor@3e27ba32[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]

2018-12-19 10:17:47.405 DEBUG   --- [main] o.a.activemq.ActiveMQMessageConsumer     : remove: ID:EEW00163-52272-1545207456632-4:1:2:1, lastDeliveredSequenceId: -1
2018-12-19 10:17:47.406 DEBUG   --- [ActiveMQ VMTransport: vm://test_broker#1-2] o.a.a.broker.jmx.ManagementContext       : Unregistering MBean org.apache.activemq:type=Broker,brokerName=test_broker,destinationType=Topic,destinationName=test_topic,endpoint=Consumer,clientId=clientId4.74703049017198,consumerId=ID_EEW00163-52272-1545207456632-4_1_2_1
2018-12-19 10:17:47.407 DEBUG   --- [ActiveMQ VMTransport: vm://test_broker#1-2] o.a.a.broker.region.AbstractRegion       : test_broker removing consumer: ID:EEW00163-52272-1545207456632-4:1:2:1 for destination: topic://test_topic
2018-12-19 10:17:47.407  INFO   --- [main] ru.mkv.jms.JmsPublisherIntTest           : Message is retrieved
2018-12-19 10:17:47.408  INFO   --- [main] o.apache.activemq.broker.BrokerService   : Apache ActiveMQ 5.15.3 (test_broker, ID:EEW00163-52272-1545207456632-0:1) is shutting down

我怀疑这与我不太了解的连接/经纪人生命周期有关,或者可能是检索方法。顺便说一下,ActiveMQ文档的范围似乎非常有限,我无法找到有关其实际工作方式的任何深入文档。

1 个答案:

答案 0 :(得分:1)

这是预期的行为。您正在向任何主题存在之前发送消息到主题。发送到没有订阅的主题的消息将被简单地丢弃。这是主题的工作方式。您需要创建一个订阅,发送消息,然后从订阅中检索消息。除此之外,您可以切换为使用队列而不是主题。如果您这样做的话,我希望您的测试能够顺利进行。

此外,您可能会考虑仅使用JMS API而不是在JMS API之上使用Spring,因为它可能更简单。