You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by rogelio_sevilla1 <ro...@gmail.com> on 2011/07/27 20:13:17 UTC

Injecting data to routes loaded from xml file

Hello everyone:

I'm using camel as an osgi bundle on fuse esb. I currently have a bundle
with a simple POJO that instantiates some camel routes that reside on
another bundle. This works fine.

However, my routes are written on java DSL and I would like to transform
them to Spring DSL. Currently my camel routes look like this:


public class MyRoute extends RouteBuilder implements myInterface {

private String myvar1;
private String myvar2;

@Override
public String getMyvar1(){
return myvar1;
}

//the rest of the getters and setters ...


public void configure() {
//route stuff
}
}



On my application, i need to send some data on the route before starting it.
Using java DSL,  i've done it using the previous code. However, i've read
this page on the camel site:

http://camel.apache.org/loading-routes-from-xml-files.html

And i don't see any way to achieve the same thing using the spring DSL. So i
was wondering: 

Is it possible to declare some kind of instance members on a spring dsl
camel route, load the xml file on my java code,send my values and finally
starting the route??


Any advice would be appreciated.

Thanks a lot in advance.





--
View this message in context: http://camel.465427.n5.nabble.com/Injecting-data-to-routes-loaded-from-xml-file-tp4639765p4639765.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Injecting data to routes loaded from xml file

Posted by Claus Ibsen <cl...@gmail.com>.
On Tue, Aug 2, 2011 at 8:44 PM, rogelio_sevilla1
<ro...@gmail.com> wrote:
> Thanks for the answer mr. Claus,  "myVar1"  is an instance member variable
> contained on the class that loads the xml route file.
>
> MyRouteClass extends RouteBuilder{
>
> *private String myVar1;
> //getter and setter for myVar1*
>
>
> public void configure() throws Exception {
>                    FileInputStream fis = new
> FileInputStream("myroute.xml");
>                    RoutesDefinition routes =
>  getContext().loadRoutesDefinition(fis);
>  }
>
> }
>
> And in my xml file i have this:
>
> <from uri="quartz://fetchUrl?cron=0+0/1+*+*+*+?"/>
>            <setProperty propertyName="coolProperty">
>                <simple>*myVar1*</simple>
>            </setProperty>
>
>
>
>
> I need to have the class written this way because each time i create an
> instance, i need to pass different values to it. Is it possible to send the
> values on the variable myVar1 to the routes defined on the spring file??
>

No not easily, instead you can insert the myVar value directly into
the xml file you are loading.

Or you can adjust it directly on the model classes from the
RoutesDefinition you get loaded, eg using the API from the
org.apache.camel.model classes.



>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Injecting-data-to-routes-loaded-from-xml-file-tp4639765p4659913.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Injecting data to routes loaded from xml file

Posted by rogelio_sevilla1 <ro...@gmail.com>.
Thanks for the answer mr. Claus,  "myVar1"  is an instance member variable
contained on the class that loads the xml route file.

MyRouteClass extends RouteBuilder{

*private String myVar1;
//getter and setter for myVar1*


public void configure() throws Exception {
                    FileInputStream fis = new
FileInputStream("myroute.xml");
                    RoutesDefinition routes =
 getContext().loadRoutesDefinition(fis);
 }

}

And in my xml file i have this:

<from uri="quartz://fetchUrl?cron=0+0/1+*+*+*+?"/>
            <setProperty propertyName="coolProperty">
                <simple>*myVar1*</simple>
            </setProperty>




I need to have the class written this way because each time i create an
instance, i need to pass different values to it. Is it possible to send the
values on the variable myVar1 to the routes defined on the spring file??


