Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Jakarta EE Cookbook
  • Table Of Contents Toc
Jakarta EE Cookbook

Jakarta EE Cookbook - Second Edition

By : Moraes
5 (2)
close
close
Jakarta EE Cookbook

Jakarta EE Cookbook

5 (2)
By: Moraes

Overview of this book

Jakarta EE is widely used around the world for developing enterprise applications for a variety of domains. With this book, Java professionals will be able to enhance their skills to deliver powerful enterprise solutions using practical recipes. This second edition of the Jakarta EE Cookbook takes you through the improvements introduced in its latest version and helps you get hands-on with its significant APIs and features used for server-side development. You'll use Jakarta EE for creating RESTful web services and web applications with the JAX-RS, JSON-P, and JSON-B APIs and learn how you can improve the security of your enterprise solutions. Not only will you learn how to use the most important servers on the market, but you'll also learn to make the best of what they have to offer for your project. From an architectural point of view, this Jakarta book covers microservices, cloud computing, and containers. It allows you to explore all the tools for building reactive applications using Jakarta EE and core Java features such as lambdas. Finally, you'll discover how professionals can improve their projects by engaging with and contributing to the community. By the end of this book, you'll have become proficient in developing and deploying enterprise applications using Jakarta EE.
Table of Contents (14 chapters)
close
close

Using EJB and JPA for data caching

Knowing how to build a simple and local cache for your application is an important skill. It may have a big impact on some data access performance and is quite easy to do.

This recipe will show you how to do this.

Getting ready

Simply add a Jakarta EE dependency to your project:

<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>

How to do it...

You need to perform the following steps to complete this recipe:

  1. Let's create a User class. This will be our cached object:
public class User {

private String name;
private String email;

//DO NOT FORGET TO IMPLEMENT THE GETTERS AND SETTERS
}
  1. Next, create a singleton that will hold our user list cache:
@Singleton
@Startup
public 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();
}
}

Now, let's see how this recipe works.

How it works...

First, let's understand our bean declaration:

@Singleton
@Startup
public 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. This is the way we want a data cache to be we don't want different data being shared.

Also, note that we used the @Startup annotation. This tells the server that this bean should be executed once it is loaded and that the method annotated with @PostConstruct is used for it.

So, we use the startup time to load our cache:

protected void loadCache() {
List<User> list = em.createQuery("SELECT u FROM USER
as u").getResultList();

list.forEach((user) -> {
cache.add(user);
});
}

Now, let's check the object holding our cache:

protected Queue<User> cache = null;

...

cache = new ConcurrentLinkedQueue<>();

ConcurrentLinkedQueue is a list that's built with one main purpose to be accessed by multiple processes in a thread-safe environment. That's exactly what we need. This also offers great performance when we need to access its members.

Finally, let's check the access to our data cache:

Lock(LockType.READ)
public List<User> get() {
return cache.stream().collect(Collectors.toList());
}

We annotated the get() method with LockType.READ, which is telling the concurrency manager that it can be accessed by multiple processes at once in a thread-safe way.

There's more...

If you need big and complex caches in your application, you should use some enterprise cache solutions for better results.

See also

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Jakarta EE Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon