在嵌入式 Jetty 上使用 javax.websocket 进行 JUnit 测试抛出 RejectedExecutionException: NonBlockingThread

Posted

技术标签:

【中文标题】在嵌入式 Jetty 上使用 javax.websocket 进行 JUnit 测试抛出 RejectedExecutionException: NonBlockingThread【英文标题】:JUnit test with javax.websocket on embedded Jetty throws RejectedExecutionException: NonBlockingThread 【发布时间】:2014-07-13 20:32:22 【问题描述】:

我正在尝试编写一个创建套接字并连接到嵌入式码头实例的测试用例。我正在使用

码头:9.2.0.RC0 javax.websocket-api & javax.websocket-client-api: 1.0 javax.websocket 服务器和客户端实现:9.1.5.v20140505

使用 websocket servlet 启动嵌入式码头服务器似乎工作正常。我从this example 获取了一些代码。然而这一行

...
container.connectToServer(SocketClient.class, uri);
...

抛出此异常

java.io.IOException: java.util.concurrent.RejectedExecutionException: org.eclipse.jetty.util.thread.NonBlockingThread@6f7476d
    at org.eclipse.jetty.websocket.client.WebSocketClient.initialiseClient(WebSocketClient.java:462)
    at org.eclipse.jetty.websocket.client.WebSocketClient.connect(WebSocketClient.java:187)
    at org.eclipse.jetty.websocket.jsr356.ClientContainer.connect(ClientContainer.java:135)
    at org.eclipse.jetty.websocket.jsr356.ClientContainer.connectToServer(ClientContainer.java:172)
    at com.playquickly.socket.SocketClient.connect(SocketClient.java:18)
    at com.playquickly.servlet.SocketServletTest.testSocket(SocketServletTest.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
Caused by: java.util.concurrent.RejectedExecutionException: org.eclipse.jetty.util.thread.NonBlockingThread@6f7476d
    at org.eclipse.jetty.util.thread.QueuedThreadPool.execute(QueuedThreadPool.java:361)
    at org.eclipse.jetty.io.SelectorManager.execute(SelectorManager.java:122)
    at org.eclipse.jetty.io.SelectorManager.doStart(SelectorManager.java:207)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
    at org.eclipse.jetty.websocket.client.io.ConnectionManager.doStart(ConnectionManager.java:200)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.websocket.client.WebSocketClient.initialiseClient(WebSocketClient.java:454)
    ... 30 more

这是我声明嵌入式码头服务器的包装器。

public class EmbeddedJetty 

    private final Logger LOG = Logger.getLogger(EmbeddedJetty.class.getSimpleName());

    private final int port;
    private Server server;

    public EmbeddedJetty(int port) 
        this.port = port;
    

    public void start() throws Exception 
        server = new Server();
        ServerConnector connector = new ServerConnector(server);
        connector.setPort(8080);
        server.addConnector(connector);

        // Setup the basic application "context" for this application at "/"
        // This is also known as the handler tree (in jetty speak)
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);

        try 
            // Initialize javax.websocket layer
            ServerContainer wscontainer = WebSocketServerContainerInitializer.configureContext(context);

            // Add WebSocket endpoint to javax.websocket layer
            wscontainer.addEndpoint(SocketServlet.class);
            System.out.println("Begin start");
            server.start();
            System.out.println("End start");
         catch (Throwable t) 
            t.printStackTrace(System.err);
        
    

    public void stop() throws Exception 
        server.stop();
        LOG.info("Jetty server stopped");
    

客户端端点

@ClientEndpoint(encoders = MessageCoder.class, decoders = MessageCoder.class)
public class SocketClient 

    public static Session connect(URI uri) throws Exception 
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();

        try 
            // Attempt Connect
            return container.connectToServer(SocketClient.class, uri);
         finally 
            // Force lifecycle stop when done with container.
            // This is to free up threads and resources that the
            // JSR-356 container allocates. But unfortunately
            // the JSR-356 spec does not handle lifecycles (yet)
            if (container instanceof LifeCycle) 
                ((LifeCycle) container).stop();
            
        
    

    @OnMessage
    public void onMessage(Message msg, Session session) 
        System.out.println(session.getId() + ": " + msg.toString());
    



还有测试

public class SocketServletTest 

    private static EmbeddedJetty server;

    @ClassRule
    public static final ExternalResource integrationServer = new ExternalResource() 
        @Override
        protected void before() throws Throwable 
            System.out.println("Starting...");
            server = new EmbeddedJetty(8080);
            server.start();
            System.out.println("Started");
        
    ;


    @Before
    public void setUp() throws Exception 

    

    @After
    public void shutdown() throws Exception 
        server.stop();
    

    @Test
    public void testSocket() throws Exception 
        URI uri = server.getWebsocketUri(SocketServlet.class);

        Session s1 = SocketClient.connect(uri);
    


【问题讨论】:

【参考方案1】:

不要混合使用不同版本的 Jetty。

这是 JSR-356 API 设计的一个不幸的副作用。 (Client 实现是根实现,Server 实现建立在它之上)

Client 容器按 JVM 初始化,每个服务器容器按 webapp 初始化。

您的堆栈跟踪不是您指出的 Jetty 9.2.0.RC0(行号已关闭)

https://github.com/eclipse/jetty.project/blob/jetty-9.2.0.RC0/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/io/ConnectionManager.java#L200

它们似乎来自 Jetty 9.1.5.v20140505

https://github.com/eclipse/jetty.project/blob/jetty-9.1.5.v20140505/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/io/ConnectionManager.java#L200

在任何地方都使用 Jetty 9.2.2.v20140723。

另外,使用这个版本意味着你可以摆脱 finally container.stop() hack。

【讨论】:

使用相同的码头版本肯定有帮助,谢谢。虽然现在我正在努力从服务器发送到客户端,因为测试结束并且连接显然已关闭。你有什么提示我可以为我的套接字服务器编写某种集成测试吗?感谢您的宝贵时间。

以上是关于在嵌入式 Jetty 上使用 javax.websocket 进行 JUnit 测试抛出 RejectedExecutionException: NonBlockingThread的主要内容,如果未能解决你的问题,请参考以下文章

在 Spring 中在嵌入式 Jetty 上配置 Spring Security

无法启动嵌入式 Jetty 服务器

有人在 Jetty 7 上成功部署了声纳吗?

嵌入 Jetty 10 的 GraphQL 订阅

嵌入 Jetty 并在 ServletContext 上设置默认属性

为啥有人会使用 jetty-maven-plugin 与嵌入式码头