通知所有用户的所有@ SessionScoped-Beans-Instances [英] Notify all @SessionScoped-Beans-Instances from all Users

查看:116
本文介绍了通知所有用户的所有@ SessionScoped-Beans-Instances的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个@SessionScoped sessionInformationBean,它保存了一个已登录用户的Person -Entity.因此,如果用户登录,我将查找相应的Entity并放入@SessionScoped CDI Bean中.该Bean用于检索代码中任意位置的当前用户(Person -Entity),以便您可以检查它是否是Admin或类似的东西.

I have a @SessionScoped sessionInformationBean, which holds a Person-Entity from a logged in user. So, if a User logs in, I am looking up the corresponding Entity and put in in the @SessionScoped CDI Bean. This Bean is used to retrieve the current user (a Person-Entity) at any position in code, so that you can check, if it is a Admin or things like that.

@Inject
private PersonFacade personFacade;

private Person currentUser;

public Person getCurrentUser() {
    if (currentUser == null) {
        String loginname = FacesContext.
        getCurrentInstance().
        getExternalContext().getRemoteUser();  
        currentUser = personFacade.findByLoginname(loginname);
    }

    return currentUser;
}

但要设置这种情况,管理员正在为该登录用户(Person -Entity)赋予一些管理员权限,并将其保存到数据库中.在这种情况下,@SessionScoped Bean上的Person不会更新,因此,已经登录的用户在刷新页面后看不到其管理员权限.那就是问题所在.为避免此问题,每次对@SessionScoped bean的访问(没有激活缓存)时,我都从数据库中获取新用户.

But set the case, an Admin is giving this logged in user ( the Person-Entity) some Admin-Rights and saves him to the database. In this case, the Person at the @SessionScoped Bean is not updated, therefore the already logged in user is not seeing his Admin-Rights after a refresh of his page. Thats the problem. To avoid this problem I am fetching the user new from the database every access (There is no cache activated) to the @SessionScoped bean.

但是我想缓存他,并避免每次访问数据库.因此,我认为,如果有人保存用户,我将仅注意到所有sessionInformationBean实例并将currentUser -Attribute设置为null.因此,在下一次调用时,他们再次从数据库中获取它,并将其缓存到我的Person.save() -Operation中再次将其设置为null.

But I want to cache him and avoid a database access every time. So, I thought, if anyone saves a user, I will simply notice all sessionInformationBean-Instances and set the currentUser-Attribute to null. So, the next call, they fetch it again from database and cache it till its set to null again from my Person.save()-Operation.

但这似乎有些棘手.我以为我可以使用CDI-Events来处理它,但是它们只会被推送到用户的sessionInformationBean,即正在编辑另一个用户.

But that seems to be a little bit tricky. I thought I can handle it with CDI-Events, but they only will be pushed to the sessionInformationBean of the user, that is editing the other user.

也许与我的问题有关:

Maybe something to do with my problem: CDI Events observed across sessions

然后我想..好的..让我们用Primefaces-Push来做.但是,同样的事情..这些事件只是我自己的sessionInformationBean.

Then I thought.. okay.. lets do it with Primefaces-Push. But the same thing.. the Events are just coming to my own sessionInformationBean.

EventBus eventBus = EventBusFactory.getDefault().eventBus();
eventBus.publish("/session", "test");

我认为push和WebSockets的目的是通知所有用户或会话.

I thought the purpose of push and WebSockets is to notify all users or sessions.

因此,问题是:如何访问特定@SessionScoped Bean的所有实例?我只想从每个登录用户访问sessionInformationBean并将currentUser设置为null.

So, the question is: How to access all instances of a specific @SessionScopedBean? I just want to access the sessionInformationBean from every logged in user and set the currentUserto null.

推荐答案

我没有想到的内置方法可以做到这一点.我建议添加一个ApplicationScoped bean.每当创建您的SessionScoped bean时,请在此应用程序作用域bean中注册它.当您要处理此事件时,将遍历所有这些对象.

There's no built in way I can think of to do this. What I would recommend is to add an ApplicationScoped bean. Whenever your SessionScoped bean is created, register it with this app scoped bean. When you want to process this event iterate through all of these objects.

我很好奇,当您有多台服务器时会发生什么?

I'm curious though, what happens when you have multiple servers?

这篇关于通知所有用户的所有@ SessionScoped-Beans-Instances的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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