How to do it...

  1. Let's create a User class to be our cached object:
public class User {        private String name;    private String email;      //DO NOT FORGET TO IMPLEMENT THE GETTERS AND SETTERS}
  1. And then create a singleton to hold our user list cache:
@Singleton@Startuppublic class UserCacheBean {    protected Queue<User> cache = null;        @PersistenceContext    private EntityManager em;    public UserCacheBean() {    }    protected void loadCache() {        List<User> list = em.createQuery("SELECT u FROM USER                                          as u").getResultList();        list.forEach((user) -> {            cache.add(user);        });    }    @Lock(LockType.READ)    public List<User> get() {        return cache.stream().collect(Collectors.toList());    }    @PostConstruct    protected void init() {        cache = new ConcurrentLinkedQueue<>(); loadCache(); ...

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.