Lucene (Indexer and Search) Component
Available as of Camel 2.2
The lucene component is based on the Apache Lucene project. Apache Lucene is a high-performance, full-featured text search engine library written entirely in Java. For more details check out
http://lucene.apache.org/java/docs/
The lucene component in camel facilitates the following
- builds a searchable index of documents when payloads are sent to the Lucene Endpoint
- facilitates performing indexed searches in Camel when the payload header contains a QUERY.
This component supports 2 producer endpoints and a query processor.
URI format
lucene:searcherName:insert[?options] lucene:searcherName:query[?options]
You can append query options to the URI in the following format, ?option=value&option=value&...
Options
Name |
Default Value |
Description |
---|---|---|
|
|
The numer of elements that may be stored in the defined cache |
|
|
The number of elements that may be stored in the defined cache. Options include
|
|
|
Specifies whether cache may overflow to disk |
|
|
Sets whether elements are eternal. If eternal, timeouts are ignored and the |
|
|
The maximum time between creation time and when an element expires. |
|
|
The maximum amount of time between accesses before an element expires |
|
|
Whether the disk store persists between restarts of the Virtual Machine. |
|
|
The number of seconds between runs of the disk expiry thread. The default value |
Sending/Receiving Messages to/from the cache
Message Headers
Header |
Description |
---|---|
|
The operation to be performed on the cache. The valid options are
|
|
The cache key used to store the Message in the cache. The cache key is optional if the CACHE_OPERATION is DELETEALL |
Cache Producer
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
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
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
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
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
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
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 } }) } };
Example 6: Using Processors to selectively replace payload with cache values
RouteBuilder builder = new RouteBuilder() { public void configure() { //Message Body Replacer from("cache://TestCache1") .filter(header("CACHE_KEY").isEqualTo("greeting")) .process(new CacheBasedMessageBodyReplacer("cache://TestCache1","farewell")) .to("direct:next"); //Message Token replacer from("cache://TestCache1") .filter(header("CACHE_KEY").isEqualTo("quote")) .process(new CacheBasedTokenReplacer("cache://TestCache1","novel","#novel#")) .process(new CacheBasedTokenReplacer("cache://TestCache1","author","#author#")) .process(new CacheBasedTokenReplacer("cache://TestCache1","number","#number#")) .to("direct:next"); //Message XPath replacer from("cache://TestCache1"). .filter(header("CACHE_KEY").isEqualTo("XML_FRAGMENT")) .process(new CacheBasedXPathReplacer("cache://TestCache1","book1","/books/book1")) .process (new CacheBasedXPathReplacer("cache://TestCache1","book2","/books/book2")) .to("direct:next"); } };