jedis多线程异常
多线程下使用jedis会报一些奇怪的错误
[2013.09.13 11:17:19.280]redis.clients.jedis.exceptions.JedisConnectionException: Unknown reply: 2 [2013.09.13 11:17:19.280] at redis.clients.jedis.Protocol.process(Protocol.java:71) [2013.09.13 11:17:19.280] at redis.clients.jedis.Protocol.read(Protocol.java:122) [2013.09.13 11:17:19.280] at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:172) [2013.09.13 11:17:19.280] at redis.clients.jedis.Connection.getBulkReply(Connection.java:161) [2013.09.13 11:17:19.280] at redis.clients.jedis.Jedis.get(Jedis.java:65) [2013.09.13 11:17:19.280] at com.youku.index.web.manager.ProManager.getShowSearchCurve(ProManager.java:916) [2013.09.13 11:17:19.280] at com.youku.index.web.util.MergeFullkpi.mergeFull(MergeFullkpi.java:59) [2013.09.13 11:17:19.280] at com.youku.index.web.manager.ProManager.getAllShowVV(ProManager.java:836) [2013.09.13 11:17:19.280] at com.youku.index.web.action.ProAction.vrShow(ProAction.java:422) [2013.09.13 11:17:19.280] at sun.reflect.GeneratedMethodAccessor87.invoke(Unknown Source) [2013.09.13 11:17:19.280] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [2013.09.13 11:17:19.280] at java.lang.reflect.Method.invoke(Method.java:597) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252) [2013.09.13 11:17:19.280] at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265) [2013.09.13 11:17:19.280] at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [2013.09.13 11:17:19.280] at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [2013.09.13 11:17:19.280] at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [2013.09.13 11:17:19.280] at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:161) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [2013.09.13 11:17:19.280] at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189) [2013.09.13 11:17:19.280] at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [2013.09.13 11:17:19.280] at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54) [2013.09.13 11:17:19.280] at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:563) [2013.09.13 11:17:19.280] at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) [2013.09.13 11:17:19.280] at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99) [2013.09.13 11:17:19.280] at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:76) [2013.09.13 11:17:19.280] at com.caucho.server.webapp.DispatchFilterChain.doFilter(DispatchFilterChain.java:97) [2013.09.13 11:17:19.280] at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:241) [2013.09.13 11:17:19.280] at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:280) [2013.09.13 11:17:19.280] at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:108) [2013.09.13 11:17:19.280] at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213) [2013.09.13 11:17:19.280] at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171) [2013.09.13 11:17:19.280] at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145) [2013.09.13 11:17:19.280] at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92) [2013.09.13 11:17:19.280] at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381) [2013.09.13 11:17:19.280] at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:76) [2013.09.13 11:17:19.280] at com.caucho.server.cache.CacheFilterChain.doFilter(CacheFilterChain.java:158) [2013.09.13 11:17:19.280] at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:178) [2013.09.13 11:17:19.280] at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:241) [2013.09.13 11:17:19.280] at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:268) [2013.09.13 11:17:19.280] at com.caucho.server.port.TcpConnection.run(TcpConnection.java:586) [2013.09.13 11:17:19.280] at com.caucho.util.ThreadPool$Item.runTasks(ThreadPool.java:690) [2013.09.13 11:17:19.280] at com.caucho.util.ThreadPool$Item.run(ThreadPool.java:612) [2013.09.13 11:17:19.280] at java.lang.Thread.run(Thread.java:662)
jedis并不是线程安全的,于是在程序中改为每个线程只使用一个jedis实例。
较好的解决办法是使用JedisPool,见官方文档:https://github.com/xetorthio/jedis/wiki/Getting-started
先初始化一个池,可以设置一些参数,如setMaxActive,setMaxIdle等:
查看源代码打印帮助
JedisPool pool = new JedisPool(new JedisPoolConfig(), “localhost”);
然后就可以从池中取出一个实例:
Jedis jedis = pool.getResource(); try { /// ... do stuff here ... for example jedis.set("foo", "bar"); String foobar = jedis.get("foo"); jedis.zadd("sose", 0, "car"); jedis.zadd("sose", 0, "bike"); Set<String> sose = jedis.zrange("sose", 0, -1); } finally { /// ... it's important to return the Jedis instance to the pool once you've finished using it pool.returnResource(jedis); } /// ... when closing your application: pool.destroy();
用完要记得放回池,不用了需要销毁。
转自
http://www.rigongyizu.com/jedis-multithread-exception/
不过还没来得及实现,标记下……