Let's first understand our bean declaration:
@Singleton@Startuppublic class UserCacheBean { ... @PostConstruct protected void init() { cache = new ConcurrentLinkedQueue<>(); loadCache(); }}
We are using a singleton because it has one and only one instance in the application context. And that's the way we want a data cache because we don't want to allow the possibility of different data being shared.
Also note that we used the @Startup annotation. It says to the server that this bean should be executed once it is loaded and the method annotated with @PostConstruct is used for it.
So we take the startup time to load our cache:
protected void loadCache() { List<User> list = em.createQuery("SELECT u FROM USER as u").getResultList(); ...