apache-camel - Camel SQL - 在 Spring Boot 中将 DataSource 放入 SimpleRegistry

标签 apache-camel datasource camel-sql

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

应用程序属性

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

应用程序.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 将“DataSource”注入(inject) Spring Boot 中的注册表。

?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

最佳答案

它是 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"

关于apache-camel - Camel SQL - 在 Spring Boot 中将 DataSource 放入 SimpleRegistry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40725368/

相关文章:

java - 关闭与数据库的连接并不会关闭所有连接

java - WildFly 10.0.0 Final正在接受请求,但将其放入队列中而不进行处理

java - 如何在apache Camel sql批量插入中设置autocommit false?

java - Apache Camel SQL 批量插入需要很长时间

java - Camel sql存储java,无法输入数组作为oracle存储过程的输入参数

java - 如何创建 REST API,然后 REST API 将对象/字符串放入 ActiveMQ

java - Karaf 中的 bundle 使用错误的 pax-jdbc 数据源

java - 如何从流程外部监控 Camel 飞行中的消息

java - 有关 apache Camel 路线的任何信息

java - Camel - 使用 useMessageBodyForSql 进行 Camel SQL 批量插入