即使输入的值不为null,infinispan cache.put()也会引发null指针异常

时间:2018-08-21 07:50:07

标签: caching infinispan

尝试将内容添加到infinispan缓存中,当我使用put方法时,出现空指针异常。我的是一个JSF Web应用程序。我们正在尝试使用wildfly 13实现会话复制。

import org.infinispan.Cache;
public class index extends AbstractPageBean implements java.io.Serializable {
    @Resource(lookup = "java:jboss/infinispan/cache/web/web_repl")  
    Cache<String, String> ilsCache;  
    public Cache<String, String> getCache() {
        return ilsCache;
    }

    public void prerender() {
        ilsCache.put("Message", "Hello");
        logger.info("CACHE " + getCache());
    }

日志文件

2018-08-21 14:21:57,134 ERROR [biz.autoscan.ils.index] (default task-1) index.java::Exception occured  : java.lang.NullPointerException
    at biz.autoscan.ils.index.btnLogin_action(index.java:930)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:181)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:289)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830)
    at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1985)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1487)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1378)
    at java.lang.Thread.run(Thread.java:745)

知道为什么我仍然从cache.put()获得空指针异常吗?

编辑:我们在域模式下使用WildFly 13,在复制模式下使用Infinispan。我的目标是将会话对象存储在infinispan缓存中,并在所有节点之间共享它。

domain.xml中的Infinispan子系统:这就是我配置infinispan的方式。目前我们仅使用两台服务器,因此我们采用了复制模式。

<subsystem xmlns="urn:jboss:domain:infinispan:6.0">
    <cache-container name="web" aliases="ilsee" default-cache="web_repl" module="org.wildfly.clustering.web.infinispan">
        <transport channel="ee" lock-timeout="60000"/>
        <local-cache name="Username"/>
        <replicated-cache name="web_repl">
            <transaction mode="BATCH"/>
        </replicated-cache>
        <distributed-cache name="dist">
            <transaction mode="NON_XA"/>
            <file-store/>
        </distributed-cache>
    </cache-container>
</subsystem>

1 个答案:

答案 0 :(得分:1)

答案取决于您如何配置服务器。

我将提供一个通用示例。 首先,您需要配置standalone.xml文件

<subsystem xmlns="urn:infinispan:server:core:9.2">
 <cache-container module="org.infinispan.extension:ispn-9.2" name="infinispan_container" default-cache="default">
   <transport/>
   <global-state/>
   <distributed-cache name="default"/>
   <distributed-cache name="myCache"/>
 </cache-container>
</subsystem>

然后在Java应用程序中,您需要使用注入资源

@Resource(lookup = "java:jboss/datagrid-infinispan/container/infinispan_container/cache/myCache")
Cache cache;

您会看到 infinispan_container myCache 是您在XML配置文件中提供的值。

应用程序在Wildfly中使用Infinispan的方法有两种:嵌入式模式和服务器模式。 您可以找到更多信息http://infinispan.org/docs/stable/user_guide/user_guide.html#usage_3