嵌入式码头无法连接

时间:2014-11-05 09:49:31

标签: java jetty server

嵌入式码头在应用程序中使用了遵循代码,但无法请求成功。 它等待着永远。

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();
}

但将Server 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:使用jetty-server 9.1.0.M0版本

1 个答案:

答案 0 :(得分:0)

您需要在线程池中至少包含最多6个:

  

"线程不足:max = 5<需要(接受者= 1 +选择器= 4 +   请求= 1)"

将构造函数更改为:

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

<强>更新

我的logback.xml(src / main / resources)

<?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 org.eclipse.jetty.server.Server@9f10da5d: 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)