【发布时间】:2018-03-21 16:49:25
【问题描述】:
我在尝试连接到 mariDB 数据库时总是遇到以下异常,我无法弄清楚如何正确定义数据源。
No bean could be found in the registry for: myDataSource of type: javax.sql.DataSource
我的代码相当简单,如下所示:
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
String url = "jdbc:mariadb://localhost:3306/testDB";
BasicDataSource ds = new BasicDataSource();
ds.setUsername("user");
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setPassword("password");
ds.setUrl(url);
SimpleRegistry registry = new SimpleRegistry();
registry.put("myDataSource", ds);
CamelContext context = new DefaultCamelContext(registry);
this.setContext(context);
//poll the mySQL Database every x minutes.
from("timer://Timer?period=6000")
.to("sql:select * from testDB?dataSource=myDataSource");
}
};
}
我已经尝试过定义数据源的蓝图版本,但也无法运行。
是否以正确的方式设置? 我可能缺少依赖项还是指定了错误的驱动程序?
【问题讨论】:
-
这是完全错误的,你不能只在 Camel 内部创建一个新的 CamelContext。研究 Apache Camel 自带的数据库示例:github.com/apache/camel/tree/master/examples#examples
-
感谢查看示例已清楚如何在 JDSL 和蓝图中正确设置它。
标签: java apache-camel camel-sql