apache camel RouteBuilder在从url传递header / param名称时不起作用

时间:2017-01-11 15:24:03

标签: java apache-camel dsl servlet-3.0

我正在观看有关Servlet的骆驼示例,它适用于此xml camel定义(camel-context.xml):

 <camelContext xmlns="http://camel.apache.org/schema/spring">

    <route id="helloRoute">
      <!-- incoming requests from the servlet is routed -->
      <from uri="servlet:hello"/>
      <choice>
        <when>
          <!-- is there a header with the key name? -->
          <header>name</header>
          <!-- yes so return back a message to the user -->
          <transform>
            <simple>Hi I am ${sysenv.HOSTNAME}. Hello ${header.name} how are you today? ****</simple>
          </transform>
        </when>
        <otherwise>
          <!-- if no name parameter then output a syntax to the user -->
          <transform>
            <constant>Add a name parameter to uri, eg ?name=foo</constant>
          </transform>
        </otherwise>
      </choice>
    </route>

它适用于uri / hello和/ hello?name = foo。我正在尝试用Java dsl替换xml dsl,就像这样(当Web应用程序停止时,当Servlet上下文启动和停止时,驼峰上下文启动一次):

@WebListener
public class CamelRoutingInitializer implements ServletContextListener {

    DefaultCamelContext camctx;
    ServletContext ctx;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ctx = sce.getServletContext();
        RouteBuilder builder = new RouteBuilder() {
            /**
             *like camel-config.xml but with Java DSL syntax 
             * @see http://camel.apache.org/java-dsl.html
             */
            @Override
            public void configure() throws Exception {
                from("servlet:camel")
                .choice()
                .when(header("name").isEqualTo("name"))
                .transform(body().append("Hi I am ${sysenv.HOSTNAME}. Hello ${header.name} how are you today? ****"))
                .otherwise()
                .transform(body().append("Add a name parameter to uri, eg ?name=yourname"));    
                ctx.log("** Route config ok");
            }
        };
        DefaultCamelContext camctx = new DefaultCamelContext();
        try {
            camctx.addRoutes(builder);
            camctx.start();
            System.out.println("** CAMEL STARTED...");
            ctx.log("** CAMEL STARTED...");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        if (camctx!=null && camctx.isStarted()){
            try {
                camctx.stop();
                ctx.log("** CAMEL STOPPED...");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

如果uri是/ camel我得到“为uri添加名称参数,例如?name = yourname”但是 同样的事情发生在使用“/ camel?name = foo”(而不是“你好我是xxx你好foo今天你好吗?****”)

它出了什么问题? webapplication使用两者(camel-config.xml和CamelRoutingInitializer类)。

感谢

拉​​吉

1 个答案:

答案 0 :(得分:2)

我认为您的问题就在header("name").isEqualTo("name")行。期望标题的值等于文字"name"。你应该简单地说明header("name"),就像在xml dsl中一样。