Singleton Missing After Client Logout

I’m having trouble with a module losing a singleton that I’ve created when a client logs out.

Client Hook Example:

public class ClientHook extends AbstractClientModuleHook {

    @Override
    public void startup(ClientContext context, LicenseState activationState) throws Exception {
        super.startup(context, activationState);
        this.context = context;
        MySingleton.initialize(context);
    }

}

Then my singleton looks like:

public class MySingleton {

    private static final Logger logger = LoggerFactory.getLogger();
    private static MySingleton instance;

    private ReplayRequestEngine(ClientContext context){
        this.context = context;

    }

    public synchronized static void initialize(ClientContext context){
        logger.trace("Initializing Singleton Instance");

        if(instance == null)
            instance = new MySingleton(context);
    }

    public static ReplayRequestEngine getInstance(){
        return instance;
    }

}

The first time I run this code I can see the logger working, but after I log in and back out the logger doesn’t seem to work anymore, and neither do any methods of my singleton. Any ideas?

startup() is called once, when the client loads the module, not every time a login occurs.

edit: wrong!

My singleton should persist after that no?

Yeah, but I actually need to verify what I just said… I’m too used to working in Gateway scope.

1 Like

Actually looking at the logs, it seems that when I log back in it re-runs my client hook startup. I can see my logger from the client hook but for some reason I’m missing the logger in my singleton class…

public class ClientHook extends AbstractClientModuleHook {

@Override
public void startup(ClientContext context, LicenseState activationState) throws Exception {
    super.startup(context, activationState);
    this.context = context;
    logger.info("Initalizing Singleton Instance");
    MySingleton.initialize(context);
}

}

Yeah I think I’m wrong about that. So… carry on debugging, not sure why things are weird :slight_smile:

Ah I think I got it. I was depending on something in your ClientExecutionEngine in my singleton and I needed to force it to re-initialize. Setting my singleton to null in the ClientHook shutdown method seems to fix everything for me.

1 Like