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 - 当响应代码为 400 时,如何从 REST 客户端访问 apache-camel 中的有效负载?

MULE(AnyPoint Studio) 中的 Oracle 数据源配置

java - Weblogic 12c 中 Web 应用程序关闭/启动后无法解析数据源 JNDI

java - 如何为 Sun App Server 8.2 设置 JNDI

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

java - 向 Camel Twitter 消费者结果添加条件行为

java - 自定义线程池配置文件 maxPoolSize

java - Camel 路线,http4 组件,动态 url 参数

java - Camel Rest 到 dB 查询,返回错误响应