@singleton表现得像@stateless bean

时间:2014-05-05 10:29:07

标签: java singleton ejb-3.1 stateless-session-bean

我正在处理一个应用程序(java中的enterprize应用程序),其中我需要的是单个实例,由多个线程共享,我已经使用了@singleton。当每个用户登录一个值时,通过调用setTeleCallersDetails()远程方法在telecallers List中设置。但是当某个用户登录的数量超过15时,@ someton开始表现得像@stateless bean,因为setTeleCallersDetails()开始在新的tellcaller arraylist中添加值。任何人都可以告诉实际如何解决这个问题 这是我的代码

@Singleton
@Startup
public class UserSessionBean implements UserRemote {
    volatile List<UserDetails> user;
    volatile List<UserDetails> telecallers;
    volatile List notloggedlt;
    /**
     * Default constructor. 
     * @return 
     */
    @PostConstruct
    private void startup() {
        // TODO Auto-generated constructor stub

        if(user == null){
            user = new ArrayList<UserDetails>();
        }
        if(telecallers == null){
            telecallers = new ArrayList<UserDetails>();
        }
        if(notloggedlt == null){
            notloggedlt =  new ArrayList();
        }
    }


    public List<UserDetails> getUserDetails(){
        return user;
    }

    public List<UserDetails> getTeleCallersDetails(){
        return telecallers;
    }

    public List getNotKLoggedInUsersDetails(){
        return notloggedlt;
    }

    @Lock(LockType.WRITE)
    public void setUserDetails(UserDetails objUser){
        if(!user.isEmpty()){
            Collections.sort(user);
            int location = Collections.binarySearch(user, new UserDetails(objUser.getUserId()));
                if (location >= 0) {
                  user.remove(location);
                }
            }
         user.add(objUser);
    }


    @Lock(LockType.WRITE)
    public void removeUserDetails(String userId){

            Collections.sort(user);
            int location = Collections.binarySearch(user, new UserDetails(userId));
                if (location >= 0) {
                  user.remove(location);
                }

    }

    @Lock(LockType.WRITE)
    public void removeTeleCallersDetails(String userId){
        Collections.sort(telecallers);
        int location = Collections.binarySearch(telecallers, new UserDetails(userId));
            if (location >= 0) {
            telecallers.remove(location);
            }

    }

    @Lock(LockType.WRITE)
    public void setNotKLoggedInUsersDetails(List notloggedusers){
        this.notloggedlt = notloggedusers;
    }

    @Lock(LockType.WRITE)
    public void setNotKLoggedInUserDetail(UserDetails objUser){
        notloggedlt.add(objUser);
    }

    @Lock(LockType.WRITE)
    public void setTeleCallersDetails(UserDetails objUser){
        if(!telecallers.isEmpty()){
        Collections.sort(telecallers);
        int location = Collections.binarySearch(telecallers, new UserDetails(objUser.getUserId()));
            if (location >= 0) {
              telecallers.remove(location);
            }
        }
        telecallers.add(objUser);

    }
}   

1 个答案:

答案 0 :(得分:0)

垃圾收集: 如果单例类被垃圾收集,那么在需要时再次通过创建新实例来重新加载。它发生在没有引用此类和&amp;它的实例;所有字段都默认/重新初始化&amp;以前的状态丢失了。

类加载器:对于多个类加载器,可以存在多个副本和每个人都可以拥有自己的单例实例。


可能在你的情况下,线程没有对单例类进行任何引用,因为一旦它们完成,引用就会被破坏&amp;单身可能会收集垃圾

由于单例bean默认WRITE锁定容器管理并发的方法,因此您无需为方法&amp;显式指定LockTypevolatile表示字段,因为一次只能访问一个客户端。

您可以尝试将记录器添加到默认构造函数,startup&amp;破坏方法来获得想法,真正发生在底层的事情。

相关问题