mercredi 17 juin 2015

storing fields in Java ee websocket

i have created a javaee websocket endpoint using a PathParam. The parameter and the session are each stored in a field @OnOpen.

The websocket listens to some event and send some data out to session (using the path parameter).

The problem is that both fields are null at the moment event is caught. What is the cause of this? Are there multiple instances of endpoint per session? I read the spec : "If the developer uses the default ServerEndpointConfig.Configurator, there will be precisely one endpoint instance per active client connection. " Here is example code:

@ServerEndpoint(value = "/allbookings/{projectId}", encoders = {JSONEncoder.class},
                configurator = WebSocketUserConfig.class)
public class BookingsByProject {

    private Session session;
    private String  projectId;

    @OnOpen
    public void open(Session session, EndpointConfig conf, @PathParam(PROJECT_ID_KEY) String projectId) {
        if(projectId!=null){
            this.session = session;
            this.projectId = projectId;
        } else {
            session.close();
        }
    }

    @OnClose
    public void close(Session session, CloseReason reason) {
        log.debug("Session closed.");
    }

    public void createdEventFired(@Observes @Created Booking entity) {
        if(session.isOpen){
            session.getAsyncRemote().sendObject(converter.from(null, repo.getBookingsByProjectId(projectId)));
        }
    }

}

The problem is that on createdEventFired session and projectId are always null! Why?

Aucun commentaire:

Enregistrer un commentaire