我有一个项目,我在其中使用 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/