Ehcache Component
Available as of Camel 2.18.x
The ehcache component enables you to perform caching operations using Ehcache 3 as the Cache Implementation.
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.
Maven users will need to add the following dependency to their pom.xml
for this component:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-ehcache</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>
URI format
ehcache://cacheName[?options]
You can append query options to the URI in the following format, ?option=value&option=#beanRef&...
Component Options
Name | Default Value | Type | Description | Since |
---|---|---|---|---|
configuration |
| EhcacheConfiguration | The global component configuration | camel 2.20.x |
cacheConfiguration | null | CacheConfiguration | The default cache configuration to be used to create caches if configUri is not set | camel 2.20.x |
cacheConfigurationUri | null | String | URI pointing to the Ehcache XML configuration file's location | camel 2.20.x |
cacheManager | null | CacheManager | The cache manager, if set configuration and configurationUri are discarded | camel 2.19.1 |
Endpoint Options
Name | Default Value | Description |
---|---|---|
configUri |
| URI pointing to the Ehcache XML configuration file's location (deprecated in camel 2.20.x) |
configurationUri | null | URI pointing to the Ehcache XML configuration file's location |
configuration |
| The default cache configuration to be used to create caches if configUri is not set |
cacheManager | null | The cache manager, if set configuration and configurationUri are discarded |
createCacheIfNotExist |
| Configure if a cache need to be created if it does exist or can't be pre-configured. |
|
| To configure the default cache action. If an action is set in the message header, then the operation from the header takes precedence. |
|
| To configure the default action key. If a key is set in the message header, then the key from the header takes precedence. |
|
| The cache key type, default Object.class |
| java.lang.Object | The cache value type, default Object.class |
eventOrdering | ORDERED | Set the the delivery mode (ordered, unordered), consumer only |
eventFiring | ASYNCHRONOUS | Set the the delivery mode (synchronous, asynchronous), consumer only |
eventTypes | EVICTED,EXPIRED,REMOVED,CREATED,UPDATED | Set the type of events to listen for, consumer only |
Message Headers Camel
Header | Type | Description |
---|---|---|
CamelEhcacheAction |
| The operation to be perfomed on the cache, valid options are:
|
CamelEhcacheActionHasResult | Boolean | Set to true if the action has a result |
CamelEhcacheActionSucceeded |
| Set to true if the actionsuccedded |
CamelEhcacheKey | Object | The cache key used for an action |
CamelEhcacheKeys | Set<Object> | A list of keys, used in
|
CamelEhcacheValue | Object | The value to put in the cache or the result of an operation |
CamelEhcacheOldValue | Object | The old value associated to a key for actions like PUT_IF_ABSENT or the Object used for comparison for actions like REPLACE |
CamelEhcacheEventType | EventType | The type of event received |
Ehcache based idempotent repository example:
CacheManager manager = CacheManagerBuilder.newCacheManager(new XmlConfiguration("ehcache.xml")); EhcacheIdempotentRepository repo = new EhcacheIdempotentRepository(manager, "idempotent-cache"); from("direct:in") .idempotentConsumer(header("messageId"), idempotentRepo) .to("mock:out");
Ehcache based aggregation repository example:
public class EhcacheAggregationRepositoryRoutesTest extends CamelTestSupport { private static final String ENDPOINT_MOCK = "mock:result"; private static final String ENDPOINT_DIRECT = "direct:one"; private static final int[] VALUES = generateRandomArrayOfInt(10, 0, 30); private static final int SUM = IntStream.of(VALUES).reduce(0, (a, b) -> a + b); private static final String CORRELATOR = "CORRELATOR"; @EndpointInject(uri = ENDPOINT_MOCK) private MockEndpoint mock; @Produce(uri = ENDPOINT_DIRECT) private ProducerTemplate producer; @Test public void checkAggregationFromOneRoute() throws Exception { mock.expectedMessageCount(VALUES.length); mock.expectedBodiesReceived(SUM); IntStream.of(VALUES).forEach( i -> producer.sendBodyAndHeader(i, CORRELATOR, CORRELATOR) ); mock.assertIsSatisfied(); } private Exchange aggregate(Exchange oldExchange, Exchange newExchange) { if (oldExchange == null) { return newExchange; } else { Integer n = newExchange.getIn().getBody(Integer.class); Integer o = oldExchange.getIn().getBody(Integer.class); Integer v = (o == null ? 0 : o) + (n == null ? 0 : n); oldExchange.getIn().setBody(v, Integer.class); return oldExchange; } } @Override protected RoutesBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from(ENDPOINT_DIRECT) .routeId("AggregatingRouteOne") .aggregate(header(CORRELATOR)) .aggregationRepository(createAggregateRepository()) .aggregationStrategy(EhcacheAggregationRepositoryRoutesTest.this::aggregate) .completionSize(VALUES.length) .to("log:org.apache.camel.component.ehcache.processor.aggregate.level=INFO&showAll=true&mulltiline=true") .to(ENDPOINT_MOCK); } }; } protected EhcacheAggregationRepository createAggregateRepository() throws Exception { CacheManager cacheManager = CacheManagerBuilder.newCacheManager(new XmlConfiguration("ehcache.xml")); cacheManager.init(); EhcacheAggregationRepository repository = new EhcacheAggregationRepository(); repository.setCacheManager(cacheManager); repository.setCacheName("aggregate"); return repository; } }