You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by "alexis.jacquemart" <ac...@hotmail.fr> on 2015/10/26 10:33:29 UTC

Create multiple SQLComponent

Hi ! 

I'm trying to create a WAR application that create routes between multiple
sources and one destination. The sources can be SQL Database, MongoDB, REST
Service, etc and parameters of these connections are stored in a external
database.

Until now, I was using Spring to initialize the SQLComponent or MongoClient
bean but it wasn't dynamic :

	<bean id="sqlConnector" class="ConnectorSQL">
		<constructor-arg name="identifiant" value="1" />
	</bean>

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		factory-bean="sqlConnector" factory-method="setupDataSource">
	</bean>

	<bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
		<property name="dataSource" ref="dataSource" />
	</bean>

I need to do it in the Java code. Moreover, I have a list of datasources and
I have to create as many SQLComponent as my number of SQL datasources (the
same for MongoDB). *The problem is, I don't have any idea where I can create
them..*

I've created an interface ConnectorEndpoint which regroups the parameters
needed by each type of Camel Component.

The process looks like that : DAO -> getDatasources() -> create the
ConnectorEndPoint -> constructor args of the RouteBuilder -> create the
route

And my code looks like that : 

*In my RouteBuilder constructor : *

         for (Datasource datasource : listDatasources){
		switch (datasource.getType()) {
			case "oracle" : 
			srcs.add(new ConnectorSQL(datasource));
                	// I was thinking about create them here but I have to give
them dynamic name
                	// to match with the name of the component *to("sql" + id +
"://" + query)* in the route, no ?
			break;
		case "elastic" : 
			srcs.add(new ConnectorElasticSearch(datasource));
			break;
		case "mongo" : 
			srcs.add(new ConnectorMongoDB(datasource));
                         // I was thinking about create them here
			break;
		case "rest" : 
			srcs.add(new ConnectorREST(datasource));
			break;
		}
	}

*The configure method :*

     public void configure() {
        for (ConnectorEndPoint src : this.srcs) {
	     if (src instanceof ConnectorSQL){
		from("direct:" + src.getId()).*to("sql" + id + "://" + query)*
		.to(dest.getRouteTo());
	     } else if (src instanceof ConnectorMongoDB){
		from("direct:" + src.getId()).to(mongodb:mongoDriver?database=...)
		.to(dest.getRouteTo());
	     } else if (src instanceof ConnectorREST){
		...
	}
   }

In fact, I don't understand where the SQLComponents must be in the
application for the Camel Context can be able to find them. I have thought
about a Registry but how can I give it to the Camel Context and where do I
have to create it ?

I know that's a long message but I'll thank everyone that would try to help
me ;) 



--
View this message in context: http://camel.465427.n5.nabble.com/Create-multiple-SQLComponent-tp5773059.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Create multiple SQLComponent

Posted by Ramiro Pugh <ra...@gmail.com>.
Hi Alexis, I'm not sure if you can have multiple SQL Components. Camel has
a registry of components (what I've understood) and when I'd tried to use
two components in the same context I had only just one, this is (I think)
for the way that they registry and why is so easy to auto discovery a
component.

May be your solution can exist in different applications and send the
exchange with direct-vm (because your apps will be in different
applications) instead of using everything in the same app.

Hope anyone with more knowledge than me could clarify this for you.

Cheers!

2015-10-26 6:33 GMT-03:00 alexis.jacquemart <ac...@hotmail.fr>:

> Hi !
>
> I'm trying to create a WAR application that create routes between multiple
> sources and one destination. The sources can be SQL Database, MongoDB, REST
> Service, etc and parameters of these connections are stored in a external
> database.
>
> Until now, I was using Spring to initialize the SQLComponent or MongoClient
> bean but it wasn't dynamic :
>
>         <bean id="sqlConnector" class="ConnectorSQL">
>                 <constructor-arg name="identifiant" value="1" />
>         </bean>
>
>         <bean id="dataSource"
> class="org.apache.commons.dbcp.BasicDataSource"
>                 factory-bean="sqlConnector"
> factory-method="setupDataSource">
>         </bean>
>
>         <bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
>                 <property name="dataSource" ref="dataSource" />
>         </bean>
>
> I need to do it in the Java code. Moreover, I have a list of datasources
> and
> I have to create as many SQLComponent as my number of SQL datasources (the
> same for MongoDB). *The problem is, I don't have any idea where I can
> create
> them..*
>
> I've created an interface ConnectorEndpoint which regroups the parameters
> needed by each type of Camel Component.
>
> The process looks like that : DAO -> getDatasources() -> create the
> ConnectorEndPoint -> constructor args of the RouteBuilder -> create the
> route
>
> And my code looks like that :
>
> *In my RouteBuilder constructor : *
>
>          for (Datasource datasource : listDatasources){
>                 switch (datasource.getType()) {
>                         case "oracle" :
>                         srcs.add(new ConnectorSQL(datasource));
>                         // I was thinking about create them here but I
> have to give
> them dynamic name
>                         // to match with the name of the component
> *to("sql" + id +
> "://" + query)* in the route, no ?
>                         break;
>                 case "elastic" :
>                         srcs.add(new ConnectorElasticSearch(datasource));
>                         break;
>                 case "mongo" :
>                         srcs.add(new ConnectorMongoDB(datasource));
>                          // I was thinking about create them here
>                         break;
>                 case "rest" :
>                         srcs.add(new ConnectorREST(datasource));
>                         break;
>                 }
>         }
>
> *The configure method :*
>
>      public void configure() {
>         for (ConnectorEndPoint src : this.srcs) {
>              if (src instanceof ConnectorSQL){
>                 from("direct:" + src.getId()).*to("sql" + id + "://" +
> query)*
>                 .to(dest.getRouteTo());
>              } else if (src instanceof ConnectorMongoDB){
>                 from("direct:" +
> src.getId()).to(mongodb:mongoDriver?database=...)
>                 .to(dest.getRouteTo());
>              } else if (src instanceof ConnectorREST){
>                 ...
>         }
>    }
>
> In fact, I don't understand where the SQLComponents must be in the
> application for the Camel Context can be able to find them. I have thought
> about a Registry but how can I give it to the Camel Context and where do I
> have to create it ?
>
> I know that's a long message but I'll thank everyone that would try to help
> me ;)
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Create-multiple-SQLComponent-tp5773059.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Ramiro Pugh