new Stateful session bean instance without calling lookup
Posted
by kislo_metal
on Stack Overflow
See other posts from Stack Overflow
or by kislo_metal
Published on 2010-05-09T18:23:32Z
Indexed on
2010/05/09
19:08 UTC
Read the original article
Hit count: 227
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.
© Stack Overflow or respective owner