053. <dependency> 054. <groupId>org.infinispan</groupId> 055. <artifactId>infinispan-embedded</artifactId> 056. <version>${infinispan.version}</version> 057. </dependency>
Running Infinispan in embedded mode is very easy. First, we'll set up a project, and then we'll run Infinispan, and start adding data.
All the code discussed in this tutorial is available in the embedded-cache quickstart.
The only thing you need to set up Infinispan is add it's dependencies to your project. If you are using Maven (or another build system like Gradle or Ivy which can use Maven dependencies), then this is easy. Just add:
053. <dependency> 054. <groupId>org.infinispan</groupId> 055. <artifactId>infinispan-embedded</artifactId> 056. <version>${infinispan.version}</version> 057. </dependency>
to your <dependencies> section of the POM. You'll need to substitute ${infinispan.version} for the version of Infinispan you wish to use.
We recommend using the latest final version of Infinispan. All releases are displayed on the downloads page.
You'll also need to enable the JBoss Maven repository. We recommend adding a profile:
076. <executions> 077. <execution> 078. <goals> 079. <goal>java</goal> 080. </goals> 081. </execution> 082. </executions> 083. </plugin> 084. </plugins> 085. </build> 086. 087. <profiles> 088. <profile> 089. <id>jboss-public-repository</id> 090. <activation> 091. <property> 092. <name>jboss-public-repository</name> 093. <value>!false</value> 094. </property> 095. </activation> 096. <repositories> 097. <repository> 098. <id>jboss-public-repository-group</id> 099. <name>JBoss Public Maven Repository Group</name> 100. <url>http://repository.jboss.org/nexus/content/groups/public</url> 101. <releases> 102. <enabled>true</enabled> 103. <updatePolicy>never</updatePolicy> 104. </releases> 105. <snapshots> 106. <enabled>true</enabled> 107. <updatePolicy>never</updatePolicy> 108. </snapshots> 109. </repository> 110. </repositories> 111. <pluginRepositories> 112. <pluginRepository> 113. <id>jboss-public-repository-group</id> 114. <name>JBoss Public Maven Repository Group</name>
Alternatively, you can use the POM from the quickstart that accompanies this tutorial.
If you are using Ant, or another build system which doesn't provide declarative dependency management, then the Infinispan distribution zip contains a lib/ directory. Add the contents of this to the build classpath.
In order to run Infinispan, we're going to create a main method in the Quickstart class. Infinispan comes configured to run out of the box; once you have set up your dependencies, all you need to do to start using Infinispan is to create a new cache manager and get a handle on the default cache.
28. public class Quickstart { 29. 30. public static void main(String args[]) throws Exception { 31. Cache<Object, Object> c = new DefaultCacheManager().getCache(); 32. }
We now need a way to run the main method! If you are using Maven, the best approach is to copy all the project dependencies to a directory, and at the same time compile the java classes from our project:
$> mvn clean compile dependency:copy-dependencies -DstripVersion
Having done that, we can run the main method:
$> java -cp target/classes/:target/dependency/* Quickstart
You should see Infinispan start up, and the version in use logged to the console.
Congratulations, you now have Infinispan running as a local cache!
Infinispan exposes a Map-like, JSR-107-esque interface for accessing and mutating the data stored in the cache. For example:
37. 38. // Add a entry 39. cache.put("key", "value"); 40. // Validate the entry is now in the cache 41. assertEqual(1, cache.size()); 42. assertTrue(cache.containsKey("key")); 43. // Remove the entry from the cache 44. Object v = cache.remove("key"); 45. // Validate the entry is no longer in the cache 46. assertEqual("value", v);
Infinispan offers a thread-safe data-structure:
48. 49. // Add an entry with the key "key" 50. cache.put("key", "value"); 51. // And replace it if missing 52. cache.putIfAbsent("key", "newValue"); 53. // Validate that the new value was not added
By default entries are immortal but you can override this on a per-key basis and provide lifespans.
58. 59. //By default entries are immortal but we can override this on a per-key basis and provide lifespans. 60. cache.put("key", "value", 5, SECONDS); 61. assertTrue(cache.containsKey("key")); 62. Thread.sleep(10000);
Each cache in Infinispan can offer a different set of features (for example transaction support, different replication modes or support for eviction), and you may want to use different caches for different classes of data in your application. To get a custom cache, you need to register it with the manager first:
33. 34. public static void main(String args[]) throws Exception { 35. EmbeddedCacheManager manager = new DefaultCacheManager(); 36. manager.defineConfiguration("custom-cache", new ConfigurationBuilder() 37. .eviction().strategy(LIRS).maxEntries(10) 38. .build()); 39. Cache<Object, Object> c = manager.getCache("custom-cache");
The example above uses Infinispan's fluent configuration, which offers the ability to configure your cache programmatically. However, should you prefer to use XML, then you may. We can create an identical cache to the one created with a programmatic configuration:
24. <infinispan 25. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 26. xsi:schemaLocation="urn:infinispan:config:7.0 http://www.infinispan.org/schemas/infinispan-config-7.0.xsd" 27. xmlns="urn:infinispan:config:7.0"> 28. 29. <cache-container default-cache="default"> 30. <local-cache name="xml-configured-cache"> 31. <eviction strategy="LIRS" max-entries="10" /> 32. </local-cache> 33. </cache-container>
We then need to load the configuration file, and use the programmatically defined cache:
29. 30. public static void main(String args[]) throws Exception { 31. Cache<Object, Object> c = new DefaultCacheManager("infinispan.xml").getCache("xml-configured-cache");