【问题标题】:How to debug Camel Route builder in eclipse IDE?如何在 Eclipse IDE 中调试 Camel Route 构建器?
【发布时间】:2015-07-23 08:31:42
【问题描述】:

我已经用 Java DSL 编写了 Camel Route 构建,现在我想在 Eclipse IDE 中调试它,我的类看起来像



public class PMRouteBuilder extends RouteBuilder {
UserProfileResponseProcessor responseProcessor=new UserProfileResponseProcessor();
         System.out.println("\n");
         System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
         System.out.println("           STARTED PROCESS MANAGER ROUTEBUILDER                          ");
         System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
try{
             from("cxf:bean:process-manager-ws?dataFormat=POJO").routeId("process-manager-route-userprofile").log( "This is ${in.header.operationName} operation called...." )
                    .log( "Entering inside the Choice with operation....${in.header.operationName}")
                    //.wireTap(RouterEndPoints.ENDPOINT_AUDITOR_QUEUE.value(),true, new PreWireTapProcessor())
                    .choice()
                        /**
                         * ##################################### ROUTE FOR USER PROFILE REQUEST ###########################################
                         */
                        .when(simple("${in.header.operationName} == 'retrieveUserProfile'"))
                            .to("log:?showAll=true&multiline=true")
                            .setHeader("OPERATION_NAME", constant("retrieveUserProfile") )
                            .process(pmRequestProcessor)
                            .log( "Setting header value to...."+constant(AuditActions.Actions.ACTION_GET_USER_PROFILE.desc()) )
                            .setHeader(RouteActions.Actions.OMGMEAT_ACTION_ID.desc(), constant(AuditActions.Actions.ACTION_GET_USER_PROFILE.desc())).convertBodyTo(UserProfile.class)
                            .to(RouterEndPoints.ENDPOINT_USERPROFILE_QUEUE.value()).process(responseProcessor)
                        .when(simple("${in.header.operationName} == 'addUserProfile'"))
                            .log( "Setting header value to...."+constant(AuditActions.Actions.ACTION_ADD_PROFILE.desc()) )
                            .setHeader(RouteActions.Actions.OMGMEAT_ACTION_ID.desc(), constant(AuditActions.Actions.ACTION_ADD_PROFILE.desc())).convertBodyTo(UserProfile.class)
                            .to(RouterEndPoints.ENDPOINT_USERPROFILE_QUEUE.value()).process(responseProcessor)
.end()
 }catch(Exception exc){
             ApplicationLogger.error("PMRouteBuilder.configure():Exception while configure the route for 'cxf:bean:process-manager-ws?dataFormat=POJO'",exc);
         }

我可以看到正在打印日志,但有什么方法可以放置调试点(断点)并调试此 Route 构建器?

【问题讨论】:

标签: debugging apache-camel


【解决方案1】:

除了 Claus 提供的链接中描述的技术外,我们还将 Hawtio 控制台与 Camel 插件一起使用。

使用此插件,您可以:

  • 所有正在运行的 Camel 应用程序列表
  • 每个 Camel Context 的详细信息,例如 Camel 版本号、运行时静态信息
  • 每个 Camel 应用程序中所有路由的列表及其运行时统计信息
  • 管理所有 Camel 应用程序及其路由的生命周期,以便您可以重启/停止/暂停/恢复等。
  • 运行路线的图形表示以及实时指标
  • 运行路线的实时跟踪和调试
  • 使用实时运行时静态分析运行路线;每个处理器详细说明
  • 浏览消息并将消息发送到 Camel 端点

我知道你要求使用 Eclipse,但我认为现在无法逐步调试 DSL,这就是为什么我们主要使用 Tracer 启用模式,最后使用 Hawtio 控制台进行一步 -逐步调试。

【讨论】:

  • 我们能有任何可以与 Eclipse IDE 集成的插件吗?
【解决方案2】:

另一种技术是使用 IDE 的 JUnit,但你应该稍微修改你的类以更好地测试:

使用端点的属性,例如更改

from("cxf:bean:process-manager-ws?dataFormat=POJO")
...
.to(RouterEndPoints.ENDPOINT_USERPROFILE_QUEUE.value()) // the two instances 

from("{{from.endpoint}}")
...
.to("{{user.profile.endpoint.1}}")
...
.to("{{user.profile.endpoint.2}}")

并使用 spring 或 bluprint 文件中的原始值设置属性(取决于您使用哪个)。

之后,您可以在名为 PMRouteBuilderTest 的测试文件夹(如果使用 maven,则为 src/test)中创建一个测试,扩展 CamelTestSupport 并具有以下内容:

@EndpointInject(uri = "direct:test")
protected Endpoint inputTest;

@EndpointInject(uri = "mock:userEndpointOne")
protected MockEndpoint destinationOne;

@EndpointInject(uri = "mock:userEndpointTwo")
protected MockEndpoint destinationTwo;

@Test
public void testRoutingSampleToDestOne() throws Exception {

    destinationOne.expectedMessageCount(1);
    destinationTewo.expectedMessageCount(1);

    String body = "Anything that can make your test useful"
    sendBody(inputTest.getEndpointUri(), body);
    assertMockEndpointsSatisfied();
}

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new PMRouteBuilder();
}

@Override
protected Properties useOverridePropertiesWithPropertiesComponent() {
    Properties props = new Properties();

    // Set your test properties here, those are examples
    props.put("from.endpoint", "direct:test");
    props.put("user.profile.endpoint.1", "mock:userEndpointOne");
    props.put("user.profile.endpoint.2", "mock:userEndpointTwo");

    return props;
}

您必须让您的测试尽可能地使用真正的 bean,但有时当您做不到时,您必须使用 Mockito 之类的模拟框架来模拟方法调用。 之后,您可以在 IDE 中以调试模式执行测试,并将断点放置到您在路由中使用的真实处理器。

我强烈推荐阅读this article about Camel testing

为了简单起见,我在测试类名称后面加上了 Test,但通常它应该命名为 PMRouteBuilderIT,因为它测试多个类并且应该在集成测试阶段执行(mvn verify,带有故障安全插件)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多