Each order needs its customer, and the customer service is slow. The Cache EIP keeps the answer per customer for a while, so the shop calls the service once for many orders of the same customer. A customer that changes is removed from the cache, and a failed lookup is never cached.
INFO ... cache.camel.yaml:77 : Calling the customer service for C-482 INFO ... cache.camel.yaml:87 : Order ORD-1001 for Anna Jensen (gold), customer from the service INFO ... cache.camel.yaml:77 : Calling the customer service for C-207 INFO ... cache.camel.yaml:87 : Order ORD-1002 for Jonas Weber (silver), customer from the service INFO ... cache.camel.yaml:87 : Order ORD-1004 for Anna Jensen (gold), customer from the cache INFO ... cache.camel.yaml:87 : Order ORD-1005 for Jonas Weber (silver), customer from the cache INFO ... cache.camel.yaml:112 : Customer C-482 changed, removed from the cache INFO ... cache.camel.yaml:77 : Calling the customer service for C-482 INFO ... cache.camel.yaml:87 : Order ORD-1006 for Anna Jensen (gold), customer from the service INFO ... cache.camel.yaml:77 : Calling the customer service for C-999 INFO ... cache.camel.yaml:93 : Order ORD-1007: customer C-999 not found (HTTP 404)
Install JBang and the Camel CLI as described in the root README; camel --version confirms the install. The Cache EIP is new in Camel 4.23.
camel run *
This runs the shop's routes and the customer service in one app, on port 8080. Stop the example with ctrl + c.
incoming/ holds six orders and, after the fourth, a notice from the back office that customer C-482 changed. The files are read one a second in name order. customers/ holds the data of the customer service. C-999 has no file.customer-service route plays the slow customer service: GET /customers/{id} takes a second and answers with the customer's file, or 404.order route wraps the call in a cache block, with the customer id as the key and a time to live of ten seconds (customer.cache.ttl). On a miss, the steps inside run and their result is stored. On a hit, the cached customer becomes the body, and the steps inside are skipped. The variable source shows which one it was.convertBodyTo: String at the end of the block stores the customer as text, not as the stream that the HTTP call returns.customerCache, a MemoryKeyValueRepository. The customer-changed route calls its delete method with the customer id, so the next order of C-482 gets fresh data.doTry around the block catches the failure and logs it, and the shop goes on with the next order.Ask your assistant, or type it yourself, one step at a time, and run after each:
platform-http route for GET /customers/{id} that answers with customers/<id>.json after a second, and a file route on incoming that calls it for each order and logs the order with the customer's name and tier.customerChanged instead of an order says that a customer changed. Remove that customer from the cache, so the next order gets fresh data.customer.cache.ttl=1s: the cached customers expire before the next order, and every order calls the service.keyValueRepository: customerCache: the cache still uses the bean, because it is the only KeyValueRepository in the registry. With two such beans, name the one each cache block uses.doTry inside the cache block: the failure for C-999 is then caught inside, the block succeeds, and its result is cached.The example comes with a test in the Citrus YAML DSL, test/cache.citrus.it.yaml, which the Camel CLI runs:
camel test run test/cache.citrus.it.yaml
The test runs the routes and verifies the hits, the removed customer and the unknown customer.
If you hit any problem using Camel or have some feedback, then please let us know.
We also love contributors, so get involved :-)
The Camel riders!