Ultra-Fast Microservices: The Enterprise Integration Makes It Easy
In this article, explore more about the integration between MicroStream, Jakarta EE, and MicroProfile, thanks to CDI.
Join the DZone community and get the full member experience.
Join For FreeWhen we talk about enterprise integration in the Java world, it brings a huge challenge, mainly in the news architecture trends such as microservices and the cloud age. We have several points to think about, such as a platform to integrate. This article will cover why and how to incorporate a persistence tool with the standard of Java enterprise applications. Then, we can explore and enjoy ultra-fast applications.
This rocket market is experiencing a vast revolution. Right now, the market is not exclusive to extensive and mature companies such as Red Hat, Google, etc. We can see even start-ups exploring this demand.
That is the case of MicroStream, a dynamic persistence company whose focus is to help the Java developer handle data easily and ultra-fast.
What or Who Is MicroStream?
Persistence in Java has always been complex, effortful, and expensive. During the runtime, Java database apps are often too slow. It happens mainly because the parser and the mapping process are needed.
The MicroStream vision was not to build yet another database system or ORM framework but to fundamentally change database development in Java to simplify and accelerate the entire database development process.
- About MicroStream
By default, MicroStream works in memory and persists locally in a disk; thus, it will avoid lost data in the process of the shutdown of the server. It also provides integration with other storage targets. Therefore, you can use it beyond file systems such as relational databases such as MySQL, NoSQL such as MongoDB, and blog stores such as Amazon S3.
MicroStream is an excellent solution that has proven to minimize latency, maximize throughput and workload, and save around 90% CPU power and costs.
Integration Based on Specifications
Java became popular with the word "WORA" or Write Once Run Anywhere. However, it is not true when we talk about developer experience; indeed, the vast flavors of platforms and tools we have right now in the Java world are enormous!
When we talk about MicroStream integration, all of those platforms look like a considerable challenge; primarily, each platform is provided by a different company. How do we achieve all of those?
The instant answer would be: let's create one API or implementation for each solution, but it is expensive and hard to maintain and update.
Thus, let's explore the hottest feature in the Java world that is outside the code: the specification.
The specification guarantees all the parts in the technology industry a path to walk through, so it brings benefits to developers, companies, and technology providers.
Developer: A low learning curve and cognitive load
The company: An easy way to avoid vendor-locking
The technology creator: An expected minimum requirement to follow
We can see it everywhere: SQL to databases, HTML and HTTP to web, and so on.
Be aware that popularity and standards are two different things: one tool might be popular and won't be a standard. A standard has fundamental components: Process, API, TCK, and an organization behind it to orchestrate and make everything work.
Jakarta EE: The WORA’s Answer to Integration
On Enterprise Java, we have the Jakarta EE, the successor of Java EE, with a vibrant community under the Eclipse Foundation umbrella.
It has several components to make the Java developer's life even better.
The CDI is one of the most critical specifications on Jakarta EE, which works as glue. Thanks to CDI, we can inject resources to JAR-RS, JMS, and JPA with the @Inject
annotation, which will work like a charm.
Jakarta EE also inspires the MicroProfile initiative, where several organizations, companies, and communities create an optimized profile to work with both microservices and cloud-age.
We decided to have an API based on Jakarta EE using CDI specifications. With a single library, we can now have support for several twenty-one Jakarta vendors and around ten Microprofile vendors.
Besides the CDI, MicroStream uses MicroProfile configuration. This API helps optimize the environment's settings by applying the Twelve factor App.
Thus, one implementation to over-thirty vendors.
MicroStream + CDI Integration Hands-On
We discussed why we decided to use CDI instead of code directly on each one of the third vendors. In the next step, let's examine the code and the API.
The first step in the integration is to add the integration library. Please look at the latest version and add it to your application.
<dependency>
<groupId>one.microstream</groupId>
<artifactId>microstream-integrations-cdi</artifactId>
<version>LAST_VERSION_HERE</version>
</dependency>
MicroStream operates on any Object graph, so you're free to create the ideal structure for your application, and the engine will take care of the rest. To focus more on the API, we'll create a simple store that will handle entity Names
where we'll have a Set
of Strings
.
To highlight, we can explore any Java structure. As a recommendation, explore immutable as much as possible on this design.
@Storage
public class Names implements Supplier<Set<String>> {
private final Set<String> names = new HashSet<>();
public void add(final String name) {
this.names.add(Objects.requireNonNull(name, "name is required"));
}
//...
}
To define the root of the object graph, we have the Storage
annotation. The integration will do the work. If you enjoy DDD as I do, think of the Storage modeling as an aggregate root.
But how do we work with this root class? That's easy: use it, and explore the power of CDI with the @Inject
annotation.
@ApplicationScoped
public class NamesService {
@Inject
private Names names;
@Store
public void add(final String name) {
this.names.add(name);
}
public Set<String> getNames() {
return this.names.get();
}
}
There is the Store
annotation to commit the operation on the MicroStream engine. Anything else is Java. So, you can explore any data structure smoothly and inject it simply as that.
public class App {
public static void main(final String[] args) {
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
final StorageManager manager = container.select(StorageManager.class).get();
final Object root = manager.root();
System.out.println("The root value: " + root);
final NamesService service = container.select(NamesService.class).get();
System.out.println("The names: " + service.getNames());
service.add("Sebastian");
service.add("Otavio");
service.add("Ada");
service.add("Mari");
}
System.exit(0);
}
}
MicroStream also supports cache with JCache, where you can inject this dependency using CDI and this cache can be persistent.
@Inject
@StorageCache
private Cache<String, Integer> counter;
As we said, if you want to know more or even explore more the power of Jakarta EE combined with MicroStream, we do have several tutorials for you:
The CDI and MicroStream are a good combination when discussing enterprise solutions. In the cloud age, you can explore this solution on Java with several architectural styles such as monolith and microservices. We can see a significant and vibrant future ahead.
Opinions expressed by DZone contributors are their own.
Comments