GlassFish Java Web 服务超时
GlassFish Java web service timeout
我正在设置一个新的 GlassFish Server 4.1.1,其中部署了一个 Java Web 服务。 Web 服务有一个 Web 方法,它接收 JSON 作为输入参数,因此它也 return 是一个 JSON。为了 serialize/deserialize Java objects to/from JSON 我正在使用 Gson 库。 Web 方法正在调用另一个 Web 服务,一个 运行 正在执行大约 2-3 分钟。例如,我在循环中调用此 Web 服务 6-10 次,因此 Web 方法等待大约 16-30 分钟以 return 一个值。问题是超时,但我不知道在哪里以及如何为我的问题设置超时。
我尝试从 Visual Studio C# 项目调用这个 Java Web 服务,我在其中添加了一个 Web 引用,我在其中定义了 Web 服务的 Web 引用 URL。我还设置了 XML Web 服务客户端等待对同步 XML Web 服务请求的回复到达的时间。我将它设置为 10 小时,如下所示。
static string CallEARSjDE(string json)
{
EARSWebReference.EARSWebService EARSWS = new EARSWebReference.EARSWebService();
EARSWS.Timeout = 36000000; // 10 hours
return EARSWS.EARSjDE(json);
}
我还为在 Java 网络方法中发出的网络服务请求设置了超时。我设置为1小时。
Gson gson = new Gson();
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1 * 3600000).build(); // timeout set to 1 hour
HttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
HttpPost post = new HttpPost(this.ServiceURL);
StringEntity postingString = new StringEntity(gson.toJson(problemLCSRequest));
post.setEntity(postingString);
post.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
ProblemLCSResponse responseJson = gson.fromJson(responseString, ProblemLCSResponse.class);
除了这两个超时之外,我还在 GlassFish 管理控制台中设置了一个超时。我在 Configurations → server-config → Web Container 部分完成了它,我将会话超时设置为 36.000 秒(10 小时)。
例如,当我在 web 方法中调用 web 服务 6 次时,我得到了所需的响应。但是当我调用 Web 服务超过 6 次时,它会抛出如下所示的异常。我觉得这个异常的原因是超时了,但是不知道是哪一个导致的,哪里可以改。
Warning: StandardWrapperValve[EARSWebService]: Servlet.service() for servlet EARSWebService threw exception
java.nio.channels.ClosedByInterruptException
at java.nio.channels.spi.AbstractInterruptibleChannel.end(AbstractInterruptibleChannel.java:202)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:478)
at org.glassfish.grizzly.nio.transport.TCPNIOUtils.flushByteBuffer(TCPNIOUtils.java:149)
at org.glassfish.grizzly.nio.transport.TCPNIOUtils.writeCompositeBuffer(TCPNIOUtils.java:87)
at org.glassfish.grizzly.nio.transport.TCPNIOAsyncQueueWriter.write0(TCPNIOAsyncQueueWriter.java:129)
at org.glassfish.grizzly.nio.transport.TCPNIOAsyncQueueWriter.write0(TCPNIOAsyncQueueWriter.java:106)
at org.glassfish.grizzly.nio.AbstractNIOAsyncQueueWriter.write(AbstractNIOAsyncQueueWriter.java:260)
at org.glassfish.grizzly.nio.AbstractNIOAsyncQueueWriter.write(AbstractNIOAsyncQueueWriter.java:169)
at org.glassfish.grizzly.nio.AbstractNIOAsyncQueueWriter.write(AbstractNIOAsyncQueueWriter.java:71)
at org.glassfish.grizzly.nio.transport.TCPNIOTransportFilter.handleWrite(TCPNIOTransportFilter.java:126)
at org.glassfish.grizzly.filterchain.TransportFilter.handleWrite(TransportFilter.java:191)
at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:111)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
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:1029)
at org.glassfish.grizzly.http.io.OutputBuffer.flushBinaryBuffers(OutputBuffer.java:1016)
at org.glassfish.grizzly.http.io.OutputBuffer.flushAllBuffers(OutputBuffer.java:987)
at org.glassfish.grizzly.http.io.OutputBuffer.flush(OutputBuffer.java:738)
at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:291)
at org.apache.catalina.connector.OutputBuffer.flush(OutputBuffer.java:275)
at org.apache.catalina.connector.CoyoteOutputStream.flush(CoyoteOutputStream.java:175)
at java.io.FilterOutputStream.flush(FilterOutputStream.java:140)
at com.ctc.wstx.io.UTF8Writer.flush(UTF8Writer.java:103)
at com.ctc.wstx.sw.BufferingXmlWriter.flush(BufferingXmlWriter.java:225)
at com.ctc.wstx.sw.BaseStreamWriter.flush(BaseStreamWriter.java:259)
at com.sun.xml.ws.util.xml.XMLStreamWriterFilter.flush(XMLStreamWriterFilter.java:71)
at com.sun.xml.ws.streaming.XMLStreamWriterUtil.getOutputStream(XMLStreamWriterUtil.java:103)
at com.sun.xml.ws.message.jaxb.JAXBMessage.writePayloadTo(JAXBMessage.java:403)
at com.sun.xml.ws.message.AbstractMessageImpl.writeTo(AbstractMessageImpl.java:192)
at com.sun.xml.ws.api.message.MessageWrapper.writeTo(MessageWrapper.java:226)
at com.sun.xml.ws.encoding.StreamSOAPCodec.encode(StreamSOAPCodec.java:144)
at com.sun.xml.ws.encoding.SOAPBindingCodec.encode(SOAPBindingCodec.java:242)
at com.sun.xml.ws.transport.http.HttpAdapter.encodePacket(HttpAdapter.java:636)
at com.sun.xml.ws.transport.http.HttpAdapter.access0(HttpAdapter.java:108)
at com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:878)
at com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:422)
at com.sun.xml.ws.transport.http.servlet.ServletAdapter.handle(ServletAdapter.java:169)
at org.glassfish.webservices.JAXWSServlet.doPost(JAXWSServlet.java:169)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
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:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
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:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:748)
终于,我找到了正确的超时设置。我必须转到管理面板,在 Configurations → server-config → Network Config
中我必须设置 http-listener-1 的 HTTP 请求超时。
我正在设置一个新的 GlassFish Server 4.1.1,其中部署了一个 Java Web 服务。 Web 服务有一个 Web 方法,它接收 JSON 作为输入参数,因此它也 return 是一个 JSON。为了 serialize/deserialize Java objects to/from JSON 我正在使用 Gson 库。 Web 方法正在调用另一个 Web 服务,一个 运行 正在执行大约 2-3 分钟。例如,我在循环中调用此 Web 服务 6-10 次,因此 Web 方法等待大约 16-30 分钟以 return 一个值。问题是超时,但我不知道在哪里以及如何为我的问题设置超时。
我尝试从 Visual Studio C# 项目调用这个 Java Web 服务,我在其中添加了一个 Web 引用,我在其中定义了 Web 服务的 Web 引用 URL。我还设置了 XML Web 服务客户端等待对同步 XML Web 服务请求的回复到达的时间。我将它设置为 10 小时,如下所示。
static string CallEARSjDE(string json)
{
EARSWebReference.EARSWebService EARSWS = new EARSWebReference.EARSWebService();
EARSWS.Timeout = 36000000; // 10 hours
return EARSWS.EARSjDE(json);
}
我还为在 Java 网络方法中发出的网络服务请求设置了超时。我设置为1小时。
Gson gson = new Gson();
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1 * 3600000).build(); // timeout set to 1 hour
HttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
HttpPost post = new HttpPost(this.ServiceURL);
StringEntity postingString = new StringEntity(gson.toJson(problemLCSRequest));
post.setEntity(postingString);
post.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
ProblemLCSResponse responseJson = gson.fromJson(responseString, ProblemLCSResponse.class);
除了这两个超时之外,我还在 GlassFish 管理控制台中设置了一个超时。我在 Configurations → server-config → Web Container 部分完成了它,我将会话超时设置为 36.000 秒(10 小时)。
例如,当我在 web 方法中调用 web 服务 6 次时,我得到了所需的响应。但是当我调用 Web 服务超过 6 次时,它会抛出如下所示的异常。我觉得这个异常的原因是超时了,但是不知道是哪一个导致的,哪里可以改。
Warning: StandardWrapperValve[EARSWebService]: Servlet.service() for servlet EARSWebService threw exception java.nio.channels.ClosedByInterruptException at java.nio.channels.spi.AbstractInterruptibleChannel.end(AbstractInterruptibleChannel.java:202) at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:478) at org.glassfish.grizzly.nio.transport.TCPNIOUtils.flushByteBuffer(TCPNIOUtils.java:149) at org.glassfish.grizzly.nio.transport.TCPNIOUtils.writeCompositeBuffer(TCPNIOUtils.java:87) at org.glassfish.grizzly.nio.transport.TCPNIOAsyncQueueWriter.write0(TCPNIOAsyncQueueWriter.java:129) at org.glassfish.grizzly.nio.transport.TCPNIOAsyncQueueWriter.write0(TCPNIOAsyncQueueWriter.java:106) at org.glassfish.grizzly.nio.AbstractNIOAsyncQueueWriter.write(AbstractNIOAsyncQueueWriter.java:260) at org.glassfish.grizzly.nio.AbstractNIOAsyncQueueWriter.write(AbstractNIOAsyncQueueWriter.java:169) at org.glassfish.grizzly.nio.AbstractNIOAsyncQueueWriter.write(AbstractNIOAsyncQueueWriter.java:71) at org.glassfish.grizzly.nio.transport.TCPNIOTransportFilter.handleWrite(TCPNIOTransportFilter.java:126) at org.glassfish.grizzly.filterchain.TransportFilter.handleWrite(TransportFilter.java:191) at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:111) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200) at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132) at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111) 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:1029) at org.glassfish.grizzly.http.io.OutputBuffer.flushBinaryBuffers(OutputBuffer.java:1016) at org.glassfish.grizzly.http.io.OutputBuffer.flushAllBuffers(OutputBuffer.java:987) at org.glassfish.grizzly.http.io.OutputBuffer.flush(OutputBuffer.java:738) at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:291) at org.apache.catalina.connector.OutputBuffer.flush(OutputBuffer.java:275) at org.apache.catalina.connector.CoyoteOutputStream.flush(CoyoteOutputStream.java:175) at java.io.FilterOutputStream.flush(FilterOutputStream.java:140) at com.ctc.wstx.io.UTF8Writer.flush(UTF8Writer.java:103) at com.ctc.wstx.sw.BufferingXmlWriter.flush(BufferingXmlWriter.java:225) at com.ctc.wstx.sw.BaseStreamWriter.flush(BaseStreamWriter.java:259) at com.sun.xml.ws.util.xml.XMLStreamWriterFilter.flush(XMLStreamWriterFilter.java:71) at com.sun.xml.ws.streaming.XMLStreamWriterUtil.getOutputStream(XMLStreamWriterUtil.java:103) at com.sun.xml.ws.message.jaxb.JAXBMessage.writePayloadTo(JAXBMessage.java:403) at com.sun.xml.ws.message.AbstractMessageImpl.writeTo(AbstractMessageImpl.java:192) at com.sun.xml.ws.api.message.MessageWrapper.writeTo(MessageWrapper.java:226) at com.sun.xml.ws.encoding.StreamSOAPCodec.encode(StreamSOAPCodec.java:144) at com.sun.xml.ws.encoding.SOAPBindingCodec.encode(SOAPBindingCodec.java:242) at com.sun.xml.ws.transport.http.HttpAdapter.encodePacket(HttpAdapter.java:636) at com.sun.xml.ws.transport.http.HttpAdapter.access0(HttpAdapter.java:108) at com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:878) at com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:422) at com.sun.xml.ws.transport.http.servlet.ServletAdapter.handle(ServletAdapter.java:169) at org.glassfish.webservices.JAXWSServlet.doPost(JAXWSServlet.java:169) at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283) at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167) 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:235) at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:119) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200) at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132) at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111) at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536) 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:591) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571) at java.lang.Thread.run(Thread.java:748)
终于,我找到了正确的超时设置。我必须转到管理面板,在 Configurations → server-config → Network Config
中我必须设置 http-listener-1 的 HTTP 请求超时。