2

I am using Spring Boot to initiate a camel route that uses Camel-sql to query MySQL DB and call a REST service.

application.properties

db.driver=com.mysql.jdbc.Driver
db.url=mysql://IP:PORT/abc
db.username=abc
db.password=pwd

Application.java

public static void main(String[] args) {
    SpringApplication.run(WlEventNotificationBatchApplication.class, args);

}

DataSourceConfig.java

public class DataSourceConfig {

    @Value("${db.driver}")
    public String dbDriver;

    @Value("${db.url}")
    public String dbUrl;

    @Value("${db.username}")
    public String dbUserName;

    @Value("${db.password}")
    public String dbPassword;
    @Bean("dataSource")
    public DataSource getConfig() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(dbDriver);
        dataSource.setUrl(dbUrl);
        dataSource.setUsername(dbUserName);
        dataSource.setPassword(dbPassword);

        return dataSource;
    }
}

WLRouteBuilder.java

@Component
public class WLRouteBuilder extends RouteBuilder {
    @Autowired
    private NotificationConfig notificationConfig;

    @Autowired
    private DataSource dataSource;

    @Override
    public void configure() throws Exception {

        from("direct:eventNotification")
                .to("sql:"+notificationConfig.getSqlQuery()+"?dataSource="+dataSource)
                .process(new RowMapper())
                .log("${body}");

    }
}

I see the below error when I run, found out that Camel is unable to find DataSource bean in registry. I am quite not sure how to inject "DataSource" to Registry in Spring Boot using Java DSL.

?dataSource=org.springframework.jdbc.datasource.DriverManagerDataSource%40765367 due to: No bean could be found in the registry for: org.springframework.jdbc.datasource.DriverManagerDataSource@765367 of type: javax.sql.DataSource
Daniel Quinlan
  • 2,639
  • 1
  • 20
  • 23
user1637487
  • 241
  • 1
  • 9
  • 17

1 Answers1

4

Its the name of the bean that Camel uses in the uri, where you refer to it using the # syntax as documented here: http://camel.apache.org/how-do-i-configure-endpoints.html (referring beans)

So something alike

 .to("sql:"+notificationConfig.getSqlQuery()+"?dataSource=#dataSource"

Where dataSource is the name of the bean that creates the DataSource, which you can give another name eg

@Bean("myDataSource")

And then the Camel SQL endpoint is

    .to("sql:"+notificationConfig.getSqlQuery()+"?dataSource=#myDataSource"
Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
  • Hi Claus, Thanks a lot for your response. Can I ask you one more question. from("sql:{{list.sql}}?dataSource=#dataSource") .log("process row ${body}") .end(); and in application.properties file, I have included camel.springboot.main-run-controller=true. Route is executed in infinite until I press CNTRL + C. If I remove camel.springboot.main-run-controller, Camel starts the route and stops the main thread. Is there anyway I can have Camel start, execute the route and stop. Hope my question is clear. Thanks in advance! – user1637487 Nov 24 '16 at 01:04
  • Hi Claus, Sorry if I am missing something very simple. I am using Spring Boot for this application. I have tried from("sql:{{list.eventnotification.sql}}?dataSource=#dataSource") .log("process row ${body}") .to("mock:bar"); and added getContext().stop();. Still the route is not stopped because of camel.springboot.main-run-controller=true in application.properties. In a standalone Java app, we start route (camelcontext.start()) and stop it manually. How does it work in Spring Boot? Pls help – user1637487 Nov 24 '16 at 15:20
  • Yeah I think there is a fix in the next 2.18.1 release. – Claus Ibsen Nov 24 '16 at 16:30
  • Is there any work-around I can do, to start, process and stop the route in Spring Boot. My code either runs infinite route loop (camel.springboot.main-run-controller=true) or stops with out executing the route? – user1637487 Nov 24 '16 at 17:18