Hazelcast - Jacache cacheManager.getCache返回null - 抛出ClassCastException

时间:2015-05-07 15:43:16

标签: java classcastexception hazelcast jcache

我正在使用Jache和Hazelcast作为我的Web应用程序中的实现,当我在tomcat上部署我的应用程序时,我有一个上下文监听器,它调用CacheManager并加载一些数据,现在当它试图从CacheManager获取Cache时指定(cacheName,Class,Class)它返回NULL。

我正在使用声明式配置进行hazelcast,即我已经定义了hazelcast.xml,当服务器启动时,我可以看到它能够读取我的这个文件。

以下是我的ContextListener代码

public void contextInitialized(final ServletContextEvent arg0) {
    // TODO Auto-generated method stub
    PPCacheManager.loadClientConfiguration();
}

我的CacheManager类

public class PPCacheManager {

// Loads the default CacheProvider (HazelCast) from hazelcast.xml which is
// in classpath
private static CachingProvider defaultCachingProvider = Caching.getCachingProvider(); //
// Loads the default CacheManager from hazelcast.xml which is in classpath
private static CacheManager defaultCacheManager = defaultCachingProvider.getCacheManager();

public static void loadClientConfiguration() {

    final Set<String> keys = new HashSet<>();
    keys.add(PaymentConstants.P_CLIENT_DEF_RS);
    keys.add(PaymentConstants.P_CLIENT_PAM_RS);
    getClientCache().getAll(keys);

}

public static Cache<String, Object> getClientCache() {
    System.out.println("cache names :: " + defaultCacheManager.getCacheNames());
    return defaultCacheManager.getCache("clientCache", String.class, Object.class);
}

我的Hazelcas.xml

<cache name="clientCache">
    <key-type class-name="java.lang.String"></key-type>
    <value-type class-name="java.lang.Object"></value-type>
    <statistics-enabled>true</statistics-enabled>
    <management-enabled>true</management-enabled>
    <read-through>true</read-through>
    <cache-loader-factory class-name="com.test.test.cache.ClientConfigCacheLoader"/>
</cache>
<management-center enabled="true">http://myserver:myport/mancenter-3.4.2</management-center>

我的自定义CacheLoader类

public class ClientConfigCacheLoader implements CacheLoader<String, Object> {

private BaseDAO clientDetailsDAO;

@Autowired
public void setClientDetailsDAO(@Qualifier("clientDetailsDAO") final BaseDAO clientDetailsDAO) {
    this.clientDetailsDAO = clientDetailsDAO;
}

@Override
public Object load(final String key) throws CacheLoaderException {
    final Map<String, Object> clientMasterData = this.loadAll(null);
    return clientMasterData.get(key);
}

@Override
public Map<String, Object> loadAll(final Iterable<? extends String> keys) throws CacheLoaderException {
    // TODO Auto-generated method stub
    final LinkedHashMap<String, Object> inputMap = new LinkedHashMap<>(1);
    final Map<String, Object> clientMasterData = this.clientDetailsDAO.execute(inputMap);
    final String statusCode = (String) clientMasterData.get("P_STATUS_CD");
    if (JavaUtil.isNullOrEmpty(statusCode) || !PaymentConstants.SP_SUCCESS_CODE.equalsIgnoreCase(statusCode)) {
        throw new Exception(ErrorCode.PROCESSOR_ERROR, "Failed to Load the Client Configuration",
                null);
    }
    return clientMasterData;
}

}

服务器堆栈跟踪

