1
votes

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
1

1 Answers

3
votes

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"