简体   繁体   English

EJB容器中的ThreadLocal(和Singleton)

[英]ThreadLocal (and Singleton) in EJB Container

I've written an authorization system which relies on objects representing the current user. 我已经编写了一个授权系统,该系统依赖于代表当前用户的对象。 To simplify programming and increase performance I want to hold those objects in a ThreadLocal after the user has logged in. 为了简化编程并提高性能,我想在用户登录后将这些对象保存在ThreadLocal中。

It looks like this: 看起来像这样:

public class UserCache {

    private static final ThreadLocal<User> cache = new ThreadLocal<User>();

    public User getCurrentUser() {
        return cache.get();
    }

    public void setCurrentUser(User user) {
        cache.set(user);
    }

}

I've read that static elements make clustering problematic. 我读过静态元素使聚类成为问题。 If I had an UserCache on each cluster node, they all had their own cache object not synchronized with the cache objects on other nodes. 如果我在每个群集节点上都有一个UserCache,则它们都有自己的缓存对象,这些对象与其他节点上的缓存对象不同步。 Right? 对? UserCache is a classic candidate for a singleton, because the application needs only a single instance of it. UserCache是单例的经典候选人,因为应用程序仅需要一个实例。 But as far as I know @Singleton EJBs have the same behaviour in a cluster. 但是据我所知,@ Singleton EJB在群集中具有相同的行为。

So what to do to make UserCache clusterable in an EJB 3.1 (Java EE 6) environment? 那么如何使UserCache在EJB 3.1(Java EE 6)环境中可集群化呢?

Solutions extracted from the answers: 从答案中提取的解决方案:

  • Using SessionScope from CDI (JSR 299) or 使用来自CDI(JSR 299)的SessionScope或
  • Using JVM clustering with Terracotta 在Terracotta中使用JVM集群

Since you're already on Java EE 6, wouldn't it even be a lot easier to use CDI (Contexts and Dependency Injection). 既然您已经在使用Java EE 6,那么使用CDI(上下文和依赖注入)会不会容易得多。 This would allow to put the user info in the session scope , and inject it easily everywhere. 这将允许将用户信息放入会话范围 ,并轻松地将其注入到任何地方。 CDI manages the rest for you. CDI为您管理其余部分。

that shouldn't cause you a problem, because threads don't span different nodes anyway - or am i missing the point of your question? 那应该不会给您带来问题,因为线程无论如何都不会跨越不同的节点-还是我错过了您的问题的重点?

edit : you might want to look into something like terracotta - http://www.terracotta.org/ - for ways you can cluster existing objects 编辑:您可能想研究类似兵马俑的东西-http: //www.terracotta.org/-了解如何将现有对象聚类

If you need to share objects between nodes, I would also suggest looking at the existing frameworks (like Terracotta). 如果您需要在节点之间共享对象,我还建议您查看现有框架(例如Terracotta)。

Also, using ThreadLocal might possibly cause different problems: 另外,使用ThreadLocal可能会导致其他问题:

http://forums.sun.com/thread.jspa?threadID=589744 http://www.devwebsphere.com/devwebsphere/2005/06/dont_use_thread.html http://www.theserverside.com/discussions/thread.tss?thread_id=21055 http://forums.sun.com/thread.jspa?threadID=589744 http://www.devwebsphere.com/devwebsphere/2005/06/dont_use_thread.html http://www.theserverside.com/discussions/thread。 tss?thread_id = 21055

This may not be an issue here, though. 不过,这可能不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM