mardi 14 juillet 2015

Lookup with InitialContext lookup() and @Inject of Singleton

I have an @Singleton bean that I use to store state that is to be shared with other beans/threads. The state that is to be shared is maintained in a HashMap. Other beans that require the services of this Singleton simply do an @Inject and invoke the methods.

Recently, we had to introduce Quartz Scheduler since we had some asynchronous jobs to be done. In one quartz job I lookup the Singleton using IntialContext lookup(), and use methods provided by the Singleton. Once this job executes, the other beans that use @Inject (for the Singleton) no longer get information maintained in the HashMap.

@Singleton(mappedName = "ClientSessionMgr")
public class ClientSessionMgr {

    private final Map<String, ClientSession> clientSessions = new HashMap<String, ClientSession>();

    /* Methods that manage the clientSessions data */
}

One amongst a few beans that Inject the above Singleton.

@Stateless(mappedName = "NwdCredProfRepo")
public class NwdCredProfRepo {

  @Inject private ClientSessionMgr csm;

  /* Bean Methods that invoke some methods of the singleton */
}

The Quartz Job that does a lookup of the singleton and then uses the methods provided by the same.

public class ClientSyncProbe implements Job {
  private ClientSessionMgr csm;

  @Override
  public void execute(JobExecutionContext jec) throws JobExecutionException {
      InitialContent ic = new InitialContext();
      csm = (ClientSessionMgr) ic.lookup("java:app/SVWeb/ClientSessionMgr");

      /* Some tasks, and finally */
      ic.close()
  }
}

I am guessing that I am missing a pattern principle. Any help?

Aucun commentaire:

Enregistrer un commentaire