1

我在我的 Web 应用程序中使用 Jache 和 Hazelcast 作为实现,当我在 tomcat 上部署我的应用程序时,我有一个上下文侦听器,它调用 CacheManager 并加载一些数据,现在当它尝试从指定的 CacheManager 获取缓存时(cacheName,类,类)它返回 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

但是有人可以告诉我为什么会这样。

4

1 回答 1

1

正如堆栈跟踪所提到的,您设置了一个类而不是 java.cache.configuration.Factory。您必须在 XML 中设置 Factory 而不是 CacheLoader 本身的类名:

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

当然,您还必须首先创建这个工厂类。

于 2015-05-07T16:26:40.133 回答