Step 1: Initializing the CacheManager

Now that we have our simple application, let's add Infinispan to it and initialize a CacheManager. Adding Infinispan is achieved by declaring the following dependency in the pom.xml file:

<dependency>
  <groupId>org.infinispan</groupId>
  <artifactId>infinispan-core</artifactId>
  <version>9.4.0.Final</version>
</dependency>

Now we can add a CacheManager. The CacheManager has many purposes:

  • it acts as a container for caches and controls their lifecycle
  • it manages global configuration and common data structures and resources (e.g. thread pools)
  • it manages clustering

A CacheManager is a fairly heavy-weight component, and you will probably want to initialize it early on in your application lifecycle.
A CacheManager is simply initialized by constructing it as follows:
EmbeddedCacheManager cacheManager = new DefaultCacheManager();
This will give us a default local (i.e. non-clustered) CacheManager. Because a CacheManager holds on to some resources which require proper disposal, you also need to make sure you stop it when you don't need it anymore. This is done by invoking the stop() method, as follows:
cacheManager.stop();
Once a CacheManager has been stopped it, and all resources obtained from it, cannot be used anymore.

If you attempt to compile and run the application at this step

$ git checkout step-1
$ mvn clean package exec:exec

you will see that nothing has changed. This is expected: we are not doing anything with the CacheManager yet. We will put it to good use in the next step.

back to top