0

这是与 apache camel 二进制文件捆绑在一起的示例

 <route>
      <!-- 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>Hello ${header.name} how are you?</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>

如何将其翻译成 Java

我是骆驼的初学者,有些是怎么做到的

CamelContext context = new DefaultCamelContext();

context.addRoutes(new RouteBuilder(){

 public void configure(){

 from("servlet://hello").transform().....
}
});

但不知道如何进一步进行......

4

2 回答 2

3

如果您想在没有任何 XML(即 spring)的情况下将其移植到 java,则不能(轻松)使用 servlet 组件。

只是移植路线就像:

from("servlet:///hello")
      .choice()
        .when()
           .header("name")
              .transform(simple("Hello ${header.name} how are you?"))
        .otherwise()
            .transform(constant("Add a name parameter to uri, eg ?name=foo"));

它应该在 spring 示例(或任何 spring web 应用程序)中工作,只需将 替换为您<route>已将路由定义为 spring bean ( )。<CamelContext><routeBuilder ref="demoRoute"><bean id="demoRoute" class="org.example.demo.DemoRoute">

但是,我猜你想用普通的java(没有spring,没有xml,没有webapp)来做这件事。您可以使用 Jetty 组件。不同之处在于 Camel 然后将启动 servlet 容器,而不是 servlet 容器启动 Camel。不过,这个简单的例子没有区别。

我建议您从 Maven 原型开始构建骨架

例如,mvn archetype:generate然后选择org.apache.camel.archetypes:camel-archetype-java (Creates a new Camel project using Java DSL.) 好吧,如果您有自己的 java 应用程序并让线程继续运行,则不需要 maven 原型。那么你应该用你的方法做得很好。然而,maven 原型非常适合训练目的。

然后,您需要向 Jetty (camel-jetty.jar) 添加依赖项(在此处阅读更多内容)。

除了第一行之外,实际路线将完全相同:from("jetty:http://localhost:8080/camel/hello")

好,易于。

于 2012-12-31T09:17:04.017 回答
1

试试这个:

from("servlet://hello")
.choice()
.when(header("name").isNotNull()).transform(simple("Hello ${header.name} how are you?"))
.otherwise().transform(constant("Add a name parameter to uri, eg ?name=foo"));
于 2012-12-31T08:30:25.127 回答