尝试在我的WebApp中包含Elasticsearch Java客户端时遇到问题。我有所需的依赖关系,我正在初始化/销毁Spring bean中创建客户端(作为简单测试),如下所示:
@Override public void afterPropertiesSet() throws Exception { try { client = new TransportClient(); InetSocketTransportAddress addr = new InetSocketTransportAddress("localhost", 9300); ((TransportClient) client).addTransportAddress(addr); } catch(Exception e){ System.out.println(e.toString()); } }
相应的destroy()将关闭客户端。
一切看起来正常,上下文开始正常,但是访问JSP时,我收到以下异常:
SEVERE: Servlet.service() for servlet [jsp] in context with path [/Fountain] threw exception [Filter execution threw an exception] with root cause java.lang.LinkageError: loader constraint violation in interface itable initialization: when resolving method "javax.servlet.ServletResponseWrapper.getOutputStream()Ljavax/servlet/ServletOutputStream;" the class loader (instance of org/apache/catalina/loader/WebappClassLoader) of the current class, javax/servlet/ServletResponseWrapper, and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for interface javax/servlet/ServletResponse have different Class objects for the type vlet/ServletOutputStream; used in the signature at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:186) at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:181) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:581) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) 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:722) Jan 10, 2014 10:44:46 AM org.apache.catalina.core.ApplicationDispatcher invoke SEVERE: Servlet.service() for servlet jsp threw exception java.lang.LinkageError: loader constraint violation: when resolving method "javax.servlet.jsp.JspFactory.getPageContext(Ljavax/servlet/Servlet;Ljavax/servlet/ServletRequest;Ljavax/servlet/ServletResponse;Ljava/lang/String;ZIZ)Ljavax/servlet/jsp/PageContext;" the class loader (instance of org/apache/jasper/servlet/JasperLoader) of the current class, org/apache/jsp/jsp/ajaxerror_jsp, and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for resolved class, javax/servlet/jsp/JspFactory, have different Class objects for the type t/ServletRequest;Ljavax/servlet/ServletResponse;Ljava/lang/String;ZIZ)Ljavax/servlet/jsp/PageContext; used in the signature at org.apache.jsp.jsp.ajaxerror_jsp._jspService(ajaxerror_jsp.java:59) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:489) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339) at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:467) at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:412) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:201) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) 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:722) Jan 10, 2014 10:44:46 AM org.apache.catalina.core.StandardHostValve custom SEVERE: Exception Processing ErrorPage[exceptionType=java.lang.Throwable, location=/jsp/ajaxerror.jsp] org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.LinkageError: loader constraint violation: when resolving method "javax.servlet.jsp.JspFactory.getPageContext(Ljavax/servlet/Servlet;Ljavax/servlet/ServletRequest;Ljavax/servlet/ServletResponse;Ljava/lang/String;ZIZ)Ljavax/servlet/jsp/PageContext;" the class loader (instance of org/apache/jasper/servlet/JasperLoader) of the current class, org/apache/jsp/jsp/ajaxerror_jsp, and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for resolved class, javax/servlet/jsp/JspFactory, have different Class objects for the type t/ServletRequest;Ljavax/servlet/ServletResponse;Ljava/lang/String;ZIZ)Ljavax/servlet/jsp/PageContext; used in the signature at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:489) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339) at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:467) at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:412) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:201) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) 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:722) Caused by: javax.servlet.ServletException: java.lang.LinkageError: loader constraint violation: when resolving method "javax.servlet.jsp.JspFactory.getPageContext(Ljavax/servlet/Servlet;Ljavax/servlet/ServletR equest;Ljavax/servlet/ServletResponse;Ljava/lang/String;ZIZ)Ljavax/servlet/jsp/PageContext;" the class loader (instance of org/apache/jasper/servlet/JasperLoader) of the current class, org/apache/jsp/jsp/ajaxerror_jsp, and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for resolved class, javax/servlet/jsp/JspFactory, have different Class objects for the type t/ServletRequest;Ljavax/servlet/ServletResponse;Ljava/lang/String;ZIZ)Ljavax/servlet/jsp/PageContext; used in the signature at org.apache.jsp.jsp.ajaxerror_jsp._jspService(ajaxerror_jsp.java:130) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) ... 22 more Caused by: java.lang.LinkageError: loader constraint violation: when resolving method "javax.servlet.jsp.JspFactory.getPageContext(Ljavax/servlet/Servlet;Ljavax/servlet/ServletRequest;Ljavax/servlet/ServletResponse;Ljava/lang/String;ZIZ)Ljavax/servlet/jsp/PageContext;" the class loader (instance of org/apache/jasper/servlet/JasperLoader) of the current class, org/apache/jsp/jsp/ajaxerror_jsp, and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for resolved class, javax/servlet/jsp/JspFactory, have different Class objects for the type t/ServletRequest;Ljavax/servlet/ServletResponse;Ljava/lang/String;ZIZ)Ljavax/servlet/jsp/PageContext; used in the signature at org.apache.jsp.jsp.ajaxerror_jsp._jspService(ajaxerror_jsp.java:59) ... 25 more
没有这种情况,上下文将启动并且将正确服务JSP。
有谁知道这可能是什么原因?我假设elasticsearch客户端使用的是网络应用所需的类,但在其他类加载器中将其实例化。
我尝试通过使用ImmutableSettings显式设置elasticsearch使用的类加载器,但这似乎没有任何区别:
ClassLoader contextClassLoader = thread.getContextClassLoader(); try { //thread.setContextClassLoader(ClassLoader.getSystemClassLoader()); Settings s = ImmutableSettings.settingsBuilder() .classLoader(contextClassLoader) .build();
任何帮助,将不胜感激!谢谢!
好了,设法解决了这个问题:
获取对tomcat’common’类加载器的引用,并将其传递给Elasticsearch客户端:
ClassLoader contextClassLoader = thread.getContextClassLoader(); try { Settings settings = ImmutableSettings.settingsBuilder().classLoader(contextClassLoader.getParent()).build(); client = new TransportClient(settings);
这样做的副作用是必须将names.txt文件从elasticSearch jar中移到“通用”类路径上的某个位置。
我仍然没有找到为什么这个问题会影响我的特定堆栈而不是其他堆栈的原因,但希望这样做并在此处进行更新。