1
votes

I am doing Apache Camel PoC in my project. I am stuck at one issue when using Camel JDBC component.

I can read from the database with JDBC component. But I need to use Timer component always. As per the Camel documentation JDBC component cannot be used in from() statement. I tried using Direct component in from() statement as given in the documentation but it doesn’t work.

Below is my code:

from("direct:zh_ICS_Test")
//from("timer://myTimer?period=2s")
  .setBody(constant("select * from ZH_ICS_TEST"))
  .to("jdbc:myDataSource")
  .split(body())
  .convertBodyTo(String.class)
  .to("file://" + dst);

Below is the console output:

[main] INFO org.apache.camel.impl.DefaultCamelContext - Apache Camel 2.12.1 (CamelContext: camel-1) is starting [main] INFO org.apache.camel.management.ManagedManagementStrategy - JMX is enabled [main] INFO org.apache.camel.impl.converter.DefaultTypeConverter - Loaded 176 type converters [main] INFO org.apache.camel.impl.DefaultCamelContext - StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html [main] INFO org.apache.camel.impl.DefaultCamelContext - Route: route1 started and consuming from: Endpoint[direct://zh_ICS_Test] [main] INFO org.apache.camel.impl.DefaultCamelContext - Total 1 routes, of which 1 is started. [main] INFO org.apache.camel.impl.DefaultCamelContext - Apache Camel 2.12.1 (CamelContext: camel-1) started in 0.798 seconds [main] INFO org.apache.camel.impl.DefaultCamelContext - Apache Camel 2.12.1 (CamelContext: camel-1) is shutting down [main] INFO org.apache.camel.impl.DefaultShutdownStrategy - Starting to graceful shutdown 1 routes (timeout 300 seconds) [Camel (camel-1) thread #1 - ShutdownTask] INFO org.apache.camel.impl.DefaultShutdownStrategy - Route: route1 shutdown complete, was consuming from: Endpoint[direct://zh_ICS_Test] [main] INFO org.apache.camel.impl.DefaultShutdownStrategy - Graceful shutdown of 1 routes completed in 0 seconds [main] INFO org.apache.camel.impl.DefaultCamelContext - Apache Camel 2.12.1 (CamelContext: camel-1) uptime 5.818 seconds [main] INFO org.apache.camel.impl.DefaultCamelContext - Apache Camel 2.12.1 (CamelContext: camel-1) is shutdown in 0.016 seconds

Above code works if I use Timer instead of Direct component. I don’t want to use Timer always and just need to execute my query once. I am using Camel 2.12.1 with JDK7.

Can someone please help?

2

2 Answers

2
votes

the behavior you describe is normal. You have only one route with a direct component for the from.

In that case nothing will happen unless you programmatically send an Exchange to that direct component.

Look what the camel doc says:

The direct: component provides direct, synchronous invocation of any consumers when a producer sends a message exchange. This endpoint can be used to connect existing routes in the same camel context.

0
votes

This is expected since the route does not receives any inbound exchange to consume that would trigger it.

When using the timer component, no exchange is required since it is generated by the component instead:

The timer: component is used to generate message exchanges when a timer fires. You can only consume events from this endpoint.

If you want to stick with direct: you have to sent an exchange with to("direct:zh_ICS_Test") somehow.

You can use any endpoint to trigger it:

from("...").to("direct:zh_ICS_Test");

If you plan to run your route at regular interval you could use the Quartz Component to do it (please read documentation for implementation detials):

from("quartz://myGroup/myTimerName?trigger.repeatInterval=2000&trigger.repeatCount=5").to("direct:zh_ICS_Test");