madhairsilence
madhairsilence

Reputation: 3870

Translation XML information to Java - Apache Camel

This is the example that is bundled with apache camel binaries

 <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>

How to translate this to Java

Am a beginner in camel, and some how came up to this

CamelContext context = new DefaultCamelContext();

context.addRoutes(new RouteBuilder(){

 public void configure(){

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

But dont know how to proceed further...

Upvotes: 0

Views: 876

Answers (2)

Petter Nordlander
Petter Nordlander

Reputation: 22279

If you want to port it over to java without any XML (spring that is) you can't (easily) use the servlet component.

Just porting the route will be like:

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"));

It should work in the spring example (or any spring web app), just replacing the <route> in the <CamelContext> with <routeBuilder ref="demoRoute"> given you have defined your route as a spring bean (<bean id="demoRoute" class="org.example.demo.DemoRoute">).

However, I guess you want to do this in plain java (no spring, no xml, no webapp). You could go with the Jetty component. The difference being that Camel then will start the servlet container, instead of the servlet container starting Camel. No difference for this simple example though.

I suggest you start out with a Maven archetype to get the skeleton up

e.g. mvn archetype:generate then choose org.apache.camel.archetypes:camel-archetype-java (Creates a new Camel project using Java DSL.) Well, you don't need the maven archetype if you have your own java application and have the thread keep running. Then you should do fine with your approach. The maven archetype is however very good for training purposes.

You then need to add a dependency to Jetty (camel-jetty.jar) (read more here).

The actual route would be exactly the same except the first row: from("jetty:http://localhost:8080/camel/hello")

Nice and easy.

Upvotes: 3

Christian Schneider
Christian Schneider

Reputation: 19606

Try this one:

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"));

Upvotes: 1

Related Questions