【问题标题】:JUnit test case for Camel route for ActiveMQActiveMQ 的骆驼路由的 JUnit 测试用例
【发布时间】:2022-02-12 22:17:10
【问题描述】:

我在 MyRouteBuilder.java 文件中有一条骆驼路线,它正在使用来自 ActiveMQ 的消息:

from("activemq:queue:myQueue" )
.process(consumeDroppedMessage)
.log(">>> I am here");

我为下面这样写了一个测试用例:

@Override
public RouteBuilder createRouteBuilder() throws Exception {
    return new MyRouteBuilder();
}

@Test
void testMyTest() throws Exception {
    String queueInputMessage = "My Msg";
    template.sendBody("activemq:queue:myQueue", queueInputMessage);
    assertMockEndpointsSatisfied();

}

当我运行单元测试用例时,我得到了这个奇怪的错误:

7:53:26.175 [main] DEBUG org.apache.camel.impl.engine.InternalRouteStartupManager - Route: route1 >>> Route[activemq://queue:null -> null]
17:53:26.175 [main] DEBUG org.apache.camel.impl.engine.InternalRouteStartupManager - Starting consumer (order: 1000) on route: route1
17:53:26.175 [main] DEBUG org.apache.camel.support.DefaultConsumer - Build consumer: Consumer[activemq://queue:null]
17:53:26.185 [main] DEBUG org.apache.camel.support.DefaultConsumer - Init consumer: Consumer[activemq://queue:null]
17:53:26.185 [main] DEBUG org.apache.camel.support.DefaultConsumer - Starting consumer: Consumer[activemq://queue:null]
17:53:26.213 [main] DEBUG org.apache.activemq.thread.TaskRunnerFactory - Initialized TaskRunnerFactory[ActiveMQ Task] using ExecutorService: java.util.concurrent.ThreadPoolExecutor@3fffff43[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
17:53:26.215 [main] DEBUG org.apache.activemq.transport.failover.FailoverTransport - Reconnect was triggered but transport is not started yet. Wait for start to connect the transport.
17:53:26.334 [main] DEBUG org.apache.activemq.transport.failover.FailoverTransport - Started unconnected
17:53:26.334 [main] DEBUG org.apache.activemq.transport.failover.FailoverTransport - Waking up reconnect task
17:53:26.335 [ActiveMQ Task-1] DEBUG org.apache.activemq.transport.failover.FailoverTransport - urlList connectionList:[tcp://localhost:61616], from: [tcp://localhost:61616]
17:53:26.339 [main] DEBUG org.apache.camel.component.jms.DefaultJmsMessageListenerContainer - Established shared JMS Connection
17:53:26.340 [main] DEBUG org.apache.camel.component.jms.DefaultJmsMessageListenerContainer - Resumed paused task: org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker@58c34bb3
17:53:26.372 [ActiveMQ Task-1] DEBUG org.apache.activemq.transport.failover.FailoverTransport - Attempting 0th connect to: tcp://localhost:61616
17:53:28.393 [ActiveMQ Task-1] DEBUG org.apache.activemq.transport.failover.FailoverTransport - Connect fail to: tcp://localhost:61616, reason: {}

看到这些消息我特别难过:

Route: route1 >>> Route[activemq://queue:null -> null]

urlList connectionList:[tcp://localhost:61616], from: [tcp://localhost:61616]

虽然我有一个正确的队列名称,但为什么队列会显示为 null?还有为什么broker url是tcp://localhost:61616

我想运行这个单元测试用例,以便它在所有环境中都能正常运行,例如:本地、DIT、SIT、PROD 等。因此,我无法承担代理 URL 为:tcp://localhost:61616

关于我在这里做错了什么以及我应该做什么有什么想法吗?

编辑 1:
我看到的问题之一是在调用测试类之前,createRouteBuilder() 中的 MyRouteBuilder() 被调用,导致我在日志中看到的问题。

【问题讨论】:

    标签: java apache-camel activemq junit5


    【解决方案1】:

    “activemq:queue:..”告诉 Camel 在幕后使用自动配置魔法(使用默认 url),而您的用例超出了此范围。

    你需要配置一个连接工厂(ActiveMQConnectionFactory)并配置一个camel-jms组件来使用那个连接工厂。

    连接工厂允许您指定 url、用户名、密码、默认连接设置和设置 SSL。

    最佳实践是将 url、用户名、密码和队列外部化到属性文件中,以便您可以跨环境(本地、DIT、SIT 和 prod 等)更改它们。

    注意:使用 org.apache.camel/camel-jms 组件,而不是 org.apache.activemq/activemq-camel 组件。 activemq-camel 在 ActiveMQ 5.17.x 中已被弃用并被删除。

    【讨论】:

    • 谢谢马特。有没有你知道的活生生的例子来帮助我。我在网上搜索了很长时间,但没有找到适合我的用例。
    • 因提及 activemq 组件而被投票赞成。
    【解决方案2】:

    我没有设置显式的活动 mq 代理,而是开始使用 VM 代理。

    @Override
        protected RoutesBuilder createRouteBuilder() throws Exception {
            return new RouteBuilder() {
                @Override
                public void configure() {
                    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
                    ActiveMQComponent activeMQComponent = new ActiveMQComponent();
                    activeMQComponent.setConnectionFactory(connectionFactory);
                    context.addComponent("activemq", activeMQComponent);
                    from("activemq:queue:myQueue").to("mock:collector");
                }
            };
        }
    

    另外,我把骆驼junit误认为是传统的junit。我们不需要显式调用实际的路由构建器类。相反,在上面设置了我的 activeMq 组件之后,我能够编写我的测试方法,模拟我的队列端点并发送消息并声明它们。骆驼真是多才多艺。不过需要大量学习。

    【讨论】:

    • 注意:您应该将 ActiveMQComponent 替换为 JMSComponent。 ActiveMQComponent 在 ActiveMQ 5.16.x 中已弃用,并在即将发布的 5.17.x 中删除
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    相关资源
    最近更新 更多