You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Olaf <om...@gmail.com> on 2013/11/22 16:28:16 UTC

re-use route definition

Hello, 

what is the best way to reuse a route with different parameters? Let's say I
have many classes implementing RouteBuilder interface and add them all in
camel-context.xml

    <camelContext xmlns="http://camel.apache.org/schema/spring">
        <routeBuilder ref="Route1" />
        <routeBuilder ref="Route2" />
        ...
        ....
    </camelContext>

Almost in every Route class I repeat the similar logic: from("file:" +
in).process(new  DoWork()).to("file:" + out); 

Is it possible to reuse this part in each Route, to avoid code duplication?
I'd have to configure in and out parameter in xml and  import this part into
configure method of Route1,2 class. Are there any examples how to do it?



--
View this message in context: http://camel.465427.n5.nabble.com/re-use-route-definition-tp5743696.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: re-use route definition

Posted by Olaf <om...@gmail.com>.
the right way :)

    @Override
    public void configure() throws Exception {
        from("file:data").to("file:data/output");
        getContext().addRoutes(fileRoute);//can be injected
    }



--
View this message in context: http://camel.465427.n5.nabble.com/re-use-route-definition-tp5743696p5743700.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: re-use route definition

Posted by Olaf <om...@gmail.com>.
Thanks for hint! 

What about composition of objects? Test doesn't work :(


    @Test()
    public void testRoute() throws Exception {
        template.sendBodyAndHeader("file:data", "foo bar",
Exchange.FILE_NAME, "foo.txt");
        Thread.sleep(3000);
        File target = new File("data/processed/foo.txt");
        assertTrue("not moved", target.exists());
        String content = context.getTypeConverter().convertTo(String.class,
target);
        assertEquals("foo bar", content);
}


######### main route

        @Override
        public void configure() throws Exception {
            from("file:data").to("file:data/output");
            //           
from("file:data/output").to("file:data/processed"); // moved to
ProcessedFileRoute class
            ProcessedFileRoute route = new ProcessedFileRoute();
            route.configure();
        }

public class ProcessedFileRoute extends RouteBuilder {
    public void configure() throws Exception {
        from("file:data/output").to("file:data/processed");
    }
}



--
View this message in context: http://camel.465427.n5.nabble.com/re-use-route-definition-tp5743696p5743698.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: re-use route definition

Posted by Claus Ibsen <cl...@gmail.com>.
Yes if you use Java RouteBuilder class you can use OO inheritance etc.

Just create a abstract base class and add the shared logic in the
configure method.
And then extend this base class, and call super.configure() first.

On Fri, Nov 22, 2013 at 4:28 PM, Olaf <om...@gmail.com> wrote:
> Hello,
>
> what is the best way to reuse a route with different parameters? Let's say I
> have many classes implementing RouteBuilder interface and add them all in
> camel-context.xml
>
>     <camelContext xmlns="http://camel.apache.org/schema/spring">
>         <routeBuilder ref="Route1" />
>         <routeBuilder ref="Route2" />
>         ...
>         ....
>     </camelContext>
>
> Almost in every Route class I repeat the similar logic: from("file:" +
> in).process(new  DoWork()).to("file:" + out);
>
> Is it possible to reuse this part in each Route, to avoid code duplication?
> I'd have to configure in and out parameter in xml and  import this part into
> configure method of Route1,2 class. Are there any examples how to do it?
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/re-use-route-definition-tp5743696.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen