Java 如何使用骆驼创建数据源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19357395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to create datasource using camel?
提问by ParagJ
I have just started learning Apache Camel. I understood the basics of Routes and Components. Now I want to give a try by connecting to Oracle database, reading records from one particular table and write those records to a file using File
component. To read from database I assume I need to use JDBC
component and give the dataSourceName
.
我刚刚开始学习 Apache Camel。我了解路由和组件的基础知识。现在我想尝试连接到 Oracle 数据库,从一个特定的表中读取记录并使用File
组件将这些记录写入文件。要从数据库中读取,我假设我需要使用JDBC
组件并提供dataSourceName
.
However, I couldn't find any info on how to create a dataSource using camel. All info that I found related to this topic uses Spring DSL examples. I don't use Spring and I just need to test this using simple standalone Java application.
但是,我找不到有关如何使用骆驼创建数据源的任何信息。我发现的与此主题相关的所有信息都使用 Spring DSL 示例。我不使用 Spring,我只需要使用简单的独立 Java 应用程序来测试它。
I am using JDK7u25 with Apache Camel 2.12.1.
我在 Apache Camel 2.12.1 中使用 JDK7u25。
Can someone please post a sample to read from the oracle table and write to a file?
有人可以发布一个示例以从 oracle 表中读取并写入文件吗?
[EDIT]
[编辑]
After checking several solutions on the web, I came to know about following two approaches:
在网上查了几个解决方案后,我了解到以下两种方法:
Camel to run as standalone. Here is my code:
import javax.sql.DataSource; import org.apache.camel.main.Main; import org.apache.camel.builder.RouteBuilder; import org.apache.commons.dbcp.BasicDataSource; public class JDBCExample { private Main main; public static void main(String[] args) throws Exception { JDBCExample example = new JDBCExample(); example.boot(); } public void boot() throws Exception { // create a Main instance main = new Main(); // enable hangup support so you can press ctrl + c to terminate the JVM main.enableHangupSupport(); String url = "jdbc:oracle:thin:@MYSERVER:1521:myDB"; DataSource dataSource = setupDataSource(url); // bind dataSource into the registery main.bind("myDataSource", dataSource); // add routes main.addRouteBuilder(new MyRouteBuilder()); // run until you terminate the JVM System.out.println("Starting Camel. Use ctrl + c to terminate the JVM.\n"); main.run(); } class MyRouteBuilder extends RouteBuilder { public void configure() { String dst = "C:/Local Disk E/TestData/Destination"; from("direct:myTable") .setBody(constant("select * from myTable")) .to("jdbc:myDataSource") .to("file:" + dst); } } private DataSource setupDataSource(String connectURI) { BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName("oracle.jdbc.driver.OracleDriver"); ds.setUsername("sa"); ds.setPassword("devon1"); ds.setUrl(connectURI); return ds; } }
Using the approach mentioned by Claus lbsen. Here is the code again:
import javax.sql.DataSource; import org.apache.camel.CamelContext; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.SimpleRegistry; import org.apache.camel.main.Main; import org.apache.camel.builder.RouteBuilder; import org.apache.commons.dbcp.BasicDataSource; public class JDBCExample { private Main main; public static void main(String[] args) throws Exception { String url = "jdbc:oracle:thin:@MYSERVER:1521:myDB"; DataSource dataSource = setupDataSource(url); SimpleRegistry reg = new SimpleRegistry() ; reg.put("myDataSource",dataSource); CamelContext context = new DefaultCamelContext(reg); context.addRoutes(new JDBCExample().new MyRouteBuilder()); context.start(); Thread.sleep(5000); context.stop(); } class MyRouteBuilder extends RouteBuilder { public void configure() { String dst = "C:/Local Disk E/TestData/Destination"; from("direct:myTable") .setBody(constant("select * from myTable")) .to("jdbc:myDataSource") .to("file:" + dst); } } private static DataSource setupDataSource(String connectURI) { BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName("oracle.jdbc.driver.OracleDriver"); ds.setUsername("sa"); ds.setPassword("devon1"); ds.setUrl(connectURI); return ds; } }
Camel 作为独立运行。这是我的代码:
import javax.sql.DataSource; import org.apache.camel.main.Main; import org.apache.camel.builder.RouteBuilder; import org.apache.commons.dbcp.BasicDataSource; public class JDBCExample { private Main main; public static void main(String[] args) throws Exception { JDBCExample example = new JDBCExample(); example.boot(); } public void boot() throws Exception { // create a Main instance main = new Main(); // enable hangup support so you can press ctrl + c to terminate the JVM main.enableHangupSupport(); String url = "jdbc:oracle:thin:@MYSERVER:1521:myDB"; DataSource dataSource = setupDataSource(url); // bind dataSource into the registery main.bind("myDataSource", dataSource); // add routes main.addRouteBuilder(new MyRouteBuilder()); // run until you terminate the JVM System.out.println("Starting Camel. Use ctrl + c to terminate the JVM.\n"); main.run(); } class MyRouteBuilder extends RouteBuilder { public void configure() { String dst = "C:/Local Disk E/TestData/Destination"; from("direct:myTable") .setBody(constant("select * from myTable")) .to("jdbc:myDataSource") .to("file:" + dst); } } private DataSource setupDataSource(String connectURI) { BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName("oracle.jdbc.driver.OracleDriver"); ds.setUsername("sa"); ds.setPassword("devon1"); ds.setUrl(connectURI); return ds; } }
使用 Claus lbsen 提到的方法。这里又是代码:
import javax.sql.DataSource; import org.apache.camel.CamelContext; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.SimpleRegistry; import org.apache.camel.main.Main; import org.apache.camel.builder.RouteBuilder; import org.apache.commons.dbcp.BasicDataSource; public class JDBCExample { private Main main; public static void main(String[] args) throws Exception { String url = "jdbc:oracle:thin:@MYSERVER:1521:myDB"; DataSource dataSource = setupDataSource(url); SimpleRegistry reg = new SimpleRegistry() ; reg.put("myDataSource",dataSource); CamelContext context = new DefaultCamelContext(reg); context.addRoutes(new JDBCExample().new MyRouteBuilder()); context.start(); Thread.sleep(5000); context.stop(); } class MyRouteBuilder extends RouteBuilder { public void configure() { String dst = "C:/Local Disk E/TestData/Destination"; from("direct:myTable") .setBody(constant("select * from myTable")) .to("jdbc:myDataSource") .to("file:" + dst); } } private static DataSource setupDataSource(String connectURI) { BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName("oracle.jdbc.driver.OracleDriver"); ds.setUsername("sa"); ds.setPassword("devon1"); ds.setUrl(connectURI); return ds; } }
But in both the cases I am getting below exception:
但在这两种情况下,我都遇到了以下异常:
Caused by: org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: jdbc://myDataSource due to: No component found with scheme: jdbc
at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:534)
at org.apache.camel.util.CamelContextHelper.getMandatoryEndpoint(CamelContextHelper.java:63)
at org.apache.camel.model.RouteDefinition.resolveEndpoint(RouteDefinition.java:192)
at org.apache.camel.impl.DefaultRouteContext.resolveEndpoint(DefaultRouteContext.java:106)
at org.apache.camel.impl.DefaultRouteContext.resolveEndpoint(DefaultRouteContext.java:112)
at org.apache.camel.model.SendDefinition.resolveEndpoint(SendDefinition.java:61)
at org.apache.camel.model.SendDefinition.createProcessor(SendDefinition.java:55)
at org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:500)
at org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:213)
at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:909)
... 12 more
[Thread-0] INFO org.apache.camel.main.MainSupport$HangupInterceptor - Received hang up - stopping the main instance.
采纳答案by ParagJ
So silly me! I had not included camel-jdbc-2.12.1.jar in the CLASSPATH. Now above examples work.
太傻了我!我没有在 CLASSPATH 中包含camel-jdbc-2.12.1.jar。现在上面的例子工作。
回答by aviad
Spring was mentioned there just because it is very useful paradigm of working with DB (mainly because of templates introduced by Spring Framework.) Of course you can hook up the standard JDBCconnection and implement DAO by yourself - nothing wrong with that.
在那里提到 Spring 只是因为它是使用 DB 的非常有用的范例(主要是因为Spring Framework 引入的模板。)当然,您可以连接标准的JDBC连接并自己实现 DAO - 没有错。
回答by Claus Ibsen
There is the SQL example which shows how to setup a DataSource
有一个 SQL 示例显示了如何设置一个 DataSource
Yes that examples using Spring XML. But how you setup the DataSource can be done in Java code also. Then you need to register the DataSource in the Camel Registry.
是的,使用 Spring XML 的示例。但是如何设置 DataSource 也可以在 Java 代码中完成。然后你需要在 Camel Registry 中注册 DataSource。
For example you can use a JndiRegistry
or the SimpleRegistry
. The latter is easier.
例如,您可以使用 aJndiRegistry
或SimpleRegistry
. 后者更容易。
Here is some pseudo code showing the principle, of creating a registry, add your beans into this registry, and then providing the registry to the constructor of DefaultCamelContext
.
下面是一些伪代码,显示了创建注册表的原理,将您的 bean 添加到此注册表中,然后将注册表提供给DefaultCamelContext
.
SimpleRegistry registry = new SimpleRegistry();
// code to create data source here
DateSource ds = ...
registry.put("myDataSource", ds);
CamelContext camel = new DefaultCamelContext(registry);