jdbc - Camel : use datasource configured by spring-boot

标签 jdbc spring-boot apache-camel

我有一个项目,我在其中使用 spring-boot-jdbc-starter,它会自动为我配置一个数据源。 现在,我将 camel-spring-boot 添加到项目中,并且能够从类型为 RouteBuilder 的 Beans 成功创建路由。 但是当我使用 camel 的 sql 组件时,它找不到数据源。有什么简单的方法可以将 Spring 配置的数据源添加到 CamelContext?在 camel 项目示例中,他们使用 spring xml 进行数据源配置,但我正在寻找一种使用 java 配置的方法。这是我尝试过的:

@Configuration
public class SqlRouteBuilder extends RouteBuilder {
  @Bean
  public SqlComponent sqlComponent(DataSource dataSource) {
    SqlComponent sqlComponent = new SqlComponent();
    sqlComponent.setDataSource(dataSource);
    return sqlComponent;
  }

  @Override
  public void configure() throws Exception {
    from("sql:SELECT * FROM tasks WHERE STATUS NOT LIKE 'completed'")
            .to("mock:sql");
  }
}

最佳答案

我必须发布它,因为虽然答案在评论中,但您可能没有注意到它,在我的情况下,这样的配置是运行该过程所必需的。 SQL 组件的使用应该是这样的:

         from("timer://dbQueryTimer?period=10s")
                .routeId("DATABASE_QUERY_TIMER_ROUTE")
                .to("sql:SELECT * FROM event_queue?dataSource=#dataSource")
                .process(xchg -> {
                    List<Map<String, Object>> row = xchg.getIn().getBody(List.class);
                    row.stream()
                            .map((x) -> {
                                EventQueue eventQueue = new EventQueue();
                                eventQueue.setId((Long)x.get("id"));
                                eventQueue.setData((String)x.get("data"));
                                return eventQueue;
                            }).collect(Collectors.toList());
                })
                .log(LoggingLevel.INFO,"******Database query executed - body:${body}******");

注意 ?dataSource=#dataSource 的使用。 dataSource名称指向Spring配置的DataSource对象,可以更改为另一个,从而在不同的路由中使用不同的DataSource。

关于jdbc - Camel : use datasource configured by spring-boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33102524/

相关文章:

docker - 在 vagrant 和 docker 中调试 spring boot

java - 我可以使 jdbc 4.1 源代码级别与 jdk 1.6 兼容吗?

java - 如何在查询的基础上设置 JDBC 的时区?

java - 如何使用jdbc从oracle DB获取时间戳

java - 将 REST API 的 404 响应更改为 200 空响应

java - Apache Camel : Is there any way to set a message to fault without injecting the exchange in a bean method?

mysql - Clojure JDBC : MySQL errors when creating table with composite unique constraint

sql - 我如何将 Flyway 迁移用于单个模式和多个项目

java - 为什么 setHeader 不在流程功能内工作而是在功能之外工作

java - Apache Camel Cron 作业调度