--
View this message in context: http://camel.465427.n5.nabble.com/Injecting-data-to-routes-loaded-from-xml-file-tp4639765p4659913.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Injecting data to routes loaded from xml file

Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Jul 28, 2011 at 6:51 PM, Rogelio Alejandro Ortiz Sevilla
<ro...@gmail.com> wrote:
> Thanks A LOT for the answer mr. Claus, i used
>
> <bean beanType="com.mycompany.MyPojo" method="getHtmlCode"/>
>
> And works great!!!.  I do have a follow up question if you don't mind, it's
> also about data injection. My route class currently  looks like this:
>
>
> MyRouteClass extends RouteBuilder{
>
> private String myVar1;
> //getter and setter for myVar1
>
> public void configure() throws Exception {
>                    FileInputStream fis = new
> FileInputStream("myroute.xml");
>                    RoutesDefinition routes =
>  getContext().loadRoutesDefinition(fis);
>  }
>
> }
>
> And in my xml file i have this:
>
> <from uri="quartz://fetchUrl?cron=0+0/1+*+*+*+?"/>
>            <setProperty propertyName="coolProperty">
>                <simple>myVar1</simple>
>            </setProperty>
>
>
> Obviously the property is getting the literal value "myVar1" which is
> something i don't want. I need to send the value contained on
> the instance member myVar1, is there any way i could achieve this??
>

Where is the source for that myVar1 value?

You can use property placeholders if the source is a properties file
http://camel.apache.org/using-propertyplaceholder.html



> Thanks A LOT in advance
>
>
>
> 2011/7/28 Claus Ibsen <cl...@gmail.com>
>
>> Camel is not a bean container. You have to have the bean installed already.
>> For example in the spring xml file.
>>
>> However you can tell Camel the FQN of the bean class and it can invoke
>> it directly, in case it has a default ctr.
>>
>> In the camel route do something like:
>> <bean type="com.foo.MyBean"/>
>>
>> The attribute may have a slightly different name, than type.
>>
>>
>> On Thu, Jul 28, 2011 at 5:08 PM, rogelio_sevilla1
>> <ro...@gmail.com> wrote:
>> > I think i was wrong, the info on this page:
>> >
>> > http://camel.apache.org/loading-routes-from-xml-files.html
>> >
>> > is the right answer, however, i think i'm missing something. Currently, i
>> > have a camel route that looks like this:
>> >
>> > public void configure() throws Exception {
>> >                   FileInputStream fis = new
>> FileInputStream("myroute.xml");
>> >                    RoutesDefinition routes =
>> > getContext().loadRoutesDefinition(fis);
>> > }
>> >
>> >
>> > And my  myroute.xml  file looks like this:
>> >
>> >
>> >
>> > <?xml version="1.0" encoding="UTF-8"?>
>> >    <routes xmlns="http://camel.apache.org/schema/spring">
>> >        <bean id="myPojo" class="com.mycompany.MyPojo"/>
>> >        <route>
>> >            <from uri="quartz://fetchUrl?cron=0+0/1+*+*+*+?"/>
>> >            <setProperty propertyName="contract">
>> >                <simple>contract</simple>
>> >            </setProperty>
>> >            <setProperty propertyName="dataFeedSource">
>> >                <simple>dataFeedSource</simple>
>> >            </setProperty>
>> >            <setProperty propertyName="dataFeedType">
>> >                <simple>dataFeedType</simple>
>> >            </setProperty>
>> >            <setProperty propertyName="url">
>> >                <simple>url</simple>
>> >            </setProperty>
>> >            <bean ref="myPojo" method="getHtmlCode"/>
>> >            <bean ref="myPojo" method="getUrls"/>
>> >            <split>
>> >                <tokenize  token="\n"/>
>> >                <to uri="seda:theUrls"/>
>> >            </split>
>> >        </route>
>> >    <route>
>> >        <from uri="seda:theUrls?concurrentConsumers=8"/>
>> >        <bean ref="myPojo" method="setUrl"/>
>> >        <to uri="http://dummyhost" />
>> >        <bean ref="myPojo" method="processContent"/>
>> >    </route>
>> >    </routes>
>> >
>> >
>> >
>> > As you can see, i'm declaring a bean using this:
>> >
>> >        <bean id="myPojo" class="com.mycompany.MyPojo"/>
>> >
>> > However, at the moment of running this code, i'm getting this exception:
>> >
>> >
>> > Failed to create route route1 at: >>> Bean[ref:myPojo method:
>> getHtmlCode]
>> >  because of No bean could be found in the registry for: myPojo
>> >
>> > Caused by: org.apache.camel.NoSuchBeanException: No bean could be found
>> in
>> > the registry for: myPojo
>> >        at
>> >
>> org.apache.camel.component.bean.RegistryBean.getBean(RegistryBean.java:68)
>> >        at
>> >
>> org.apache.camel.model.BeanDefinition.createProcessor(BeanDefinition.java:155)
>> >        at
>> >
>> org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:433)
>> >        at
>> >
>> org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:181)
>> >        at
>> >
>> org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:815)
>> >
>> >
>> >
>> > Am i missing another step on the process??
>> >
>> > --
>> > View this message in context:
>> http://camel.465427.n5.nabble.com/Injecting-data-to-routes-loaded-from-xml-file-tp4639765p4643056.html
>> > Sent from the Camel - Users mailing list archive at Nabble.com.
>> >
>>
>>
>>
>> --
>> Claus Ibsen
>> -----------------
>> FuseSource
>> Email: cibsen@fusesource.com
>> Web: http://fusesource.com
>> Twitter: davsclaus, fusenews
>> Blog: http://davsclaus.blogspot.com/
>> Author of Camel in Action: http://www.manning.com/ibsen/
>>
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Injecting data to routes loaded from xml file

