Chapter 12. Using the Data Grid CDI Extension
Data Grid provides an extension that integrates with the CDI (Contexts and Dependency Injection) programming model and allows you to:
- Configure and inject caches into CDI Beans and Java EE components.
- Configure cache managers.
- Receive cache and cache manager level events.
- Control data storage and retrieval using JCache annotations.
12.1. CDI Dependencies Copy linkLink copied to clipboard!
Update your pom.xml
with one of the following dependencies to include the Data Grid CDI extension in your project:
Embedded (Library) Mode
<dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-cdi-embedded</artifactId> </dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-cdi-embedded</artifactId>
</dependency>
Server Mode
<dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-cdi-remote</artifactId> </dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-cdi-remote</artifactId>
</dependency>
12.2. Injecting Embedded Caches Copy linkLink copied to clipboard!
Set up CDI beans to inject embedded caches.
Procedure
Create a cache qualifier annotation.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- creates a
@GreetingCache
qualifier.
Add a producer method that defines the cache configuration.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add a producer method that creates a clustered cache manager, if required
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- adds the cache qualifier.
- 2
- creates the bean once for the application. Producers that create cache managers should always include the
@ApplicationScoped
annotation to avoid creating multiple cache managers. - 3
- creates a new
DefaultCacheManager
instance that is bound to the@GreetingCache
qualifier.
NoteCache managers are heavy weight objects. Having more than one cache manager running in your application can degrade performance. When injecting multiple caches, either add the qualifier of each cache to the cache manager producer method or do not add any qualifier.
Add the
@GreetingCache
qualifier to your cache injection point.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
12.3. Injecting Remote Caches Copy linkLink copied to clipboard!
Set up CDI beans to inject remote caches.
Procedure
Create a cache qualifier annotation.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the
@RemoteGreetingCache
qualifier to your cache injection point.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Tips for injecting remote caches
You can inject remote caches without using qualifiers.
... @Inject @Remote("greetingCache") private RemoteCache<String, String> cache;
... @Inject @Remote("greetingCache") private RemoteCache<String, String> cache;
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you have more than one Data Grid cluster, you can create separate remote cache manager producers for each cluster.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- creates the bean once for the application. Producers that create cache managers should always include the
@ApplicationScoped
annotation to avoid creating multiple cache managers, which are heavy weight objects. - 2
- creates a new
RemoteCacheManager
instance that is bound to the@RemoteGreetingCache
qualifier.
12.4. JCache Caching Annotations Copy linkLink copied to clipboard!
You can use the following JCache caching annotations with CDI managed beans when JCache artifacts are on the classpath:
@CacheResult
- caches the results of method calls.
@CachePut
- caches method parameters.
@CacheRemoveEntry
- removes entries from a cache.
@CacheRemoveAll
- removes all entries from a cache.
Target type: You can use these JCache caching annotations on methods only.
To use JCache caching annotations, declare interceptors in the beans.xml
file for your application.
Managed Environments (Application Server)
Non-managed Environments (Standalone)
JCache Caching Annotation Examples
The following example shows how the @CacheResult
annotation caches the results of the GreetingService.greet()
method:
With JCache annotations, the default cache uses the fully qualified name of the annotated method with its parameter types, for example:org.infinispan.example.GreetingService.greet(java.lang.String)
To use caches other than the default, use the cacheName
attribute to specify the cache name as in the following example:
@CacheResult(cacheName = "greeting-cache")
@CacheResult(cacheName = "greeting-cache")
12.5. Receiving Cache and Cache Manager Events Copy linkLink copied to clipboard!
You can use CDI Events to receive Cache and cache manager level events.
-
Use the
@Observes
annotation as in the following example: