【问题标题】:Camel SQL - Put DataSource to SimpleRegistry in Spring BootCamel SQL - 在 Spring Boot 中将数据源放入 SimpleRegistry
【发布时间】:2017-04-05 03:43:05
【问题描述】:

我正在使用 Spring Boot 启动骆驼路线,该路线使用 Camel-sql 来查询 MySQL DB 并调用 REST 服务。

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}");

    }
}

我在运行时看到以下错误,发现 Camel 无法在注册表中找到 DataSource bean。我不太确定如何使用 Java DSL 在 Spring Boot 中将“DataSource”注入 Registry。

?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

【问题讨论】:

    标签: apache-camel datasource camel-sql


    【解决方案1】:

    它是 Camel 在 uri 中使用的 bean 的名称,您可以使用此处记录的 # 语法来引用它:http://camel.apache.org/how-do-i-configure-endpoints.html(引用 bean)

    类似的东西

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

    其中dataSource 是创建DataSource 的bean 的名称,您可以给它取另一个名称,例如

    @Bean("myDataSource")
    

    然后 Camel SQL 端点是

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

    【讨论】:

    • 嗨克劳斯,非常感谢您的回复。我能再问你一个问题吗? from("sql:{{list.sql}}?dataSource=#dataSource") .log("处理行 ${body}") .end();在 application.properties 文件中,我包含了 camel.springboot.main-run-controller=true。在我按下 CNTRL + C 之前,路由会无限执行。如果我删除 camel.springboot.main-run-controller,Camel 会启动路由并停止主线程。无论如何我可以让骆驼开始,执行路线并停止。希望我的问题很清楚。提前致谢!
    • 嗨克劳斯,对不起,如果我错过了一些非常简单的东西。我正在为这个应用程序使用 Spring Boot。我试过 from("sql:{{list.eventnotification.sql}}?dataSource=#dataSource") .log("process row ${body}") .to("mock:bar");并添加了 getContext().stop();。由于 application.properties 中的 camel.springboot.main-run-controller=true,路由仍然没有停止。在一个独立的 Java 应用程序中,我们启动路由 (camelcontext.start()) 并手动停止它。它在 Spring Boot 中是如何工作的?请帮忙
    • 是的,我认为在下一个 2.18.1 版本中有修复。
    • 我有什么办法可以在 Spring Boot 中启动、处理和停止路由。我的代码要么运行无限路由循环(camel.springboot.main-run-controller=true),要么在不执行路由的情况下停止?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 2021-09-24
    相关资源
    最近更新 更多