Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the early version of the new website
https://camel.apache.org/staging/
We would very much like to receive any feedback on the new site, please join the discussion on the Camel user mailing list.
Ehcache ComponentAvailable 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 <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 formatehcache://cacheName[?options] You can append query options to the URI in the following format, Component Options
Endpoint Options
Message Headers Camel
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; } } |