    May 07, 2015 11:31:04 AM com.hazelcast.config.XmlConfigLocator
INFO: Loading 'hazelcast.xml' from classpath.
May 07, 2015 11:31:08 AM com.hazelcast.instance.DefaultAddressPicker
INFO: [LOCAL] [dev] [3.4.2] Prefer IPv4 stack is true.
May 07, 2015 11:31:08 AM com.hazelcast.instance.DefaultAddressPicker
INFO: [LOCAL] [dev] [3.4.2] Picked Address[172.16.190.180]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
May 07, 2015 11:31:23 AM com.hazelcast.spi.OperationService
INFO: [172.16.190.180]:5701 [dev] [3.4.2] Backpressure is disabled
May 07, 2015 11:31:23 AM com.hazelcast.spi.impl.BasicOperationScheduler
INFO: [172.16.190.180]:5701 [dev] [3.4.2] Starting with 2 generic operation threads and 4 partition operation threads.
May 07, 2015 11:31:26 AM com.hazelcast.system
INFO: [172.16.190.180]:5701 [dev] [3.4.2] Hazelcast 3.4.2 (20150326 - f6349a4) starting at Address[172.16.190.180]:5701
May 07, 2015 11:31:26 AM com.hazelcast.system
INFO: [172.16.190.180]:5701 [dev] [3.4.2] Copyright (C) 2008-2014 Hazelcast.com
May 07, 2015 11:31:26 AM com.hazelcast.core.LifecycleService
INFO: [172.16.190.180]:5701 [dev] [3.4.2] Address[172.16.190.180]:5701 is STARTING
May 07, 2015 11:31:30 AM com.hazelcast.instance.Node
WARNING: [172.16.190.180]:5701 [dev] [3.4.2] No join method is enabled! Starting standalone.
May 07, 2015 11:31:31 AM com.hazelcast.core.LifecycleService
INFO: [172.16.190.180]:5701 [dev] [3.4.2] Address[172.16.190.180]:5701 is STARTED
May 07, 2015 11:31:31 AM com.hazelcast.management.ManagementCenterService
INFO: [172.16.190.180]:5701 [dev] [3.4.2] Hazelcast will connect to Hazelcast Management Center on address: 
http://myserver:myport/mancenter-3.4.2
May 07, 2015 11:31:32 AM com.hazelcast.partition.InternalPartitionService
INFO: [172.16.190.180]:5701 [dev] [3.4.2] Initializing cluster partition table first arrangement...
cache names :: []
May 07, 2015 11:31:48 AM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class com.test.test.servlet.PPServicesListener
javax.cache.CacheException: java.lang.ClassCastException: com.test.test.cache.ClientConfigCacheLoader cannot be cast to javax.cache.configuration.Factory
    at com.hazelcast.cache.impl.HazelcastServerCacheManager.findConfig(HazelcastServerCacheManager.java:139)
    at com.hazelcast.cache.impl.AbstractHazelcastCacheManager.getCacheUnchecked(AbstractHazelcastCacheManager.java:198)
    at com.hazelcast.cache.impl.AbstractHazelcastCacheManager.getCache(AbstractHazelcastCacheManager.java:155)
    at com.hazelcast.cache.impl.AbstractHazelcastCacheManager.getCache(AbstractHazelcastCacheManager.java:47)
    at com.test.test.cache.PPCacheManager.getClientCache(PPCacheManager.java:68)
    at com.test.test.cache.PPCacheManager.loadClientConfiguration(PPCacheManager.java:41)
    at com.test.test.servlet.PPServicesListener.contextInitialized(PPServicesListener.java:20)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4728)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5162)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassCastException: com.test.test.cache.ClientConfigCacheLoader cannot be cast to javax.cache.configuration.Factory
    at com.hazelcast.config.CacheConfig.<init>(CacheConfig.java:96)
    at com.hazelcast.cache.impl.HazelcastServerCacheManager.findConfig(HazelcastServerCacheManager.java:134)
    ... 15 more

根据服务器堆栈跟踪,很明显它无法将我的custo CacheLoader强制转换为javax.cache.configuration.Factory

但有人可以告诉我它为什么会发生。

1 个答案:

答案 0 :(得分:1)

由于stacktrace提到你设置了一个类而不是java.cache.configuration.Factory。您必须在XML中设置Factory而不是CacheLoader本身的类名:

<cache-loader-factory class-name="com.test.test.cache.ClientConfigCacheLoaderFactory"/>

当然,您还必须先创建此工厂类。