CachePage added by Ashwin KarpeCache ComponentThe cache component enables you to perform caching operations using EHCache as the Cache Implementation. The cache itself is created on demand or if a cache of that name already exists then it is simply utilized with its original settings. The Cache component has the following abilities a> In Producer mode, the component provides the ability to direct payloads in exchanges to a stored in a pre-existing or created-on-demand Cache. The producer mode supports operations to ADD/UPDATE/DELETE/DELETEALL elements in a cache. (Examples given below) b> In Consumer mode, the component provides the ability to listen on a pre-existing or created-on-demand Cache using an event Listener and receive automatic notifications when any cache activity take place (i.e ADD/UPDATE/DELETE/DELETEALL). Upon such an activity taking place, an exchange containing header elements describing the operation and cachekey and a body containing the just added/updated payload is placed and sent. In case of a DELETEALL operation the body of the exchanage is not populated. c> There are a set of nice processors to the camel-cache component to provide the ability to perform cache lookups and selectively replace payload content at the
URI formatcache:cacheName[?options] This component supports producer and event based consumer endpoints.
ResultThe result is returned in the OUT body as an ArrayList<HashMap<String, Object>>. The List object contains the list of rows and the Map objects contain each row with the String key as the column name. Note: This component fetches ResultSetMetaData to be able to return the column name as the key in the Map. Message Headers
SamplesIn the following example, we fetch the rows from the customer table. First we register our datasource in the Camel registry as testdb: JndiRegistry reg = super.createRegistry(); reg.bind("testdb", ds); return reg; Then we configure a route that routes to the JDBC component, so the SQL will be executed. Note how we refer to the testdb datasource that was bound in the previous step: // lets add simple route public void configure() throws Exception { from("direct:hello").to("jdbc:testdb?readSize=100"); } Or you can create a DataSource in Spring like this: <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="timer://kickoff?period=10000"/> <setBody> <constant>select * from customer</constant> </setBody> <to uri="jdbc:testdb"/> <to uri="mock:result"/> </route> </camelContext> <!-- Just add a demo to show how to bind a date source for camel in Spring--> <bean id="testdb" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.hsqldb.jdbcDriver"/> <property name="url" value="jdbc:hsqldb:mem:camel_jdbc" /> <property name="username" value="sa" /> <property name="password" value="" /> </bean> We create an endpoint, add the SQL query to the body of the IN message, and then send the exchange. The result of the query is returned in the OUT body: // first we create our exchange using the endpoint Endpoint endpoint = context.getEndpoint("direct:hello"); Exchange exchange = endpoint.createExchange(); // then we set the SQL on the in body exchange.getIn().setBody("select * from customer order by ID"); // now we send the exchange to the endpoint, and receives the response from Camel Exchange out = template.send(endpoint, exchange); // assertions of the response assertNotNull(out); assertNotNull(out.getOut()); ArrayList<HashMap<String, Object>> data = "" assertNotNull("out body could not be converted to an ArrayList - was: " + out.getOut().getBody(), data); assertEquals(2, data.size()); HashMap<String, Object> row = data.get(0); assertEquals("cust1", row.get("ID")); assertEquals("jstrachan", row.get("NAME")); row = data.get(1); assertEquals("cust2", row.get("ID")); assertEquals("nsandhu", row.get("NAME")); Sample - Polling the database every minuteIf we want to poll a database using the JDBC component, we need to combine it with a polling scheduler such as the Timer or Quartz etc. In the following example, we retrieve data from the database every 60 seconds:
from("timer://foo?period=60000").setBody(constant("select * from customer")).to("jdbc:testdb").to("activemq:queue:customers");
See Also
Change Notification Preferences
View Online
|
Add Comment
|
- [CONF] Apache Camel > Cache confluence
- [CONF] Apache Camel > Cache confluence
- [CONF] Apache Camel > Cache confluence
- [CONF] Apache Camel > Cache confluence
- [CONF] Apache Camel > Cache confluence
- [CONF] Apache Camel > Cache confluence
- [CONF] Apache Camel > Cache confluence
- [CONF] Apache Camel > Cache confluence
- [CONF] Apache Camel > Cache confluence
- [CONF] Apache Camel > Cache confluence
- [CONF] Apache Camel > Cache confluence