RuedigerMoeller / kontraktor

distributed Actors for Java 8 / JavaScript
GNU Lesser General Public License v3.0
344 stars 48 forks source link

values set on HttpAppSession init are later null #66

Open davidwynter opened 6 years ago

davidwynter commented 6 years ago

Archive.zip Hi,

I am still using the old pattern for HttpApp and HttpAppSession, but found no material differences with the new example. The assertions fail in KontraktorTestHttpApp

@Test
public void testKontraktor() {
    webapp = (KontraktorTestHttpApp) new WebSocketConnectable(KontraktorTestHttpApp.class, "ws://127.0.0.1:8080/ws")
            .serType(SerializerType.JsonNoRef).connect().await();

    try {
        session = webapp.login().await(5000);
        assertTrue(session.app != null);
        assertTrue(session.testValue != null);
    } catch(AwaitException ae){
        System.err.println("Unable to get a user session - " + ae.getMessage());
    }
}

Where the app and testValue are set during the KontraktorTestHttpApp call to init the KontraktorTestHttpAppSession class. I also tried a direct non message for the init as it is actually void return in my cases. Got the feeling maybe I missing something obvious. Version 4.22

davidwynter commented 6 years ago

You can work around this by not using an init method but making explicit calls to set the app variable (and any others you need) on your session Actor that are not set yet. Kinda like lazy loading

davidwynter commented 6 years ago

Where is Ruediger?

RuedigerMoeller commented 6 years ago

quite busy at the moment :) srry

RuedigerMoeller commented 6 years ago

you are probably accessing the actor proxy. Each actor ref is actually a proxy with class == actor class but empty state. the actor proxy (== actor ref) is generated such that all calls are actually enqueued instead of being executed synchronously. However the state on actor proxy is always null. you can use session.getActorProxy().app, however you vialate the threading contract this way

davidwynter commented 6 years ago

Been thinking about this issue. I frequently need an object to refer to in the front end when I log in. Due to the use of the Actor proxy I cannot have fields on the session object. So I always need a separate roundtrip async call to the ..AppSession Actor to deliver the extra information I need.

Is it possible to alter the Session Actor to have some sort of general purpose instantiated Map<String,Object> where we can deliver the required data to the front end without this extra roundtrip?