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.
JCache ComponentAvailable as of Camel 2.17 The jcache component enables you to perform caching operations using JCache (JSR-107) 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. 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, use the processors defined for the cache component. Maven users will need to add the following dependency to their <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jcache</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> URI formatcache://cacheName[?options] You can append query options to the URI in the following format,
Options
Header variables
JCache based idempotent repository example:
JCacheIdempotentRepository idempotentRepo = new JCacheIdempotentRepository(); idempotentRepo.setCacheName("idempotent-cache") from("direct:in") .idempotentConsumer(header("messageId"), idempotentRepo) .to("mock:out"); JCache based aggregation repository example:
package org.apache.camel.component.jcache.processor.aggregate; import org.apache.camel.EndpointInject; import org.apache.camel.Exchange; import org.apache.camel.Produce; import org.apache.camel.ProducerTemplate; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.processor.aggregate.AggregationStrategy; import org.junit.Test; public class JCacheAggregationRepositoryRoutesTest extends JCacheAggregationRepositoryTestSupport { private static final String MOCK_GOTCHA = "mock:gotcha"; private static final String DIRECT_ONE = "direct:one"; private static final String DIRECT_TWO = "direct:two"; @EndpointInject(uri = MOCK_GOTCHA) private MockEndpoint mock; @Produce(uri = DIRECT_ONE) private ProducerTemplate produceOne; @Produce(uri = DIRECT_TWO) private ProducerTemplate produceTwo; @Test public void checkAggregationFromTwoRoutes() throws Exception { final JCacheAggregationRepository repoOne = createRepository(false); final JCacheAggregationRepository repoTwo = createRepository(false); final int completionSize = 4; final String correlator = "CORRELATOR"; RouteBuilder rbOne = new RouteBuilder() { @Override public void configure() throws Exception { from(DIRECT_ONE).routeId("AggregatingRouteOne") .aggregate(header(correlator)) .aggregationRepository(repoOne) .aggregationStrategy(new MyAggregationStrategy()) .completionSize(completionSize) .to(MOCK_GOTCHA); } }; RouteBuilder rbTwo = new RouteBuilder() { @Override public void configure() throws Exception { from(DIRECT_TWO).routeId("AggregatingRouteTwo") .aggregate(header(correlator)) .aggregationRepository(repoTwo) .aggregationStrategy(new MyAggregationStrategy()) .completionSize(completionSize) .to(MOCK_GOTCHA); } }; context().addRoutes(rbOne); context().addRoutes(rbTwo); context().start(); mock.expectedMessageCount(1); mock.expectedBodiesReceived(1 + 2 + 3 + 4); produceOne.sendBodyAndHeader(1, correlator, correlator); produceTwo.sendBodyAndHeader(2, correlator, correlator); produceOne.sendBodyAndHeader(3, correlator, correlator); produceOne.sendBodyAndHeader(4, correlator, correlator); mock.assertIsSatisfied(); } private class MyAggregationStrategy implements AggregationStrategy { @Override public 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; } } } protected JCacheAggregationRepository createRepository(boolean optimistic) throws Exception { JCacheAggregationRepository repository = new JCacheAggregationRepository(); repository.setConfiguration(new JCacheConfiguration()); repository.setCacheName("aggregation-repository"); repository.setOptimistic(optimistic); return repository; } }
|