Chapter 11. Using the 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.
11.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>
11.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
@GreetingCachequalifier.
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
@ApplicationScopedannotation to avoid creating multiple Cache Managers. - 3
- Creates a new
DefaultCacheManagerinstance that is bound to the@GreetingCachequalifier.
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
@GreetingCachequalifier to your cache injection point.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.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
@RemoteGreetingCachequalifier 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
@ApplicationScopedannotation to avoid creating multiple Cache Managers, which are heavy weight objects. - 2
- creates a new
RemoteCacheManagerinstance that is bound to the@RemoteGreetingCachequalifier.
11.4. 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
@Observesannotation as in the following example: