I am trying to implement Hazelcast caching with Camel blueprint. But I couldn't accomplish.I am able to create a hazelcast instance through java code (not through hazelcast XML config file). Instance has been craeted, but the cache loader class is not called during the instance creation (even though initialization method is EAGER). Attached some of the code snippets.
Let me know, if anyone come across this.
Code:
public class ConfigHack extends Config {
public ConfigHack(String instanceName ){
super(instanceName);
System.out.println("Going to create Hazelcast instance
................"+instanceName);
TcpIpConfig tcpIpConfig = new TcpIpConfig();
List membersList = new ArrayList<String>();
membersList.add("localhost");
tcpIpConfig.setMembers(membersList);
MulticastConfig multicastConfig = new MulticastConfig();
multicastConfig.setEnabled(true);
JoinConfig join = new JoinConfig();
join.setTcpIpConfig(tcpIpConfig);
join.setMulticastConfig(multicastConfig);
NetworkConfig networkConfig = new NetworkConfig();
networkConfig.setPort(5701);
networkConfig.setPortAutoIncrement(true);
networkConfig.setJoin(join);
GroupConfig groupConfig = new GroupConfig();
groupConfig.setName("devuser");
groupConfig.setPassword("devpassword");
MapStoreConfig mapStoreConfig = new MapStoreConfig();
//Absolute path in class name field below
mapStoreConfig.setClassName("VehicleCacheLoader");
mapStoreConfig.setImplementation(new VehicleCacheLoader());
mapStoreConfig.setEnabled(true);
mapStoreConfig.setInitialLoadMode(InitialLoadMode.EAGER);
mapStoreConfig.setWriteDelaySeconds(500);
MapConfig mapConfig = new MapConfig();
mapConfig.setName("vehicleMap");
mapConfig.setBackupCount(2);
mapConfig.setMaxIdleSeconds(1000000);
mapConfig.setEvictionPercentage(30);
mapConfig.setEvictionPolicy(EvictionPolicy.LFU);
mapConfig.setMapStoreConfig(mapStoreConfig);
Map<String,MapConfig> mapConfigs = new HashMap<String,MapConfig>();
mapConfigs.put("vehicleMap", mapConfig);
//config.setMapConfigs(mapConfigs);
addMapConfig(mapConfig);
setGroupConfig(groupConfig);
setNetworkConfig(networkConfig);
}
}
public class VehicleCacheLoader implements MapLoader<String, VehicleVO> {
@Override
public VehicleVO load(String paramK) {
System.out.println("Calling load method for Key " + paramK);
VehicleVO vehicleVO = new VehicleVO();
vehicleVO.setCustId("XXX");
vehicleVO.setVehicleHeader("XXX");
vehicleVO.setVehicleInitial("001");
vehicleVO.setVehicleNumber("1234");
vehicleVO.setVehicleObjId(paramK);
return vehicleVO;
}
@Override
public Map<String, VehicleVO> loadAll(Collection<String> paramCollection) {
System.out.println("Calling Load all values() " + "Got key = ");
VehicleVO vehicleVO = null;
Map<String, VehicleVO> vehicleDataMap = new HashMap<String, VehicleVO>();
for (String paramKey : paramCollection) {
System.out.println("Calling ...." + paramKey);
vehicleVO = new VehicleVO();
vehicleVO.setCustId("XXX");
vehicleVO.setVehicleHeader("XXX");
vehicleVO.setVehicleInitial("001");
vehicleVO.setVehicleNumber("1234");
vehicleVO.setVehicleObjId(paramKey);
vehicleDataMap.put(paramKey, vehicleVO);
}
return vehicleDataMap;
}
@Override
public Set<String> loadAllKeys() {
System.out.println("Calling Load all keys() ");
Set<String> vehicleKeys = new HashSet<String>();
vehicleKeys.add("XXX001");
vehicleKeys.add("XXX002");
vehicleKeys.add("XXX003");
vehicleKeys.add("XXX004");
return vehicleKeys;
}
}
Blueprint config:
-----------------
<bean id="hazelcastInstance" class="com.hazelcast.core.Hazelcast"
factory-method="newHazelcastInstance" destroy-method="shutdown">
<argument ref="hazelcastConfig"/>
</bean>
<bean id="hazelcastConfig" class="xx.yy.zz.ss.tt.cache.ConfigHack">
<argument value="TestInstance" />
</bean>
The line
mapConfigs.put("vehicleMap", mapConfig);
defines the configuration that will be used for maps with names matching "vehicaleMap
".
In order to create such a map you need to run an operation against it, such as
hazelcastInstance.getMap("vehicleMap");
The distinction is clearer if the configuration was:
mapConfigs.put("vehicleMap*", mapConfig);
This would be used when you create a map named "vehicleMap1
", or "vehicleMap123
".
The configuration defines the configuration which will be used if needed. It's not needed til you first access the map, which is when the maps are created.
"EAGER" here refers to how the map loader is run, not to how the map is created.