Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The 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

  • body
  • token
  • xpath level

This component supports producer and event based consumer endpoints.

...

The Cache consumer is an event based consumer and can be used to listen and respond to specific cache activities. If you need to perform selections from a pre-existing cache, used the processors defined for the cache component.

URI format

Code Block
cache:cacheName[?options]

...

You can append query options to the URI in the following format, ?option=value&option=value&...

...

Sending/Receiving Messages to/from the cache

Sending data to the cache involves setting the Message Exchange Headers given in the section below.

Message Headers

Header

Description

CACHE_OPERATION

The operation to be performed on the cache. The valid options are

  • ADD
  • UPDATE
  • DELETE
  • DELETEALL

CACHE_KEY

The cache key used to store the Message in the cache. The cache key is optional if the CACHE_OPERATION is DELETEALL

Cache Producer

...

In the following example, we fetch the rows from the customer table.

First we register our datasource in the Camel registry as testdb:

Wiki Markup
{snippet:id=register|lang=java|url=camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java}

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:

Wiki Markup
{snippet:id=route|lang=java|url=camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java}

Or you can create a DataSource in Spring like this:

Wiki Markup
{snippet:id=example|lang=java|url=camel/trunk/components/camel-jdbc/src/test/resources/org/apache/camel/component/jdbc/camelContext.xml}

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:

Wiki Markup
{snippet:id=invoke|lang=java|url=camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java}

Sample - Polling the database every minute

If 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");

Sending data to the cache involves the ability to direct payloads in exchanges to be stored in a pre-existing or created-on- demand cache. The mechanics of doing this involve

  • setting the Message Exchange Headers shown above.
  • ensuring that the Message Exchange Body contains the message directed to the cache

Cache Consumer

Receiving data from the cache involves the ability of the CacheConsumer 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 Message Exchange Headers and a Message Exchange Body containing the just added/updated payload is placed and sent.
  • in case of a DELETEALL operation, the Message Exchange Header CACHE_KEY and the Message Exchange Body are not populated.

Cache Processors

There are a set of nice processors with the ability to perform cache lookups and selectively replace payload content at the

  • body
  • token
  • xpath level

Cache Usage Samples

Example 1: Configuring the cache

Code Block

from("cache://MyApplicationCache" +
          "?maxElementsInMemory=1000" + 
          "&memoryStoreEvictionPolicy=" +
              "MemoryStoreEvictionPolicy.LFU" +
          "&overflowToDisk=true" +
          "&eternal=true" +
          "&timeToLiveSeconds=300" + 
          "&timeToIdleSeconds=true" +
          "&diskPersistent=true" +
          "&diskExpiryThreadIntervalSeconds=300")

Example 2: Adding keys to the cache

Code Block

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
     from("direct:start")
     .setHeader("CACHE_OPERATION", 
        constant("ADD"))
     .setHeader("CACHE_KEY", 
        constant("Ralph_Waldo_Emerson"))
     .to("cache://TestCache1")
   }
};

Example 2: Updating existing keys in a cache

Code Block

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
     from("direct:start")
     .setHeader("CACHE_OPERATION", 
          constant("UPDATE"))
     .setHeader("CACHE_KEY", 
          constant("Ralph_Waldo_Emerson"))
     .to("cache://TestCache1")
   }
};

Example 3: Deleting existing keys in a cache

Code Block

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
     from("direct:start")
     .setHeader("CACHE_OPERATION", 
          constant("DELETE"))
     .setHeader("CACHE_KEY", 
          constant("Ralph_Waldo_Emerson"))
     .to("cache://TestCache1")
   }
};

Example 4: Deleting all existing keys in a cache

Code Block

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
     from("direct:start")
     .setHeader("CACHE_OPERATION", 
          constant("DELETEALL"))
     .to("cache://TestCache1");
    }
};

Example 5: Notifying any changes registering in a Cache to Processors and other Producers

Code Block

Note: in this example the consumer is 
     created first and then 3 routes 
     send different message as Cache Producers

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
     from("cache://TestCache1")
     .process(new Processor() {
        public void process(Exchange exchange) 
               throws Exception {
           String operation =
            (String) exchange.getIn()
                .getHeader("CACHE_OPERATION");
           String key =
            (String) exchange.getIn()
                .getHeader("CACHE_KEY");
           Object body = exchange.getIn()
                .getBody();
           // Do something
        } 
     })
   } 
};

...