2014-11-05 108 views
0

嵌入式碼頭在應用程序中跟隨代碼,但不能請求成功。 它永遠等待。嵌入式碼頭不能連接

public static class TestHandler extends ContextHandler { 
    public TestHandler(String contextPath) { 
     super(contextPath); 
    } 

    @Override 
    public void doHandle(String target, Request baseRequest, HttpServletRequest request, 
      HttpServletResponse response) throws IOException, ServletException { 
     super.doHandle(target, baseRequest, request, response); 
     System.out.println(""); 
    } 
} 

public static void main(String[] args) throws Exception { 

    Server server = new Server(new QueuedThreadPool(5, 4)); 
    ServerConnector connector = new ServerConnector(server); 
    connector.setPort(9001); 
    server.setConnectors(new Connector[] { connector }); 

    HandlerCollection handler = new HandlerCollection(); 
    ContextHandlerCollection contexts = new ContextHandlerCollection(); 
    contexts.setHandlers(new Handler[] { new TestHandler("/test") }); 

    handler.setHandlers(new Handler[] { contexts, new DefaultHandler() }); 
    server.setHandler(handler); 
    server.start(); 
    server.dumpStdErr(); 
    server.join(); 
} 

但改變了服務器的contstructor到另一個,它會好的,有人可以解釋原因嗎? 我跟蹤代碼,找不到原因。謝謝!!

public static void main(String[] args) throws Exception { 
    Server server = new Server(9001); 

    // Server server = new Server(new QueuedThreadPool(5, 4)); 
    // ServerConnector connector = new ServerConnector(server); 
    // connector.setPort(9001); 
    // server.setConnectors(new Connector[] { connector }); 

    HandlerCollection handler = new HandlerCollection(); 
    ContextHandlerCollection contexts = new ContextHandlerCollection(); 
    contexts.setHandlers(new Handler[] { new TestHandler("/test") }); 

    handler.setHandlers(new Handler[] { contexts, new DefaultHandler() }); 
    server.setHandler(handler); 
    server.start(); 
    server.dumpStdErr(); 
    server.join(); 
} 

PS:帶碼頭服務器9.1.0.M0版本

回答

0

你需要有至少一個線程池中的最大6:

「線程不足:最大= 5 <需要(受體= 1個+選擇= 4 + 請求= 1)」

變化的構造這樣:

Server server = new Server(new QueuedThreadPool(60, 6)); 

更新:

我logback.xml中(src /主/資源)

<?xml version="1.0" encoding="UTF-8"?> 
<configuration scan="false" scanPeriod="10 seconds"> 

<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> 
    <Target>System.out</Target> 
    <encoder> 
     <pattern>%p %d{HH:mm:ss} [%c{1}] - %m%n</pattern> 
    </encoder> 
</appender> 

<logger name="org.eclipse.jetty" level="DEBUG" /> 

<root level="INFO"> 
    <appender-ref ref="stdout" /> 
</root> 

堆棧跟蹤:

... 
INFO 11:25:18 [o.e.j.s.Server] - jetty-9.3.0.M0 
WARN 11:25:18 [o.e.j.u.c.AbstractLifeCycle] - FAILED [email protected]: java.lang.IllegalStateException: Insufficient threads: max=5 < needed(acceptors=1 + selectors=4 + request=1) 
java.lang.IllegalStateException: Insufficient threads: max=5 < needed(acceptors=1 + selectors=4 + request=1) 
at org.eclipse.jetty.server.Server.doStart(Server.java:351) ~[jetty-server-9.3.0.M0.jar:9.3.0.M0] 
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) ~[jetty-util-9.3.0.M0.jar:9.3.0.M0] 
at TestHandler.main(TestHandler.java:43) [classes/:na] 
Exception in thread "main" java.lang.IllegalStateException: Insufficient threads: max=5 < needed(acceptors=1 + selectors=4 + request=1) 
at org.eclipse.jetty.server.Server.doStart(Server.java:351) 
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) 
at TestHandler.main(TestHandler.java:43) 
+0

我瓚ge日誌級別爲DEBUG,沒有找到消息,哪個java文件用這個消息存在?謝謝 – nealons 2014-11-05 10:09:07

+0

我使用Slf4J + Logback作爲日誌框架。我認爲你的應用程序的一些消息沒有被記錄,請參閱[這裏](http://www.eclipse.org/jetty/documentation/current/configuring-logging.html#selecting-log-framework)。 – Stefan 2014-11-05 10:15:16

+0

我也用slf4j + logback – nealons 2014-11-05 10:16:42