new Stateful session bean instance without calling lookup
- by kislo_metal
Scenario:
I have @Singleton UserFactory (@Stateless could be) , its method createSession() generating @Stateful UserSession bean by manual lookup.
If I am injecting by DI @EJB - i will get same instance during calling fromFactory() method(as it should be)
What I want - is to get new instance of UserSession without preforming lookup.
Q1: how could I call new instance of @Stateful session bean?
Code:
@Singleton
@Startup
@LocalBean
public class UserFactory {
@EJB
private UserSession session;
public UserFactory() {
}
@Schedule(second = "*/1", minute = "*", hour = "*")
public void creatingInstances(){
try {
InitialContext ctx = new InitialContext();
UserSession session2 = (UserSession) ctx.lookup("java:global/inferno/lic/UserSession");
System.out.println("in singleton UUID " +session2.getSessionUUID());
} catch (NamingException e) {
e.printStackTrace();
}
}
@Schedule(second = "*/1", minute = "*", hour = "*")
public void fromFactory(){
System.out.println("in singleton UUID " +session.getSessionUUID());
}
public UserSession creatSession(){
UserSession session2 = null;
try {
InitialContext ctx = new InitialContext();
session2 = (UserSession) ctx.lookup("java:global/inferno/lic/UserSession");
System.out.println("in singleton UUID " +session2.getSessionUUID());
} catch (NamingException e) {
e.printStackTrace();
}
return session2;
}
}
As I understand, calling of
session.getClass().newInstance();
is not a best idea
Q2 : is it true?
I am using glassfish v3, ejb 3.1.