I just started learning Apache Camel. I understood the basics of routes and components. Now I want to try connecting to the Oracle database, reading records from one specific table and writing these records to a file using the File component. For reading from the database, I assume that I need to use the JDBC component and give dataSourceName .
However, I could not find information on how to create a data source using a camel. All the information that was found in this thread uses Spring DSL examples. I do not use Spring, and I just need to test this using a simple standalone Java application.
I am using JDK7u25 with Apache Camel 2.12.1.
Can someone please send a sample to read from the oracle table and write to a file?
[EDIT]
After checking several solutions on the Internet, I found out about the following two approaches:
Camel to run 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 {
Using the approach mentioned by Klaus 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; } }
But in both cases, I get below the 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.
java apache-camel
Paragj
source share