How it works...

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(); ...

Get Java EE 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.