Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeoutException when running example in doc #11009

Closed
leonchen83 opened this issue Dec 1, 2023 · 4 comments · Fixed by #11032
Closed

TimeoutException when running example in doc #11009

leonchen83 opened this issue Dec 1, 2023 · 4 comments · Fixed by #11032

Comments

@leonchen83
Copy link
Contributor

Jetty Version
jetth 12
Jetty Environment
ee10

Java Version

java 17

Question

dependency

        <dependency>
            <scope>test</scope>
            <version>2.1.0</version>
            <groupId>jakarta.websocket</groupId>
            <artifactId>jakarta.websocket-api</artifactId>
        </dependency>
         <dependency>
            <groupId>org.eclipse.jetty.websocket</groupId>
            <artifactId>jetty-websocket-jetty-client</artifactId>
            <version>12.0.3</version>
        </dependency>
        <dependency>
            <scope>test</scope>
            <groupId>org.eclipse.jetty.ee10.websocket</groupId>
            <artifactId>jetty-ee10-websocket-jakarta-server</artifactId>
            <version>12.0.3</version>
        </dependency>

code

public static void main(String[] args) throws Exception {
		Server server = new Server(8080);
		ServletContextHandler handler = new ServletContextHandler("/ctx");
		server.setHandler(handler);
		JakartaWebSocketServletContainerInitializer.configure(handler, (servletContext, container) -> {
			container.setDefaultMaxTextMessageBufferSize(128 * 1024);
			container.addEndpoint(ServerEndpointConfig.Builder.create(Test.class, "/ws").build());
		});
		server.start();
		
		WebSocketClient client = new WebSocketClient();
		client.start();
		Session session = client.connect(new Session.Listener.AutoDemanding() {
			public void onWebSocketOpen(Session session) {
				System.out.println("open");
			}
			
			public void onWebSocketText(String message) {
				System.out.println(message);
			}
		}, new URI("ws://127.0.0.1:8080/ctx/ws")).get(3, TimeUnit.SECONDS);
		
		var callback = new Callback.Completable();
		session.sendText("test", callback);
		callback.get();
	}
	
	public static class Test implements Session.Listener.AutoDemanding {
		public void onWebSocketOpen(Session session) {
			var callback = new Callback.Completable();
			session.sendText("connected", callback);
			try {
				callback.get();
			} catch (InterruptedException e) {
			} catch (ExecutionException e) {
			}
		}
		
		public void onWebSocketText(String message) {
			System.out.println(message);
		}
	}
@leonchen83
Copy link
Contributor Author

Exception in thread "main" java.util.concurrent.TimeoutException
	at java.base/java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1960)
	at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2095)

@lachlan-roberts
Copy link
Contributor

Note that Session.Listener.AutoDemanding is a Jetty WebSocket API endpoint, so you cannot register it as a Jarkarta websocket endpoint.

You are doing

ServerEndpointConfig.Builder.create(Test.class, "/ws")

The class used in this line should extend jakarta.websocket.Endpoint or have the @ServerEndpoint annotation.

@lachlan-roberts
Copy link
Contributor

But regardless you should be getting better error messages instead of just a timeout exception so I will look into this soon and see if it can be improved.

@lachlan-roberts
Copy link
Contributor

Here is a revision of your code which works.
You just need to use Jakarta APIs if you're using JakartaWebSocketServletContainerInitializer.
You can use the Jetty API instead of jakarta.websocket if you want but you cannot use Jetty API endpoints with the Jakarta initializer.

public static void main(String[] args) throws Exception {
    Server server = new Server(8080);
    ServletContextHandler handler = new ServletContextHandler("/ctx");
    server.setHandler(handler);
    JakartaWebSocketServletContainerInitializer.configure(handler, (servletContext, container) -> {
        container.setDefaultMaxTextMessageBufferSize(128 * 1024);
        container.addEndpoint(ServerEndpointConfig.Builder.create(Test.class, "/ws").build());
    });
    server.start();

    WebSocketClient client = new WebSocketClient();
    client.start();
    Session session = client.connect(new Session.Listener.AutoDemanding() {
        public void onWebSocketOpen(Session session) {
            System.out.println("client open");
        }

        public void onWebSocketText(String message) {
            System.out.println(message);
        }
    }, new URI("ws://127.0.0.1:8080/ctx/ws")).get(300, TimeUnit.SECONDS);

    var callback = new Callback.Completable();
    session.sendText("test", callback);
    callback.get();
}

public static class Test extends Endpoint implements MessageHandler.Whole<String> {
    @Override
    public void onOpen(jakarta.websocket.Session session, EndpointConfig config)
    {
        try
        {
            session.addMessageHandler(this);
            System.out.println("server open");
            session.getBasicRemote().sendText("connected");
        }
        catch (Throwable t)
        {
            t.printStackTrace(System.err);
        }
    }

    @Override
    public void onMessage(String message)
    {
        System.out.println(message);
    }
}

lachlan-roberts added a commit that referenced this issue Dec 7, 2023
lachlan-roberts added a commit that referenced this issue Dec 7, 2023
lachlan-roberts added a commit that referenced this issue Dec 7, 2023
lachlan-roberts added a commit that referenced this issue Dec 7, 2023
Signed-off-by: Lachlan Roberts <[email protected]>
sbordet pushed a commit that referenced this issue Dec 17, 2023
…ServerEndpointConfig (#11032)

* Issue #11009 - add test for bad Jakarta endpoint
* Issue #11009 - ensure endpoint deployable before adding ServerEndpointConfig
* add same test and fix for ee9

Signed-off-by: Lachlan Roberts <[email protected]>
@sbordet sbordet moved this to ✅ Done in Jetty 12.0.5 - FROZEN Dec 17, 2023
@joakime joakime changed the title Jetty 12 TimeoutException when running example in doc TimeoutException when running example in doc Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Status: ✅ Done
3 participants