Deprecation

This document is DEPRECATED.

Please consider any information here as out of date. DO NOT use this document.

Instead, refer to http://infinispan.org/documentation.

Please update your bookmarks accordingly.

Skip to end of metadata
Go to start of metadata

Introduction

Infinispan includes integration with CDI in the infinispan-cdi module. Configuration and injection of the Inifispan's Cache API is provided, and it is planned to bridge Cache listeners to the CDI event system. The module also provide partial support of the JCache (JSR-107) caching annotations - for further details see Chapter 8 of the JCACHE specification.

Maven Dependencies

All you need is org.infinispan:infinispan-cdi

Which version of Infinispan should I use?
We recommend using the latest final version of the infinispan-cdi module. This module is available since Infinispan version 5.0.0.CR8.

Embedded cache integration

Inject an embedded cache

By default you can inject the default Infinispan cache. Let's look at the following example:

If you want to use a specific cache you just have to provide your own cache configuration and cache qualifier. For example, if you want to use a custom cache for the GreetingService you should write your own qualifier (here GreetingCache) and define its configuration:

The new configuration system is used since 5.1.0.CR2
As you probably know Infinispan 5.1.0 comes with a new way to configure your cache programmatically and a bit more (more information are available here). Now to configure a cache you must use this new configuration system.

To use this cache in the GreetingService add the @GeetingCache qualifier on your cache injection point. Simple!

Override the default embedded cache manager and configuration

Since 5.1.0.CR1 @OverrideDefault is deprecated
To migrate your code remove this annotation from your producers as detailed in the following section.

You can override the default cache configuration used by the default embedded cache manager. For that, you just have to create one Configuration producer with the @Default qualifier as illustrated in the following snippet:

It's also possible to override the default embedded cache manager used. The new default cache manager produced must have the @Default qualifier and the scope @ApplicationScoped.

Configure the transport for clustered use

To use Infinispan 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:

Remote cache integration

Inject a remote cache

With the CDI integration it's also possible to use a remote cache. For example you can inject the default RemoteCache as illustrated in the following snippet:

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.

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:

To use this cache in the GreetingService add the qualifier @RemoteGreetingCache qualifier on your cache injection.

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 remote cache manager can be overridden as illustrated in the following snippet:

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:

With the above code the GreetingCache or the RemoteGreetingCache will be associated with the produced cache manager.

Producer method scope
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.

Use a JBoss AS 7 configured cache

With JBoss AS 7, you can setup an Infinispan cache manager in the server configuration file. This allows you to externalize your Infinispan configuration and also to lookup the cache manager from JNDI, normally with the @Resource annotation.

As we mentioned earlier, you can override the default cache manager used by the Infinispan CDI extension. To use a JBoss AS 7 configured cache, you need to use the cache manager defined in JBoss AS 7. You only need to annotate the default cache manager producer with @Resource. The following example shows how use an embedded cache manager configured in JBoss AS 7.

Use JCache caching annotations

The infinispan-cdi module provides a partial support of JCache caching annotations. 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
Annotations target type
These annotations must only be used on methods.

To use these annotations the following interceptors must be declared in your application beans.xml.

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.

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)).

Can I use a different cache?
To use another cache specify its name with the cacheName attribute of the cache annotation. For example:
Labels:
infinispan infinispan Delete
cdi cdi Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Jul 01, 2013

    Hi Pete,

    The producer you gave for using the JBOSS AS7 configured cache does not work anymore. Not atleast with Jboss EAP 6.1. (Ofcourse it did not work with JBoss EAP 6.0.1 too).

    The @ApplicationScoped is no longer being accepted by the EAP deployment, when used in association with @Resource. It's suggesting to use @Dependent.

    The following producer worked:

           @SuppressWarnings("unused")
           @Produces
           @Dependent
           @Default
           @Resource(lookup="java:jboss/infinispan/infini")
           public EmbeddedCacheManager container;

    Regards

    Jithendra