【发布时间】:2014-05-05 10:29:06
【问题描述】:
我正在开发一个应用程序(java 中的企业应用程序),我需要的是单个实例,由多个线程同时共享,我使用了 @singleton。当每个用户登录时,通过调用 setTeleCallersDetails() 远程方法在电话呼叫者列表中设置一个值。但是在某些时候,当登录的用户数超过 15 时,@singleton 开始表现得像 @stateless bean,因为 setTeleCallersDetails() 开始在新的 tellcaller 数组列表中添加值。任何人都可以告诉实际如何解决这个问题 这是我的代码
@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);
}
}
【问题讨论】:
标签: java singleton ejb-3.1 stateless-session-bean