15.2. Using the Infinispan CDI Module
The Infinispan CDI module can be used for the following purposes:
- To configure and inject Infinispan caches into CDI Beans and Java EE components.
- To configure cache managers.
- To control storage and retrieval using CDI annotations.
15.2.1. Configure and Inject Infinispan Caches Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
15.2.1.1. Inject an Infinispan Cache Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
An Infinispan cache is one of the multiple components that can be injected into the project's CDI beans.
The following code snippet illustrates how to inject a cache instance into the CDI bean:
public class MyCDIBean {
@Inject
Cache<String, String> cache;
}
public class MyCDIBean {
@Inject
Cache<String, String> cache;
}
15.2.1.2. Inject a Remote Infinispan Cache Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The code snippet to inject a normal cache is slightly modified to inject a remote Infinispan cache, as follows:
public class MyCDIBean {
@Inject
RemoteCache<String, String> remoteCache;
}
public class MyCDIBean {
@Inject
RemoteCache<String, String> remoteCache;
}
15.2.1.3. Set the Injection's Target Cache Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The following are the three steps to set an injection's target cache:
- Create a qualifier annotation.
- Add a producer class.
- Inject the desired class.
15.2.1.3.1. Create a Qualifier Annotation Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
To use CDI to return a specific cache, create custom cache qualifier annotations as follows:
Use the created
@SmallCache qualifier to specify how to create specific caches.
15.2.1.3.2. Add a Producer Class Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The following code snippet illustrates how the
@SmallCache qualifier (created in the previous step) specifies a way to create a cache:
The elements in the code snippet are:
@ConfigureCachespecifies the name of the cache.@SmallCacheis the cache qualifier.
15.2.1.3.3. Inject the Desired Class Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Use the
@SmallCache qualifier and the new producer class to inject a specific cache into the CDI bean as follows:
public class MyCDIBean {
@Inject @SmallCache
Cache<String, String> mySmallCache;
}
public class MyCDIBean {
@Inject @SmallCache
Cache<String, String> mySmallCache;
}