Posted by Rogelio Alejandro Ortiz Sevilla <ro...@gmail.com>.
Thanks A LOT for the answer mr. Claus, i used

<bean beanType="com.mycompany.MyPojo" method="getHtmlCode"/>

And works great!!!.  I do have a follow up question if you don't mind, it's
also about data injection. My route class currently  looks like this:


MyRouteClass extends RouteBuilder{

private String myVar1;
//getter and setter for myVar1

public void configure() throws Exception {
                    FileInputStream fis = new
FileInputStream("myroute.xml");
                    RoutesDefinition routes =
 getContext().loadRoutesDefinition(fis);
 }

}

And in my xml file i have this:

<from uri="quartz://fetchUrl?cron=0+0/1+*+*+*+?"/>
            <setProperty propertyName="coolProperty">
                <simple>myVar1</simple>
            </setProperty>


Obviously the property is getting the literal value "myVar1" which is
something i don't want. I need to send the value contained on
the instance member myVar1, is there any way i could achieve this??

Thanks A LOT in advance



2011/7/28 Claus Ibsen <cl...@gmail.com>

> Camel is not a bean container. You have to have the bean installed already.
> For example in the spring xml file.
>
> However you can tell Camel the FQN of the bean class and it can invoke
> it directly, in case it has a default ctr.
>
> In the camel route do something like:
> <bean type="com.foo.MyBean"/>
>
> The attribute may have a slightly different name, than type.
>
>
> On Thu, Jul 28, 2011 at 5:08 PM, rogelio_sevilla1
> <ro...@gmail.com> wrote:
> > I think i was wrong, the info on this page:
> >
> > http://camel.apache.org/loading-routes-from-xml-files.html
> >
> > is the right answer, however, i think i'm missing something. Currently, i
> > have a camel route that looks like this:
> >
> > public void configure() throws Exception {
> >                   FileInputStream fis = new
> FileInputStream("myroute.xml");
> >                    RoutesDefinition routes =
> > getContext().loadRoutesDefinition(fis);
> > }
> >
> >
> > And my  myroute.xml  file looks like this:
> >
> >
> >
> > <?xml version="1.0" encoding="UTF-8"?>
> >    <routes xmlns="http://camel.apache.org/schema/spring">
> >        <bean id="myPojo" class="com.mycompany.MyPojo"/>
> >        <route>
> >            <from uri="quartz://fetchUrl?cron=0+0/1+*+*+*+?"/>
> >            <setProperty propertyName="contract">
> >                <simple>contract</simple>
> >            </setProperty>
> >            <setProperty propertyName="dataFeedSource">
> >                <simple>dataFeedSource</simple>
> >            </setProperty>
> >            <setProperty propertyName="dataFeedType">
> >                <simple>dataFeedType</simple>
> >            </setProperty>
> >            <setProperty propertyName="url">
> >                <simple>url</simple>
> >            </setProperty>
> >            <bean ref="myPojo" method="getHtmlCode"/>
> >            <bean ref="myPojo" method="getUrls"/>
> >            <split>
> >                <tokenize  token="\n"/>
> >                <to uri="seda:theUrls"/>
> >            </split>
> >        </route>
> >    <route>
> >        <from uri="seda:theUrls?concurrentConsumers=8"/>
> >        <bean ref="myPojo" method="setUrl"/>
> >        <to uri="http://dummyhost" />
> >        <bean ref="myPojo" method="processContent"/>
> >    </route>
> >    </routes>
> >
> >
> >
> > As you can see, i'm declaring a bean using this:
> >
> >        <bean id="myPojo" class="com.mycompany.MyPojo"/>
> >
> > However, at the moment of running this code, i'm getting this exception:
> >
> >
> > Failed to create route route1 at: >>> Bean[ref:myPojo method:
> getHtmlCode]
> >  because of No bean could be found in the registry for: myPojo
> >
> > Caused by: org.apache.camel.NoSuchBeanException: No bean could be found
> in
> > the registry for: myPojo
> >        at
> >
> org.apache.camel.component.bean.RegistryBean.getBean(RegistryBean.java:68)
> >        at
> >
> org.apache.camel.model.BeanDefinition.createProcessor(BeanDefinition.java:155)
> >        at
> >
> org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:433)
> >        at
> >
> org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:181)
> >        at
> >
> org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:815)
> >
> >
> >
> > Am i missing another step on the process??
> >
> > --
> > View this message in context:
> http://camel.465427.n5.nabble.com/Injecting-data-to-routes-loaded-from-xml-file-tp4639765p4643056.html
> > Sent from the Camel - Users mailing list archive at Nabble.com.
> >
>
>
>
> --
> Claus Ibsen
> -----------------
> FuseSource
> Email: cibsen@fusesource.com
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.blogspot.com/
> Author of Camel in Action: http://www.manning.com/ibsen/
>

