使用Apache骆驼蓝图的Hazelcast缓存实现

时间:2018-07-13 15:03:00

标签: java apache-camel hazelcast

我正在尝试使用Camel蓝图实现Hazelcast缓存。但是我无法完成,我能够通过Java代码(而不是通过hazelcast XML配置文件)创建hazelcast实例。已创建实例,但是在实例创建期间未调用缓存加载器类(即使初始化方法为EAGER)。附加了一些代码片段。

如果有人遇到这个问题,请告诉我。

代码:

Hazelcast配置

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>

1 个答案:

答案 0 :(得分:0)

mapConfigs.put("vehicleMap", mapConfig);

定义将用于名称与“ vehicaleMap”匹配的地图的配置。

要创建这样的地图,您需要对其进行操作,例如

hazelcastInstance.getMap("vehicleMap"); 

如果配置为:

,则区别更加明显。

mapConfigs.put("vehicleMap*", mapConfig);

当您创建名为“ vehicleMap1”或“ vehicleMap123”的地图时,将使用它。

配置定义了将在需要时使用的配置。直到您首次访问地图(创建地图时)才需要。

EAGER ”是指地图加载器的运行方式,而不是指地图的创建方式。