1

设想:

我有@Singleton UserFactory@Stateless可能是),它的方法是通过手动查找createSession()生成bean。@Stateful UserSession

如果我通过 DI 注入@EJB- 我将在调用方法期间获得相同的实例fromFactory()(应该如此)

我想要的 - 是在UserSession不进行查找的情况下获得新实例。

Q1:如何调用@Stateful会话 bean 的新实例?

代码:

@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;

    }



}

据我了解,调用

session.getClass().newInstance();

不是最好的主意

Q2:是真的吗?


#

更新

目标

实际上,目标是创建一些 SessionsFactory 来管理用户的会话(这是 Web 服务用户)

会话@Statefull bean:

@Stateful
//Destroying outomaticly after 30 minuts inactive
@StatefulTimeout(180000)
@LocalBean
public class UserSession {
    //represent creation time, used by scheduler for destroying session
    private GregorianCalendar creationDate;
    private UUID sessionUUID;
    private String userId;
    private String state;

    //TODO change to xml
    private String histrory;

    public UserSession() {
    }
    @PostConstruct
    private void initSession(){
        //getting construction date
        creationDate = (GregorianCalendar) Calendar.getInstance();
        //generationg session id
        sessionUUID = UUID.randomUUID();
    }

    @PreDestroy
    private void releaseResource(){
        creationDate =null;
        sessionUUID = null;
        userId =null;
    }

    @Remove
    public void destroySession(){

    }

    public UUID getSessionUUID() {
        return sessionUUID;
    }

    public GregorianCalendar getCreationDate() {
        return creationDate;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getHistrory() {
        return histrory;
    }

    public void addHistroryEntry(String entry) {
        //TODO add history entry

    }
}

在工厂方法中,我只想创建 @Statefull UserSession 的新实例并管理为每个用户创建的会话数,并在一段时间(30 分钟)后调用 destroySession()

我需要跟踪用户会话请求的历史记录,并在以后保留历史记录..

所以我认为@Statefull bean 应该满足我的需求。但看起来通过 JNDI 名称查找是支持创建新 ejb 的唯一机会。我正在寻找在
没有查找的情况下注入新的 ejb 实例的可能性,并且可能有可能获取我的 @Statefull UserSession 当前创建的实例的集合,而不是将 thrm 保存在某个映射/集合中。

Q3:所以.. 只有 JNDI 会帮助我创建新的 ejb 实例?

Q4:是否可以从容器中获取一些 ejb 实例的集合?


我正在使用 glassfish v3,ejb 3.1。

4

1 回答 1

0

Q1: how could I call new instance of @Stateful session bean?

You must not inject a Stateful Session Bean into a stateless object such as Stateless Session Bean or Servlet that may be shared by multiple concurrent clients, you should use JNDI instead. Period.

To be honest, I'm not sure to understand what you are doing and I don't see important steps such as removal of your Stateful Session Beans. You are likely going to run out of memory or cause a lot of disk IO as the container that will try to passivate/activate instances to save memory.

Q2 : is it true?

You can call new but don't expect to get something else than a simple Java class i.e. don't expect to get a managed object i.e. don't expect to get an EJB. I don't think that this is what you want.


Sorry if this doesn't help much but as I said, I don't really understand what you're trying to achieve. You should maybe start to explain your goal first, I don't have the feeling that you are on the right path.

于 2010-05-09T19:42:56.083 回答