Re: Injecting data to routes loaded from xml file

Posted by Claus Ibsen <cl...@gmail.com>.
Camel is not a bean container. You have to have the bean installed already.
For example in the spring xml file.

However you can tell Camel the FQN of the bean class and it can invoke
it directly, in case it has a default ctr.

In the camel route do something like:
<bean type="com.foo.MyBean"/>

The attribute may have a slightly different name, than type.


On Thu, Jul 28, 2011 at 5:08 PM, rogelio_sevilla1
<ro...@gmail.com> wrote:
> I think i was wrong, the info on this page:
>
> http://camel.apache.org/loading-routes-from-xml-files.html
>
> is the right answer, however, i think i'm missing something. Currently, i
> have a camel route that looks like this:
>
> public void configure() throws Exception {
>                   FileInputStream fis = new FileInputStream("myroute.xml");
>                    RoutesDefinition routes =
> getContext().loadRoutesDefinition(fis);
> }
>
>
> And my  myroute.xml  file looks like this:
>
>
>
> <?xml version="1.0" encoding="UTF-8"?>
>    <routes xmlns="http://camel.apache.org/schema/spring">
>        <bean id="myPojo" class="com.mycompany.MyPojo"/>
>        <route>
>            <from uri="quartz://fetchUrl?cron=0+0/1+*+*+*+?"/>
>            <setProperty propertyName="contract">
>                <simple>contract</simple>
>            </setProperty>
>            <setProperty propertyName="dataFeedSource">
>                <simple>dataFeedSource</simple>
>            </setProperty>
>            <setProperty propertyName="dataFeedType">
>                <simple>dataFeedType</simple>
>            </setProperty>
>            <setProperty propertyName="url">
>                <simple>url</simple>
>            </setProperty>
>            <bean ref="myPojo" method="getHtmlCode"/>
>            <bean ref="myPojo" method="getUrls"/>
>            <split>
>                <tokenize  token="\n"/>
>                <to uri="seda:theUrls"/>
>            </split>
>        </route>
>    <route>
>        <from uri="seda:theUrls?concurrentConsumers=8"/>
>        <bean ref="myPojo" method="setUrl"/>
>        <to uri="http://dummyhost" />
>        <bean ref="myPojo" method="processContent"/>
>    </route>
>    </routes>
>
>
>
> As you can see, i'm declaring a bean using this:
>
>        <bean id="myPojo" class="com.mycompany.MyPojo"/>
>
> However, at the moment of running this code, i'm getting this exception:
>
>
> Failed to create route route1 at: >>> Bean[ref:myPojo method: getHtmlCode]
>  because of No bean could be found in the registry for: myPojo
>
> Caused by: org.apache.camel.NoSuchBeanException: No bean could be found in
> the registry for: myPojo
>        at
> org.apache.camel.component.bean.RegistryBean.getBean(RegistryBean.java:68)
>        at
> org.apache.camel.model.BeanDefinition.createProcessor(BeanDefinition.java:155)
>        at
> org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:433)
>        at
> org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:181)
>        at
> org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:815)
>
>
>
> Am i missing another step on the process??
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Injecting-data-to-routes-loaded-from-xml-file-tp4639765p4643056.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Injecting data to routes loaded from xml file

