0

I always get the following exception when trying to connect to a mariDB database and i cannot figure out how to properly define the dataSource.

No bean could be found in the registry for: myDataSource of type: javax.sql.DataSource

My code is fairly simple and looks like the following:

protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {


        @Override
        public void configure() throws Exception {

            String url = "jdbc:mariadb://localhost:3306/testDB";

            BasicDataSource ds = new BasicDataSource();
            ds.setUsername("user");
            ds.setDriverClassName("com.mysql.jdbc.Driver");
            ds.setPassword("password");
            ds.setUrl(url);

            SimpleRegistry registry = new SimpleRegistry();
            registry.put("myDataSource", ds);
            CamelContext context = new DefaultCamelContext(registry);


            this.setContext(context);

            //poll the mySQL Database every x minutes.
            from("timer://Timer?period=6000")

            .to("sql:select * from testDB?dataSource=myDataSource");
       }
    };
}

I have already tried the Blueprint version of defining the dataSource, but could not get that to run either.

Is this set up in the correct way? may i be missing a dependency or do i have the wrong drivers specified?

  • That is totally wrong, you cannot just create a new CamelContext inside Camel itself. Study the database examples that comes with Apache Camel: https://github.com/apache/camel/tree/master/examples#examples – Claus Ibsen Mar 21 '18 at 17:48
  • Thanks looking at the examples has made it clear how to properly set it in JDSL as well as in the blueprint. – user9338894 Mar 22 '18 at 16:25

1 Answers1

1

After looking at the examples i found out how to solve my problem.

In JDSL in CamelTestSupport the context is registered with the createCamelContext method and the dataSource is added to the context there.

public class IntegrationTest extends CamelTestSupport{

@Override
public CamelContext createCamelContext() {
    String url = "jdbc:mysql://localhost:3306/";
    DataSource dataSource = setupDataSource(url);
    SimpleRegistry registry = new SimpleRegistry();
    registry.put("myDataSource", dataSource);
    CamelContext context = new DefaultCamelContext(registry);
    return context;
}

private static DataSource setupDataSource(String connectURI) {
    BasicDataSource ds = new BasicDataSource();
    ds.setUsername("username");
    ds.setPassword("password");
    ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
    ds.setUrl(connectURI);
    return ds;
}

protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {


        @Override
        public void configure() throws Exception {

            from("timer://Timer?period=6000")
                    .to("sql:select * from testdb.testTable
                            + " ?dataSource=myDataSource" )
                    .to("mock:result");

        }
    };
}

In the blueprint file a bean needs to be set/configured and then referenced by the SQL Component in the following way:

  <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
    <property name="url" value="http:localhost:3306"/>
    <property name="username" value="username"/>
    <property name="password" value="password"/>
  </bean>

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