2017-05-26 160 views
0

框架代碼中這個空指針的原因是什麼?我最近開始將一些應用程序堆棧從eGcache與JGroups遷移到Redis。作爲其中的一部分,我們正在使用ehcache作爲二級緩存來實現某些功能。我在客戶端庫(Jedis或Lettuce)的連接代碼中獲得了一致的NullPointerExceptions,其中部分(其中約900個)的集成測試。但是,緩存機制在正常應用程序運行期間可以正常工作,並且在某些集成測試期間緩存工作正常。現有的應用程序代碼使用了@Cacheable,這對我來說非常合適。我通過以下gradle這個依賴和緩存配置做了整合:Spring Data Redis NullPointerException with @Cacheable

​​

redisContext.xml

<bean id="lettucePool" class="org.springframework.data.redis.connection.lettuce.DefaultLettucePool"> 
    <property name="hostName" value="${redis.host}" /> 
    <property name="port" value="${redis.port}" /> 
    <property name="poolConfig" ref="lettucePoolConfiguration" /> 
</bean> 

<bean id="lettucePoolConfiguration" class="org.springframework.data.redis.connection.PoolConfig"> 
    <property name="maxIdle" value="900"/> 
    <property name="maxActive" value="1000" /> 
    <property name="maxTotal" value="1200" /> 
    <property name="testOnBorrow" value="true" /> 
    <property name="testOnReturn" value="true" /> 
</bean> 

<bean id="lettuceConnectionFactory" class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}"> 
    <constructor-arg index="0" ref="lettucePool"/> 
    <property name="validateConnection" value="true"></property> 
    <property name="shareNativeConnection" value="true"></property> 
</bean> 

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="lettuceConnectionFactory"> 
    <property name="keySerializer"> 
     <bean class="c.h.c.c.util.MultiKeySerializer"/> <!-- Used to translate SPeL array to key String --> 
    </property> 
    <property name="hashKeySerializer"> 
     <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> 
    </property> 
</bean> 

<bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"> 
    <constructor-arg index="0" ref="redisTemplate"></constructor-arg> 
    <property name="cacheNames"> 
     <set> 
      <value>cachenames......</value> 
     </set> 
    </property> 
    <property name="expires"> 
     <map> 
      <entry key="cachenames......" value="times...."/> 
     </map> 
    </property> 
</bean> 

cacheContext.xml

<cache:annotation-driven cache-manager="ehcacheManager"/> 
<cache:annotation-driven cache-manager="redisCacheManager"/> 

<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> 
    <property name="cacheManager" ref="ehcache" /> 
</bean> 

<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> 
    <property name="configLocation" value="classpath:ehcache.xml" /> 
    <property name="acceptExisting" value="true" /> 
</bean> 

<bean id="userCacheInstance" factory-bean="ehcacheManager" factory-method="getCache"> 
    <constructor-arg value="user" /> 
</bean> 

<import resource="classpath:redisContext.xml /> 

例外:

