You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by arnaud <ar...@free.fr> on 2015/09/10 13:56:23 UTC

Dynamic DataSource

Hello,

Id'like to use a dynamic datasource with the camel-sql component but it's
not woking as I expected:
After a search on the forum, I have found a topic "Dynamic DataSource for
SQL", I try to do something similar :

from("direct:create")
.setHeader("dataSource", constant("myDynamicDataSource"))
.setHeader(SqlConstants.SQL_QUERY, simple("insert into customer(firstname,
lastname) values(:?firstName, :?lastName)"))
.setHeader("destination", new
QueryBuilder()).recipientList(header("destination"));

class QueryBuilder implements Expression {

	@Override
	public <T> T evaluate(Exchange exchange, Class<T> type) {

		StringBuilder query = new StringBuilder();
		query.append("sql:");
		query.append(exchange.getIn().getHeader(SqlConstants.SQL_QUERY));
		query.append("?dataSource=#");
		query.append(exchange.getIn().getHeader("dataSource"));

		return exchange.getContext().getTypeConverter().convertTo(type,
query.toString());
	}
}

But I have this error message :
Failed to resolve endpoint:
sql://insert%20into%20customer(firstname,%20lastname)%20values(:?firstName%2C+%3A%3FlastName%29%3FdataSource=%23myDynamicDataSource
due to: DataSource must be configured

Any idea of the problem or another solution to implement a dynamic
datasource?

Thanks,
Arnaud




--
View this message in context: http://camel.465427.n5.nabble.com/Dynamic-DataSource-tp5771402.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Dynamic DataSource

Posted by arnaud <ar...@free.fr>.
Thanks for your reply Claus.

My fisrt try was with this method :

from("direct:create")
.setHeader("dataSource", simple("myDynamicDataSource"))
.to(simple("sql:insert into customer(firstname, lastname)
values(:#firstName, :#lastName)?dataSource=#${header.dataSource}")
						.getText()).to("log:output");

Bu when the camel constext is started I have the following error: 

Caused by: org.apache.camel.NoSuchBeanException: No bean could be found in
the registry for: ${header.dataSource} of type: javax.sql.DataSource

Arnaud



--
View this message in context: http://camel.465427.n5.nabble.com/Dynamic-DataSource-tp5771402p5771427.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Dynamic DataSource

Posted by Claus Ibsen <cl...@gmail.com>.
You can build the uri dynamic as in this FAQ
http://camel.apache.org/how-to-use-a-dynamic-uri-in-to.html

On Thu, Sep 10, 2015 at 1:56 PM, arnaud <ar...@free.fr> wrote:
> Hello,
>
> Id'like to use a dynamic datasource with the camel-sql component but it's
> not woking as I expected:
> After a search on the forum, I have found a topic "Dynamic DataSource for
> SQL", I try to do something similar :
>
> from("direct:create")
> .setHeader("dataSource", constant("myDynamicDataSource"))
> .setHeader(SqlConstants.SQL_QUERY, simple("insert into customer(firstname,
> lastname) values(:?firstName, :?lastName)"))
> .setHeader("destination", new
> QueryBuilder()).recipientList(header("destination"));
>
> class QueryBuilder implements Expression {
>
>         @Override
>         public <T> T evaluate(Exchange exchange, Class<T> type) {
>
>                 StringBuilder query = new StringBuilder();
>                 query.append("sql:");
>                 query.append(exchange.getIn().getHeader(SqlConstants.SQL_QUERY));
>                 query.append("?dataSource=#");
>                 query.append(exchange.getIn().getHeader("dataSource"));
>
>                 return exchange.getContext().getTypeConverter().convertTo(type,
> query.toString());
>         }
> }
>
> But I have this error message :
> Failed to resolve endpoint:
> sql://insert%20into%20customer(firstname,%20lastname)%20values(:?firstName%2C+%3A%3FlastName%29%3FdataSource=%23myDynamicDataSource
> due to: DataSource must be configured
>
> Any idea of the problem or another solution to implement a dynamic
> datasource?
>
> Thanks,
> Arnaud
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Dynamic-DataSource-tp5771402.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2nd edition:
https://www.manning.com/books/camel-in-action-second-edition

Re: Dynamic DataSource

Posted by arnaud <ar...@free.fr>.
Chistian, yes the datasource is configured in the camel context, when the
dataSource is hard coded it works :

from("direct:create")
.to("sql:insert into customer(firstname, lastname) values(:#firstName,   
#lastName)?dataSource=#myDynamicSource"));

Arnaud



--
View this message in context: http://camel.465427.n5.nabble.com/Dynamic-DataSource-tp5771402p5771487.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Dynamic DataSource

Posted by Christian Müller <ch...@gmail.com>.
Is the dataSource configured in your context?

Best,
Christian
Am 10.09.2015 13:56 schrieb "arnaud" <ar...@free.fr>:

> Hello,
>
> Id'like to use a dynamic datasource with the camel-sql component but it's
> not woking as I expected:
> After a search on the forum, I have found a topic "Dynamic DataSource for
> SQL", I try to do something similar :
>
> from("direct:create")
> .setHeader("dataSource", constant("myDynamicDataSource"))
> .setHeader(SqlConstants.SQL_QUERY, simple("insert into customer(firstname,
> lastname) values(:?firstName, :?lastName)"))
> .setHeader("destination", new
> QueryBuilder()).recipientList(header("destination"));
>
> class QueryBuilder implements Expression {
>
>         @Override
>         public <T> T evaluate(Exchange exchange, Class<T> type) {
>
>                 StringBuilder query = new StringBuilder();
>                 query.append("sql:");
>
> query.append(exchange.getIn().getHeader(SqlConstants.SQL_QUERY));
>                 query.append("?dataSource=#");
>                 query.append(exchange.getIn().getHeader("dataSource"));
>
>                 return
> exchange.getContext().getTypeConverter().convertTo(type,
> query.toString());
>         }
> }
>
> But I have this error message :
> Failed to resolve endpoint:
>
> sql://insert%20into%20customer(firstname,%20lastname)%20values(:?firstName%2C+%3A%3FlastName%29%3FdataSource=%23myDynamicDataSource
> due to: DataSource must be configured
>
> Any idea of the problem or another solution to implement a dynamic
> datasource?
>
> Thanks,
> Arnaud
>
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Dynamic-DataSource-tp5771402.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>