Chapter 15. CDI Support
Red Hat Data Grid includes integration with Contexts and Dependency Injection (better known as CDI) via Red Hat Data Grid’s infinispan-cdi-embedded
or infinispan-cdi-remote
module. CDI is part of Java EE specification and aims for managing beans' lifecycle inside the container. The integration allows to inject Cache interface and bridge Cache and CacheManager events. JCache annotations (JSR-107) are supported by infinispan-jcache
and infinispan-jcache-remote
artifacts. For more information have a look at Chapter 11 of the JCACHE specification.
15.1. Maven Dependencies
To include CDI support for Red Hat Data Grid in your project, use one of the following dependencies:
pom.xml for Embedded mode
<dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-cdi-embedded</artifactId> <version>${version.infinispan}</version> </dependency>
Replace ${version.infinispan}
with the appropriate version of Red Hat Data Grid.
pom.xml for Remote mode
<dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-cdi-remote</artifactId> <version>${version.infinispan}</version> </dependency>
Replace ${version.infinispan}
with the appropriate version of Red Hat Data Grid.
We recommend using the latest final version Red Hat Data Grid.
15.2. Embedded cache integration
15.2.1. Inject an embedded cache
By default you can inject the default Red Hat Data Grid cache. Let’s look at the following example:
Default cache injection
... import javax.inject.Inject; public class GreetingService { @Inject private Cache<String, String> cache; public String greet(String user) { String cachedValue = cache.get(user); if (cachedValue == null) { cachedValue = "Hello " + user; cache.put(user, cachedValue); } return cachedValue; } }
If you want to use a specific cache rather than the default one, you just have to provide your own cache configuration and cache qualifier. See example below:
Qualifier example
... import javax.inject.Qualifier; @Qualifier @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface GreetingCache { }
Injecting Cache with qualifier
... import org.infinispan.configuration.cache.Configuration; import org.infinispan.configuration.cache.ConfigurationBuilder; import org.infinispan.cdi.ConfigureCache; import javax.enterprise.inject.Produces; public class Config { @ConfigureCache("greeting-cache") // This is the cache name. @GreetingCache // This is the cache qualifier. @Produces public Configuration greetingCacheConfiguration() { return new ConfigurationBuilder() .memory() .size(1000) .build(); } // The same example without providing a custom configuration. // In this case the default cache configuration will be used. @ConfigureCache("greeting-cache") @GreetingCache @Produces public Configuration greetingCacheConfiguration; }
To use this cache in the GreetingService add the @GeetingCache
qualifier on your cache injection point.
15.2.2. Override the default embedded cache manager and configuration
You can override the default cache configuration used by the default EmbeddedCacheManager
. For that, you just have to create a Configuration
producer with default qualifiers as illustrated in the following snippet:
Overriding Configuration
public class Config { // By default CDI adds the @Default qualifier if no other qualifier is provided. @Produces public Configuration defaultEmbeddedCacheConfiguration() { return new ConfigurationBuilder() .memory() .size(100) .build(); } }
It’s also possible to override the default EmbeddedCacheManager
. The newly created manager must have default qualifiers and Application scope.
Overriding EmbeddedCacheManager
... import javax.enterprise.context.ApplicationScoped; public class Config { @Produces @ApplicationScoped public EmbeddedCacheManager defaultEmbeddedCacheManager() { return new DefaultCacheManager(new ConfigurationBuilder() .memory() .size(100) .build()); } }
15.2.3. Configure the transport for clustered use
To use Red Hat Data Grid in a clustered mode you have to configure the transport with the GlobalConfiguration
. To achieve that override the default cache manager as explained in the previous section. Look at the following snippet:
Overriding default EmbeddedCacheManager
... package org.infinispan.configuration.global.GlobalConfigurationBuilder; @Produces @ApplicationScoped public EmbeddedCacheManager defaultClusteredCacheManager() { return new DefaultCacheManager( new GlobalConfigurationBuilder().transport().defaultTransport().build(), new ConfigurationBuilder().memory().size(7).build() ); }
15.3. Remote cache integration
15.3.1. Inject a remote cache
With the CDI integration it’s also possible to use a RemoteCache
as illustrated in the following snippet:
Injecting RemoteCache
public class GreetingService { @Inject private RemoteCache<String, String> cache; public String greet(String user) { String cachedValue = cache.get(user); if (cachedValue == null) { cachedValue = "Hello " + user; cache.put(user, cachedValue); } return cachedValue; } }
If you want to use another cache, for example the greeting-cache, add the @Remote
qualifier on the cache injection point which contains the cache name.
Injecting RemoteCache with qualifier
public class GreetingService { @Inject @Remote("greeting-cache") private RemoteCache<String, String> cache; ... }
Adding the @Remote
cache qualifier on each injection point might be error prone. That’s why the remote cache integration provides another way to achieve the same goal. For that you have to create your own qualifier annotated with @Remote
:
RemoteCache qualifier
@Remote("greeting-cache") @Qualifier @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface RemoteGreetingCache { }
To use this cache in the GreetingService add the qualifier @RemoteGreetingCache
qualifier on your cache injection.
15.3.2. Override the default remote cache manager
Like the embedded cache integration, the remote cache integration comes with a default remote cache manager producer. This default RemoteCacheManager
can be overridden as illustrated in the following snippet:
Overriding default RemoteCacheManager
public class Config { @Produces @ApplicationScoped public RemoteCacheManager defaultRemoteCacheManager() { return new RemoteCacheManager(localhost, 1544); } }
15.4. Use a custom remote/embedded cache manager for one or more cache
It’s possible to use a custom cache manager for one or more cache. You just need to annotate the cache manager producer with the cache qualifiers. Look at the following example:
public class Config { @GreetingCache @Produces @ApplicationScoped public EmbeddedCacheManager specificEmbeddedCacheManager() { return new DefaultCacheManager(new ConfigurationBuilder() .expiration() .lifespan(60000l) .build()); } @RemoteGreetingCache @Produces @ApplicationScoped public RemoteCacheManager specificRemoteCacheManager() { return new RemoteCacheManager("localhost", 1544); } }
With the above code the GreetingCache or the RemoteGreetingCache will be associated with the produced cache manager.
To work properly the producers must have the scope @ApplicationScoped . Otherwise each injection of cache will be associated to a new instance of cache manager.
15.5. Use JCache caching annotations
There is now a separate module for JSR 107 (JCACHE) integration, including API. See this chapter for details.
When CDI integration and JCache artifacts are present on the classpath, it is possible to use JCache annotations with CDI managed beans. These annotations provide a simple way to handle common use cases. The following caching annotations are defined in this specification:
-
@CacheResult
- caches the result of a method call -
@CachePut
- caches a method parameter -
@CacheRemoveEntry
- removes an entry from a cache -
@CacheRemoveAll
- removes all entries from a cache
These annotations must only be used on methods.
To use these annotations, proper interceptors need to be declared in beans.xml
file:
Interceptors for managed environments such as Application Servers
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" version="1.2" bean-discovery-mode="annotated"> <interceptors> <class>org.infinispan.jcache.annotation.InjectedCacheResultInterceptor</class> <class>org.infinispan.jcache.annotation.InjectedCachePutInterceptor</class> <class>org.infinispan.jcache.annotation.InjectedCacheRemoveEntryInterceptor</class> <class>org.infinispan.jcache.annotation.InjectedCacheRemoveAllInterceptor</class> </interceptors> </beans>
Interceptors for unmanaged environments such as standalone applications
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" version="1.2" bean-discovery-mode="annotated"> <interceptors> <class>org.infinispan.jcache.annotation.CacheResultInterceptor</class> <class>org.infinispan.jcache.annotation.CachePutInterceptor</class> <class>org.infinispan.jcache.annotation.CacheRemoveEntryInterceptor</class> <class>org.infinispan.jcache.annotation.CacheRemoveAllInterceptor</class> </interceptors> </beans>
The following snippet of code illustrates the use of @CacheResult
annotation. As you can see it simplifies the caching of the Greetingservice#greet
method results.
Using JCache annotations
import javax.cache.interceptor.CacheResult; public class GreetingService { @CacheResult public String greet(String user) { return "Hello" + user; } }
The first version of the GreetingService
and the above version have exactly the same behavior. The only difference is the cache used. By default it’s the fully qualified name of the annotated method with its parameter types (e.g. org.infinispan.example.GreetingService.greet(java.lang.String)
).
Using other cache than default is rather simple. All you need to do is to specify its name with the cacheName
attribute of the cache annotation. For example:
Specifying cache name for JCache
@CacheResult(cacheName = "greeting-cache")
15.6. Use Cache events and CDI
It is possible to receive Cache and Cache Manager level events using CDI Events. You can achieve it using @Observes
annotation as shown in the following snippet:
Event listeners based on CDI
import javax.enterprise.event.Observes; import org.infinispan.notifications.cachemanagerlistener.event.CacheStartedEvent; import org.infinispan.notifications.cachelistener.event.*; public class GreetingService { // Cache level events private void entryRemovedFromCache(@Observes CacheEntryCreatedEvent event) { ... } // Cache Manager level events private void cacheStarted(@Observes CacheStartedEvent event) { ... } }
Check Listeners and Notifications for more information about event types.