org.springframework.data.redis.RedisSystemException: Unknown redis exception; nested exception is java.lang.NullPointerException 
at org.springframework.data.redis.FallbackExceptionTranslationStrategy.getFallback(FallbackExceptionTranslationStrategy.java:48) 
at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:38) 
at org.springframework.data.redis.connection.lettuce.LettuceConnection.convertLettuceAccessException(LettuceConnection.java:305) 
at org.springframework.data.redis.connection.lettuce.LettuceConnection.get(LettuceConnection.java:1175) 
at sun.reflect.GeneratedMethodAccessor353.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at org.springframework.data.redis.core.CloseSuppressingInvocationHandler.invoke(CloseSuppressingInvocationHandler.java:57) 
at com.sun.proxy.$Proxy137.get(Unknown Source) 
at org.springframework.data.redis.cache.RedisCache$1.doInRedis(RedisCache.java:110) 
at org.springframework.data.redis.cache.RedisCache$1.doInRedis(RedisCache.java:106) 
at org.springframework.data.redis.cache.RedisCache$AbstractRedisCacheCallback.doInRedis(RedisCache.java:424) 
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:191) 
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:153) 
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:141) 
at org.springframework.data.redis.cache.RedisCache.get(RedisCache.java:105) 
at org.springframework.data.redis.cache.RedisCache.get(RedisCache.java:90) 
at org.springframework.cache.interceptor.AbstractCacheInvoker.doGet(AbstractCacheInvoker.java:68) 
at org.springframework.cache.interceptor.CacheAspectSupport.findInCaches(CacheAspectSupport.java:466) 
at org.springframework.cache.interceptor.CacheAspectSupport.findCachedItem(CacheAspectSupport.java:432) 
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:336) 
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:302) 
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) 
at com.sun.proxy.$Proxy93.getMasterAttributeValues(Unknown Source) 
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 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 
at org.springframework.cache.interceptor.CacheInterceptor$1.invoke(CacheInterceptor.java:52) 
at org.springframework.cache.interceptor.CacheAspectSupport.invokeOperation(CacheAspectSupport.java:320) 
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:353) 
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:302) 
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) 
at com.sun.proxy.$Proxy95.search(Unknown Source) 
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 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) 
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) 
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) 
at com.sun.proxy.$Proxy96.search(Unknown Source) 
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) 
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) 
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) 
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655) 
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 org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222) 
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) 
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) 
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814) 
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737) 
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) 
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) 
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) 
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) 
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668) 
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) 
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770) 
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167) 
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134) 
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:155) 
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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) 
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) 
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193) 
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:114) 
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:57) 
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:66) 
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51) 
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 org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) 
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) 
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32) 
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93) 
at com.sun.proxy.$Proxy3.processTestClass(Unknown Source) 
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:109) 
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 org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) 
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) 
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:147) 
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:129) 
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404) 
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63) 
at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:46) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
at java.lang.Thread.run(Thread.java:745) 

Caused by: java.lang.NullPointerException 
at com.lambdaworks.redis.protocol.CommandArgs.write(CommandArgs.java:121) 
at com.lambdaworks.redis.protocol.CommandArgs.addKey(CommandArgs.java:54) 
at com.lambdaworks.redis.BaseRedisCommandBuilder.createCommand(BaseRedisCommandBuilder.java:26) 
at com.lambdaworks.redis.RedisCommandBuilder.get(RedisCommandBuilder.java:357) 
at com.lambdaworks.redis.RedisAsyncConnectionImpl.get(RedisAsyncConnectionImpl.java:354) 
at sun.reflect.GeneratedMethodAccessor354.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at com.lambdaworks.redis.FutureSyncInvocationHandler.handleInvocation(FutureSyncInvocationHandler.java:63) 
at com.google.common.reflect.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:87) 
at com.sun.proxy.$Proxy136.get(Unknown Source) 
at org.springframework.data.redis.connection.lettuce.LettuceConnection.get(LettuceConnection.java:1173) 
... 139 more 

回答

0

好的,這裏有很多問題。他們都沒有使用Spring-Data-Redis或連接客戶端庫。第一個屬於我們如何生成我們的密鑰。如果你使用:

@Cacheable(....., key={#key1,#key2}) 

然後Spring會將生成的密鑰視爲一個ArrayList。由於ArrayList!= String,我無法獲得默認的org.springframework.data.redis.serializer.StringRedisSerializer,因爲它會運行到ClassCastException中。結果,我創建了MultiKeySerializer,當它獲得一個實例來從中生成密鑰時,循環訪問ArrayList。

public class MultiKeySerializer implements RedisSerializer<Object>{ 

private final Charset charset; 

public MultiKeySerializer() { 
    this(Charset.forName("UTF8")); 
} 

public MultiKeySerializer(Charset charset) { 
    Assert.notNull(charset); 
    this.charset = charset; 
} 

@Override 
public byte[] serialize(Object t) throws SerializationException { 

    if(t instanceof ArrayList){ 

     StringBuilder sb = new StringBuilder(); 
     ArrayList tList = (ArrayList) t; 
     for(Object o: tList){ 
      sb.append(o == null ? null: o.toString()); 
     } 

     String string = sb.toString(); 

     return (string == null ? null : string.getBytes(charset)); 
    } 
    // return null - used to return null. Root cause. 
    return t == null ? null : t.toString().getBytes(charset); 
} 

@Override 
public String deserialize(byte[] bytes) throws SerializationException { 

    return (bytes == null ? null : new String(bytes, charset)); 
} 
} 

的問題是,一些代碼的使用@Cacheable和沒有指定關鍵屬性。這導致Spring把它當作一個SimpleKey,從而通過關鍵的gen邏輯。雖然Spring-Data-Redis不是罪魁禍首,但如果它們在密鑰爲空時給出更具描述性的異常,那將會很好。

相關問題