@singleton的行为类似@stateless bean [英] @singleton behaving like @stateless bean

查看:127
本文介绍了@singleton的行为类似@stateless bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序(java中的enterprize应用程序),在该应用程序中,我需要一个实例同时由多个线程同时使用@singleton共享.当每个用户登录时,通过调用setTeleCallersDetails()远程方法在电话呼叫者列表中设置一个值.但是在某些时候,当登录的用户数超过15时,@ singleton会像@stateless bean一样开始工作,因为setTeleCallersDetails()开始在新的柜员数组列表中添加值.任何人都可以告诉实际如何解决这个问题 这是我的代码

i am working on an application(enterprize application in java) in which i need is single instance to be shared by multiple thread concurrently for which i have used @singleton . when each user login a value is set in telecallers List by invoking setTeleCallersDetails() remote method. but at certain point when number of user logged in exceed 15 then @singleton starts behaving like @stateless bean as setTeleCallersDetails() start adding value in new tellcaller arraylist. can anybody tell to actually how to solve this problem this my code

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

    }
}   

推荐答案

垃圾回收: 如果单例类获取了垃圾回收,则需要时通过创建新实例再次将其重新加载.当没有对该类&的引用时,就会发生这种情况.它的实例;所有字段均默认/重新初始化&以前的状态会丢失.

Garbage Collection: If a singleton class gets garbage collected, then again it's reloaded when needed, by creating a new instance. It happens when there are no references to this class & its instance; all the fields gets defaulted/re-initialized & the previous state is lost.

类加载器:如果有多个类加载器,则可以存在多个副本&每个人都可以拥有自己的单例实例.

Class Loaders: In case of multiple class loaders, there can exist multiple copies & each one can have their own singleton instance.

在您的情况下,线程可能没有任何对单例类的引用,因为一旦完成,引用就会被破坏&单例可能会收集垃圾.

Probably in your case, threads aren't holding any reference to the singleton class, because once they finish, reference is destroyed & the singleton might get garbage collected.

由于默认情况下,单例bean在容器管理的并发方法上具有WRITE锁,因此您无需为方法&显式指定LockType. volatile用于字段,因为一次只能有一个客户端可以访问.

As singleton bean have by default WRITE lock on methods for container managed concurrency, you don't need to explicitly specify LockType for methods & volatile for fields, because only one client can access at a time.

您可以尝试将记录器添加到默认构造函数"startup&"中.破坏方法以获取想法,这实际上是在发生什么.

You can try adding loggers to the default constructor, startup & destroy methods to get the idea, what is really happening underneath.

这篇关于@singleton的行为类似@stateless bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