I want to consume data from web service and put it in camel eh-cache. later i want to use this cache outside camel context through CacheManager. I am not finding any way to do it.
In below code I have skipped consumption of web service and used data from Map and provided it to eh-cache but I am not able to access this cache using CacheManager.
CamelRouter class
package com.camel;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.ehcache.EhcacheConstants;
import org.apache.camel.main.Main;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.Configuration;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.xml.XmlConfiguration;
public class Test {
private static Main main;
public static void main(String[] args) throws Exception {
main = new Main();
main.addRouteBuilder(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("timer:foo?period=5s&repeatCount=1")
.process(exchange ->{
Map<String, String> inputMap = new HashMap<>();
inputMap.put("name", "murli");
inputMap.put("lastname", "hiware");
inputMap.put("city", "pune");
exchange.getIn().setBody(inputMap);
exchange.getIn().setHeader("CamelEhcacheAction", EhcacheConstants.ACTION_PUT_ALL);
})
.to("ehcache://testCache?configUri=ehcache.xml&keyType=java.lang.String&valueType=java.lang.String")
.process(exchange -> {
URL myUrl = getClass().getResource("/ehcache.xml");
Configuration xmlConfig = new XmlConfiguration(myUrl);
CacheManager myCacheManager = CacheManagerBuilder.newCacheManager(xmlConfig);
myCacheManager.init();
//here I want to access already created testCache component but it is creating new one.
Cache<String, String> cache = myCacheManager.getCache("testCache", String.class, String.class);
System.out.println("Cache Element:"+cache.get("name"));
System.out.println("Exchange Message:"+exchange.getIn().getBody());
});
}
});
main.run();
}
}
ehcache config file
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd">
<cache alias="testCache">
<key-type>java.lang.String</key-type>
<value-type>java.lang.String</value-type>
<resources>
<heap unit="entries">2000</heap>
<offheap unit="MB">100</offheap>
</resources>
</cache>
<cache-template name="myDefaults">
<key-type>java.lang.Long</key-type>
<value-type>java.lang.String</value-type>
<heap unit="entries">200</heap>
</cache-template>
<cache alias="bar" uses-template="myDefaults">
<key-type>java.lang.Number</key-type>
</cache>
<cache alias="simpleCache" uses-template="myDefaults" />
</config>
please let me know the use case I am trying to achieve is possible with camel eh-cache or not?
You should normally by able to retrieve a value with a from("ehcache://...)
.
However, I will assume you really want to access the cache or cache manager.
This is the way to do it:
public static void main(String[] args) throws Exception {
URL url = App.class.getResource("/ehcache.xml");
Configuration xmlConfig = new XmlConfiguration(url);
CacheManager cacheManager = CacheManagerBuilder.newCacheManager(xmlConfig);
cacheManager.init();
Main main = new Main();
main.bind("cacheManager", cacheManager);
main.addRouteBuilder(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("timer:foo?period=5s&repeatCount=1")
.process(exchange ->{
Map<String, String> inputMap = new HashMap<>();
inputMap.put("name", "murli");
inputMap.put("lastname", "hiware");
inputMap.put("city", "pune");
exchange.getIn().setBody(inputMap);
exchange.getIn().setHeader("CamelEhcacheAction", EhcacheConstants.ACTION_PUT_ALL);
})
.to("ehcache://testCache?cacheManager=#cacheManager&keyType=java.lang.String&valueType=java.lang.String")
.process(exchange -> {
//here I want to access already created testCache component but it is creating new one.
Cache<String, String> cache = cacheManager.getCache("testCache", String.class, String.class);
System.out.println("Cache Element:"+cache.get("name"));
System.out.println("Exchange Message:"+exchange.getIn().getBody());
});
}
});
main.run();
}