LOAD和CACHE应用程序范围的数据与@Singleton和@Stateless [英] LOAD and CACHE application-scoped data with @Singleton and @Stateless
问题描述
我正在寻找一个优雅的解决方案来解决在应用程序启动时加载和缓存静态,共享数据的旧问题(with无限生命)。
I'm looking for an elegant solutions to the old problem of loading and caching static, shared data at application startup (with an infinite lifetime).
我的旧方式是一个Spring Singleton Bean,但我现在正试图通过JAVA EE 6 (JPA2,EJB3.1 ,CDI)。
My old way was a Spring Singleton Bean, but I'm trying now to achieve it with JAVA EE 6 (JPA2, EJB3.1, CDI).
我有一个 @Entity
,一个 @Stateless
EJB从数据库加载实体。我的想法是添加一个
@Singleton
EJB来缓存数据;我还决定保留原来的EJB分开,以防止违反 SRP (并且因为在将来它
I have an @Entity
, and an @Stateless
EJB lo load the entity from database. My thought was to add a @Singleton
EJB to cache the data; I also decided to keep the original EJB separated, to prevent violating SRP (and because in the future it might be used bypassing the cache, by other actors).
请看看这个简化的 证明证明 :
Please take a look at this simplified Proof Of Concept:
实体
@NamedQuery(name="Room.findAll", query="SELECT r FROM Room r")
@Entity
public class Room {
@Id
private Integer id; // GETTER, SETTER
private String description; // GETTER, SETTER
}
装载机
@Stateless
public class Rooms {
@PersistenceContext
EntityManager em;
public List<Room> findAll() {
return em.createNamedQuery("Room.findAll",Room.class).getResultList();
}
}
Cacher
@Singleton
public class RoomsCached {
@EJB
Rooms rooms;
private List<Room> cachedRooms; // GETTER
@PostConstruct
public void initCache(){
this.cachedRooms = Collections.unmodifiableList(rooms.findAll());
}
}
你可以看到大的问题,概念错误或某些东西这个例子?
Can you see big problems, conceptual errors or something in this example ?
我的主要关注点是
-
我可以在cacher bean上添加
@DependsOn(Rooms)
,以确保客房在使用前已经加载,但是使用@Singleton
和@Stateless
我不能...将@Stateless
bean总是在CDI注入到@Singleton
之前加载?
If both were
@Singleton
(mehh), I could have added@DependsOn("Rooms")
on the cacher bean, to ensure Rooms is already loaded before being used, but with@Singleton
and@Stateless
I can't... will@Stateless
bean always be loaded before CDI injects it into@Singleton
?
@Singleton
调用 @Stateless
似乎很奇怪(我看过相反的例子) ;应该将 @Singleton
实例放在 @Stateless
EJB?
@Singleton
calling @Stateless
seems weird (I've seen examples of the opposite); should I change design by putting the @Singleton
instance inside the @Stateless
EJB ?
在 @PostConstruct
方法中加载和缓存是否正确?
Is it right to load and cache in the @PostConstruct
method ?
推荐答案
嗯,我做了一些测试,我也试过了 @Decorator
方式。这仍然是最好的一个。
Well, I've made some tests, and I've also tried the @Decorator
way. This still seems to be the best one.
@Entity bean和@Stateless bean与问题相同,而我更改了@Singleton bean,如下所示,还添加了经典的定时缓存: p>
@Entity bean and @Stateless bean are the same of the question, while I've changed the @Singleton bean as follows, also adding the classic timed cache:
@Singleton
public class RoomsCached {
@Inject
Rooms rooms;
private List<Room> cachedRooms;
private long timeout = 86400000L; // reload once a day
private long lastUpdate;
public List<Room> getCachedRooms() {
initCache();
return cachedRooms;
}
public void initCache() {
if (cachedRooms == null || expired()) {
this.cachedRooms = Collections.unmodifiableList(rooms.findAll());
}
}
private boolean expired() {
return System.currentTimeMillis() > lastUpdate + timeout;
}
}
不需要@PostConstruct,也不需要到@EJB,与底层的@注入@Stateless bean没有同步问题。
No need to @PostConstruct, nor to @EJB, no sync issues with the underlying, @inject-ed @Stateless bean.
这是非常好的。
这篇关于LOAD和CACHE应用程序范围的数据与@Singleton和@Stateless的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!