【问题标题】:apache camel RouteBuilder not work when passing header/param name from url从 url 传递标头/参数名称时,apache camel RouteBuilder 不起作用
【发布时间】:2017-01-11 15:24:03
【问题描述】:

我正在看一个关于 Servlet 的骆驼示例,它适用于这个 xml 骆驼定义 (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,就像这样(骆驼上下文在 Servlet 上下文启动时启动一次,在 Web 应用程序停止时停止):

@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 类)。

谢谢

罗比

【问题讨论】:

    标签: java apache-camel dsl servlet-3.0


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多