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

@ApplicationScoped does not work correctly in combination with @ServerEndpoint #7234

Closed
Neoministein opened this issue Jul 23, 2023 · 3 comments
Assignees
Labels
3.x Issues for 3.x version branch
Projects

Comments

@Neoministein
Copy link

Environment Details

  • Helidon Version: 3.2.2
  • Helidon MP
  • JDK version: 17
  • OS: Win 10
  • Docker version (if applicable): -

Problem Description

I've got a websocket endpoint that I've registered with the @ServerEndpoint annoation. I've also added the CDI scope @ApplicationScoped to that class.

When I open multiple connections to the endpoint only instance is created. However when I close a connection the instance of the endpoint is destroyed and a new one is created and therefore all data in it is lost.

I would expect that since I annoated it with @ApplicationScoped that it wouldn't be destroyed and stay alive until the application is shut down.

Steps to reproduce

Here is a simple project with a test which recreates the issue.

Java code

I am using the following Helidon dependencies:

  • helidon-microprofile-core
  • helidon-microprofile-websocket
  • helidon-microprofile-tests-junit5

And non Helidon dependency:

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.9.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.tyrus</groupId>
            <artifactId>tyrus-container-grizzly-client</artifactId>
            <version>2.0.4</version>
            <scope>test</scope>
        </dependency>

Websocket endpoint:

package com.neo.example.issue.helidon;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.websocket.OnMessage;
import jakarta.websocket.OnOpen;
import jakarta.websocket.Session;
import jakarta.websocket.server.PathParam;
import jakarta.websocket.server.ServerEndpoint;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ApplicationScoped
@ServerEndpoint(value = "/websocket/{id}")
public class WebsocketEndpoint {

    protected Map<String, List<String>> messageMap = new HashMap<>();

    @OnOpen
    public void onOpen(@PathParam("id") String id, Session session) {
        messageMap.put(id, new ArrayList<>());
    }

    @OnMessage
    public void onMessage(@PathParam("id") String id, Session session, String message) {
        messageMap.get(id).add(message);
    }

    public Map<String, List<String>> getMessageMap() {
        return messageMap;
    }
}

Junit Test:

package com.neo.example.issue.helidon;

import io.helidon.microprofile.tests.junit5.HelidonTest;
import jakarta.inject.Inject;
import jakarta.websocket.*;
import jakarta.ws.rs.client.WebTarget;
import org.glassfish.tyrus.client.ClientManager;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.net.URI;
import java.util.List;
import java.util.Map;

@HelidonTest
class IssueTest {

    @Inject
    protected WebsocketEndpoint websocketEndpoint;

    @Inject
    protected WebTarget webTarget;

    protected Map<String, List<String>> messageMap;

    @Test
    void test() throws Exception {
        Session session_1 = connectToWebsocket("websocket/id-1");
        session_1.getBasicRemote().sendText("A message 1");

        Session session_2 = connectToWebsocket("websocket/id-2");
        session_2.getBasicRemote().sendText("A message 1");

        Thread.sleep(1000);

        messageMap = websocketEndpoint.getMessageMap();
        Assertions.assertEquals("A message 1", messageMap.get("id-1").get(0));
        session_2.close();

        messageMap = websocketEndpoint.getMessageMap();
        Assertions.assertEquals("A message 1", messageMap.get("id-1").get(0)); //<-- Throws NullPointerException for messageMap
    }

    public Session connectToWebsocket(String path) {
        Endpoint endpoint = new Endpoint() {
            @Override
            public void onOpen(Session session, EndpointConfig config) {}
        };

        try {
            return ClientManager.createClient().connectToServer(endpoint, new URI("ws://127.0.0.1:" + webTarget.getUri().getPort() + "/" + path));
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}
@github-actions github-actions bot added this to Triage in Backlog Jul 23, 2023
@spericas
Copy link
Member

As a workaround, does it work correctly if the you create a helper bean in application scope and inject it into the endpoint class?

@spericas spericas self-assigned this Jul 24, 2023
@Neoministein
Copy link
Author

It works correctly when using a helper bean in application scoped.

spericas added a commit to spericas/helidon that referenced this issue Jul 25, 2023
…sue helidon-io#7234.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
spericas added a commit that referenced this issue Jul 25, 2023
…sue #7234. (#7245)

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
Backlog automation moved this from Triage to Closed Jul 25, 2023
@Neoministein
Copy link
Author

Thanks for fixing it so fast!

Neoministein added a commit to Neoministein/NeoUtil that referenced this issue Jul 26, 2023
@github-actions github-actions bot added the 3.x Issues for 3.x version branch label Aug 3, 2023
spericas added a commit to spericas/helidon that referenced this issue Aug 8, 2023
…sue helidon-io#7234. (helidon-io#7245)

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
spericas added a commit that referenced this issue Aug 8, 2023
…sue #7234. (#7245) (#7340)

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
dalexandrov pushed a commit to dalexandrov/helidon that referenced this issue Aug 9, 2023
…sue helidon-io#7234. (helidon-io#7245) (helidon-io#7340)

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.x Issues for 3.x version branch
Projects
Backlog
  
Closed
Development

No branches or pull requests

2 participants