Posted by rogelio_sevilla1 <ro...@gmail.com>.
I think i was wrong, the info on this page:

http://camel.apache.org/loading-routes-from-xml-files.html

is the right answer, however, i think i'm missing something. Currently, i
have a camel route that looks like this:

public void configure() throws Exception {
                   FileInputStream fis = new FileInputStream("myroute.xml");
                    RoutesDefinition routes =
getContext().loadRoutesDefinition(fis);
}


And my  myroute.xml  file looks like this:



<?xml version="1.0" encoding="UTF-8"?>
    <routes xmlns="http://camel.apache.org/schema/spring">
	<bean id="myPojo" class="com.mycompany.MyPojo"/>
        <route>
            <from uri="quartz://fetchUrl?cron=0+0/1+*+*+*+?"/>    
            <setProperty propertyName="contract"> 
                <simple>contract</simple> 
            </setProperty>
            <setProperty propertyName="dataFeedSource"> 
                <simple>dataFeedSource</simple> 
            </setProperty>  
            <setProperty propertyName="dataFeedType"> 
                <simple>dataFeedType</simple> 
            </setProperty>  
            <setProperty propertyName="url"> 
                <simple>url</simple> 
            </setProperty>  
            <bean ref="myPojo" method="getHtmlCode"/>
            <bean ref="myPojo" method="getUrls"/>
            <split>
                <tokenize  token="\n"/>
                <to uri="seda:theUrls"/>
            </split>
        </route>
    <route>
        <from uri="seda:theUrls?concurrentConsumers=8"/>    
        <bean ref="myPojo" method="setUrl"/>
        <to uri="http://dummyhost" />
        <bean ref="myPojo" method="processContent"/>
    </route>
    </routes>



As you can see, i'm declaring a bean using this:

	<bean id="myPojo" class="com.mycompany.MyPojo"/>

However, at the moment of running this code, i'm getting this exception:


Failed to create route route1 at: >>> Bean[ref:myPojo method: getHtmlCode]
 because of No bean could be found in the registry for: myPojo

Caused by: org.apache.camel.NoSuchBeanException: No bean could be found in
the registry for: myPojo
	at
org.apache.camel.component.bean.RegistryBean.getBean(RegistryBean.java:68)
	at
org.apache.camel.model.BeanDefinition.createProcessor(BeanDefinition.java:155)
	at
org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:433)
	at
org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:181)
	at
org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:815)



Am i missing another step on the process??

--
View this message in context: http://camel.465427.n5.nabble.com/Injecting-data-to-routes-loaded-from-xml-file-tp4639765p4643056.html
Sent from the Camel - Users mailing list archive at Nabble.com.