org.glassfish.grizzly.http.ajp.AjpHandlerFilter.encodeHttpPacket(AjpHandlerFilter.java:282) 当使用 Apache 作为 GF 5 的负载平衡器时

org.glassfish.grizzly.http.ajp.AjpHandlerFilter.encodeHttpPacket(AjpHandlerFilter.java:282) when using Apache as load balancer for GF 5

我已经配置了一个带有 AJP 连接器的 Glassfish 5.0 实例,它可以用作负载平衡器,即使目前我只有一个 glassfish。

在 domain.xml 我有以下内容:

<http-service>
    <access-log></access-log>
    <virtual-server network-listeners="http-listener-1,http-listener-2,jk-connector" id="server"></virtual-server>
    <virtual-server network-listeners="admin-listener" id="__asadmin"></virtual-server>
</http-service>

其中 jk-connector 是我使用的负载均衡器。

 <network-config>
     ...
      <protocols>
          <protocol name="jk-connector">
            <http default-virtual-server="server">
              <file-cache></file-cache>
            </http>
          </protocol>
        </protocols>
    </network-config>

然后是监听器

    <network-listeners>
      <network-listener protocol="jk-connector" jk-enabled="true" port="8009" name="jk-connector" thread-pool="http-thread-pool" transport="tcp"></network-listener>
    </network-listeners>

我的Apachehttp.conf如下:

LoadModule headers_module modules/mod_headers.so
LoadModule jk_module modules/mod_jk.so

# Where to find workers.properties
JkWorkersFile conf/workers.properties
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"

<IfModule mod_rewrite.c>
        Options +Indexes
        Options +FollowSymlinks
        RewriteEngine on
        RewriteMap  uc int:toupper

        #rewrite per compatibilita' con vecchie URL dei servizi 
        RewriteRule ^/services/(.)(.*)$   /api/${uc:}Services?%{QUERY_STRING}  [PT]
        #RewriteRule ^/services/(.)(.*)$  http://%{HTTP_HOST}/api/${uc:}Services?%{QUERY_STRING}  [R=301,L]

        #solo nel caso di un GET
        RewriteCond  %{REQUEST_METHOD}  GET
        ReWriteRule ^(.*)/login.jsf;jsessionid=[A-Za-z0-9\.]+(.*)$ /login.jsf [L,R=301]
</IfModule>

例外情况是:

  GRIZZLY0013: Exception during FilterChain execution
java.lang.NullPointerException
    at org.glassfish.grizzly.http.ajp.AjpHandlerFilter.encodeHttpPacket(AjpHandlerFilter.java:282)
    at org.glassfish.grizzly.http.ajp.AjpHandlerFilter.handleWrite(AjpHandlerFilter.java:245)
    at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:111)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.filterchain.FilterChainContext.write(FilterChainContext.java:890)
    at org.glassfish.grizzly.filterchain.FilterChainContext.write(FilterChainContext.java:858)
    at org.glassfish.grizzly.http.io.OutputBuffer.flushBuffer(OutputBuffer.java:1092)
    at org.glassfish.grizzly.http.io.OutputBuffer.flushBinaryBuffers(OutputBuffer.java:1066)
    at org.glassfish.grizzly.http.io.OutputBuffer.flushAllBuffers(OutputBuffer.java:1036)
    at org.glassfish.grizzly.http.io.OutputBuffer.close(OutputBuffer.java:754)
    at org.apache.catalina.connector.OutputBuffer.close(OutputBuffer.java:244)
    at org.apache.catalina.connector.CoyoteOutputStream.close(CoyoteOutputStream.java:179)
    at java.nio.channels.Channels$WritableByteChannelImpl.implCloseChannel(Channels.java:469)
    at java.nio.channels.spi.AbstractInterruptibleChannel.close(AbstractInterruptibleChannel.java:115)
    at com.sun.faces.application.resource.ResourceHandlerImpl.handleResourceRequest(ResourceHandlerImpl.java:365)
    at javax.faces.application.ResourceHandlerWrapper.handleResourceRequest(ResourceHandlerWrapper.java:178)
    at org.primefaces.application.resource.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:87)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:667)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1580)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:338)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:100)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:250)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:145)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:250)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:250)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:652)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:591)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:463)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:168)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:242)
    at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access0(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
    at java.lang.Thread.run(Thread.java:748)

Using JDK 1.8.0_131, Glassfish 5.0, Apache 2.4.2

看来你正面临这个问题Glassfish bug (and this one)。

您可以在此处试用最新的 Glassfish 5 版本:http://download.oracle.com/glassfish/5.0.1/nightly

不幸的是,目前除了手动修复 grizzly JAR 文件外似乎没有其他解决方案。

您可以尝试以下方法:

  1. 下载 grizzly 2.4 源代码 (https://github.com/javaee/grizzly/archive/2.4.x.zip) 并提取它们
  2. 打开项目grizzly-http-ajp并构建它,如果由于缺少依赖导致构建失败,请在pom.xml
  3. 中将项目版本更改为2.4.3
  4. 如果编译没有问题,打开class AjpHandlerFilter
  5. 在第 276 行附近进行以下更改,使代码如下所示:

        if (contentBuffer.hasRemaining()) {
            return AjpMessageUtils.appendContentAndTrim(memoryManager,
                    encodedBuffer, contentBuffer);
        } else if (encodedBuffer == null) {
            encodedBuffer = Buffers.EMPTY_BUFFER;
        }
    
  6. 保存并编译项目

  7. 转到 /grizzly-2.4.x/modules/http-ajp/target/classes/org/glassfish/grizzly/http/ajp/ 并复制为 AjpHandlerFilter
  8. 生成的 .class 文件
  9. 更新 Glassfish 安装的 glassfish-grizzly-extras-all.jar(在 glassfish/glassfish/modules 内)中的 AjpHandlerFilter.class。您可以使用 jar uf ${jar-file} ${class-file} 执行此操作。您需要确保路径正确并且您替换了 JAR
  10. 中的旧 .class 文件

或者您可以尝试找到项目 glassfish-grizzly-extras-all 的源代码,解决那里的问题并构建一个新的 glassfish-grizzly-extras-all.jar.