camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tim Dudgeon <>
Subject Re: Is there a tools to convert java dsl to xml ?
Date Fri, 12 Jun 2015 08:10:04 GMT
I was playing with just this a few weeks ago. Here's an example of what 
I got working.
The one gotcha I encountered was that you can't use beans inline in the 
Java DSL. Instead you need to put the bean in the registry and reference 
it from there. Other than that it seems to work "as expected".


  * Example of how to add a new route to a running Camel context.
  * The route is set as XML. The tricky bit is that any beans used in 
the route
  * need to be set to the registry before setting the route.

package foo

import org.apache.camel.model.language.XQueryExpression
import org.apache.camel.model.language.GroovyExpression
import org.apache.camel.model.*
import org.apache.camel.*
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.impl.SimpleRegistry

// define route 1 as XML
String xml1 = '''<?xml version="1.0"?>
<routes xmlns="">\n\
     <route id="bar">
         <from uri="timer://foo?fixedRate=true&amp;period=500"/>
         <process ref="simpleProcessor1"/>
         <log message="Hello from XML!"/>

// generate route 2 and convert to XML
RoutesDefinition routes = new RoutesDefinition()
RouteDefinition route = routes.route()
route.log("Hello from Java!")
route.processRef("simpleProcessor2") // the reference to the bean in the 

String xml2 = ModelHelper.dumpModelAsXml(null, routes)
println xml2

// create and start context
long t0 = System.currentTimeMillis();
SimpleRegistry registry = new SimpleRegistry()
CamelContext camelContext = new DefaultCamelContext(registry)
long t1 = System.currentTimeMillis();
System.out.println("Camel Context creation took " + (t1 - t0));

// add beans to the registry
registry.put("simpleProcessor1", new SimpleProcessor("simpleProcessor1"))
registry.put("simpleProcessor2", new SimpleProcessor("simpleProcessor2"))
// set route to context
RoutesDefinition routes1 = camelContext.loadRoutesDefinition(new 
long t3 = System.currentTimeMillis();
RoutesDefinition routes2 = camelContext.loadRoutesDefinition(new 
long t4 = System.currentTimeMillis();
System.out.println("New route creation took " + (t4 - t3));

// shutdown

On 09/06/2015 16:32, Claus Ibsen wrote:
> Hi
> At runtime you can output any Camel route as xml, there is api on
> camel context or jmx api to do so.
> This is what tooling such as the camel commands / hawtio etc uses to
> visualize the running routes.
> On Tue, Jun 9, 2015 at 11:11 AM, cloud <> wrote:
>> hi team,
>> Is there a tools to convert java dsl to xml ?
>> Is every java dsl code  have a equal xml counterpart?
>> When I am reading the camel document's , there are many snippets code that
>> writen by java dsl .
>> As a new user , sometimes I have to use xml instead of java dsl  .
>> In fact , I want to know how could I convert the below code to xml
>> from("direct:start")
>>    .setHeader("decisionSlip").method(OrderProcessorBean.class,
>> "calculateRoute")
>>    .routingSlip("decisionSlip");
>> thanks.
>> --
>> View this message in context:
>> Sent from the Camel - Users mailing list archive at

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message