很抱歉,我需要分享每一行代码。
我使用在jetty服务器上托管的Jersey创建了一个JAX-RS应用程序。然而,我的以下代码将进行无限循环。 Jetty不断发布消息并吃掉内存。仅在连接到CURL客户端时才会发生这种情况。
码头-服务器:
public class HttpServer {
public static void main(String[] args) throws Exception
{
//Set JAX-RS
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
context.setContextPath("/");
Server server = new Server(8888);
server.setHandler(context);
ServletHolder jerseyServlet = context.addServlet(ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
// Tells the Jersey Servlet which REST service/class to load.
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames",TestResource.class.getCanonicalName());
try {
server.start();
while(true){
server.join();
logger.log(Level.WARNING,"HTTP Server Killed. Restarting");
server.start();
}
} catch (Exception e) {
// TODO Auto-generated catch block
logger.log(Level.SEVERE,"EXCPETION DETECTED:" +e.hashCode());
}
}
}
Jax-RS资源
@Path("/test")
public class TestResource
{
@POST
@Consumes("application/json")
@Produces("text/html")
public Response fillData(String json)
{
System.out.println(json);
return Response.status(200).entity("Successfully updated").build();
}
}
需要注意的事项:
curl -H "Content-Type: application/json" -d '{"username":"xyz","password":"xyz"}' http://localhost:8888/test
无限循环调试日志:
11:21:40.760 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG org.eclipse.jetty.io.SelectorManager - Selector loop woken up from select, 0/1 selected
11:21:40.761 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG org.eclipse.jetty.io.SelectorManager - Running change org.eclipse.jetty.io.SelectChannelEndPoint$1@54ddf089
11:21:40.761 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG o.e.jetty.io.SelectChannelEndPoint - Updating key, state UPDATE_PENDING for SelectChannelEndPoint@55446de5{/127.0.0.1:55160<->8888,Open,ISHUT,out,R,-,30000,HttpConnection}{io=1,kio=0,kro=1}
11:21:40.761 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG o.e.jetty.io.SelectChannelEndPoint - Key interests update 0 -> 1 for SelectChannelEndPoint@55446de5{/127.0.0.1:55160<->8888,Open,ISHUT,out,R,-,30000,HttpConnection}{io=1,kio=0,kro=1}
11:21:40.761 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG org.eclipse.jetty.io.SelectorManager - Selector loop waiting on select
11:21:40.761 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG org.eclipse.jetty.io.SelectorManager - Selector loop woken up from select, 1/1 selected
11:21:40.761 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG o.e.jetty.io.SelectChannelEndPoint - Processing, state UPDATED for SelectChannelEndPoint@55446de5{/127.0.0.1:55160<->8888,Open,ISHUT,out,R,-,30000,HttpConnection}{io=1,kio=1,kro=1}
11:21:40.761 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG o.e.jetty.io.SelectChannelEndPoint - onSelected 1->0 for SelectChannelEndPoint@55446de5{/127.0.0.1:55160<->8888,Open,ISHUT,out,R,-,30000,HttpConnection}{io=0,kio=1,kro=1}
11:21:40.761 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG o.e.jetty.io.AbstractConnection - FILL_INTERESTED-->FILLING HttpConnection@21c7226e{FILLING}
11:21:40.762 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG o.e.jetty.io.SelectChannelEndPoint - Updating key, state UPDATE_PENDING for SelectChannelEndPoint@55446de5{/127.0.0.1:55160<->8888,Open,ISHUT,out,-,-,30000,HttpConnection}{io=0,kio=1,kro=1}
11:21:40.762 [qtp1669854350-19] DEBUG o.e.jetty.server.HttpConnection - HttpConnection@21c7226e{FILLING} onFillable HttpChannelState@36dadde6{s=IDLE i=true a=null}
11:21:40.762 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG o.e.jetty.io.SelectChannelEndPoint - Key interests update 1 -> 0 for SelectChannelEndPoint@55446de5{/127.0.0.1:55160<->8888,Open,ISHUT,out,-,-,30000,HttpConnection}{io=0,kio=1,kro=1}
11:21:40.762 [qtp1669854350-19] DEBUG org.eclipse.jetty.http.HttpParser - atEOF HttpParser{s=CLOSED,0 of -1}
11:21:40.762 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG org.eclipse.jetty.io.SelectorManager - Selector loop waiting on select
11:21:40.762 [qtp1669854350-19] DEBUG org.eclipse.jetty.http.HttpParser - parseNext s=CLOSED HeapByteBuffer@1[p=0,l=0,c=0,r=0]={<<<>>>}
11:21:40.762 [qtp1669854350-19] DEBUG o.e.jetty.io.AbstractConnection - fillInterested HttpConnection@21c7226e{FILLING}
11:21:40.762 [qtp1669854350-19] DEBUG o.e.jetty.io.AbstractConnection - FILLING-->FILLING_FILL_INTERESTED HttpConnection@21c7226e{FILLING_FILL_INTERESTED}
11:21:40.762 [qtp1669854350-19] DEBUG o.e.jetty.io.AbstractConnection - FILLING_FILL_INTERESTED-->FILL_INTERESTED HttpConnection@21c7226e{FILL_INTERESTED}
11:21:40.762 [qtp1669854350-19] DEBUG o.e.jetty.io.SelectChannelEndPoint - Changing interests in state UPDATED for SelectChannelEndPoint@55446de5{/127.0.0.1:55160<->8888,Open,ISHUT,out,R,-,30000,HttpConnection}{io=0,kio=0,kro=1}
11:21:40.762 [qtp1669854350-19] DEBUG o.e.jetty.io.SelectChannelEndPoint - changeInterests s=UPDATED 0->1 for SelectChannelEndPoint@55446de5{/127.0.0.1:55160<->8888,Open,ISHUT,out,R,-,30000,HttpConnection}{io=0,kio=0,kro=1}
11:21:40.762 [qtp1669854350-19] DEBUG org.eclipse.jetty.io.SelectorManager - Queued change org.eclipse.jetty.io.SelectChannelEndPoint$1@54ddf089
11:21:40.762 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG org.eclipse.jetty.io.SelectorManager - Selector loop woken
我对日志没有多大意义但有一点可以肯定的是,即使客户安全退出,Jetty也没有关闭连接。我尝试在码头设置超时,但没有用。
代码是否有任何问题,或者最新版本的Jetty似乎存在一些错误。请注意,我在开发JAVA EE应用程序时完全天真。如果这是一个错误,我将在Jetty论坛上报告。
答案 0 :(得分:3)
此问题已被识别为https://bugs.eclipse.org/bugs/show_bug.cgi?id=452465,并且已在master
分支中修复,并且将成为Jetty 9.3.0.M2的一部分。