Ho, ho, hooo! It looks like all members of Infinispan Community have been nice and Santa brought you Spring Boot Starters!
This will make you even more productive and your code less verbose!
Why do I need starters?
Spring Boot Starters make the bootstrapping process much easier and faster. The starter brings you required Maven dependencies as well as some predefined configuration bits.
What do I need to get started?
The starter can operate in two modes: client/server (when you connect to a remote Infinispan Server cluster) and embedded (packaged along with your app). The former is the default. It's also possible to use both those modes at the same time (store some data along with your app and connect to a remote Infinispan Server cluster to perform some other type of operations).
Assuming you have an Infinispan Server running on IP address 192.168.0.17, all you need to do is to use the following dependencies:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.example</groupId> | |
<artifactId>myproject</artifactId> | |
<version>1.0.0-SNAPSHOT</version> | |
<properties> | |
<infinispan.starters.version>1.0.0.Alpha1</infinispan.starters.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.infinispan</groupId> | |
<artifactId>inifinispan-spring-boot-starter</artifactId> | |
<version>${infinispan.starters.version}</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
<version>${spring-boot.version}</version> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
By default, the starter will try to locate hotrod-client.properties file. The file should contain at least the server list:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
infinispan.client.hotrod.server_list=192.168.0.17:11222 |
It is also possible to create RemoteCacheManager's configuration manually:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class InfinispanCacheConfiguration { | |
public static final String IP = "192.168.0.17"; | |
@Bean | |
public InfinispanRemoteConfigurer infinispanRemoteConfigurer() { | |
return () -> new ConfigurationBuilder().addServer().host(IP).build(); | |
} | |
} |
That's it! Your app should successfully connect to a remote cluster and you should be able to inject RemoteCacheManager.
Using Infinispan embedded is even simpler than that. All you need to do is to add additional dependency to the classpath:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.example</groupId> | |
<artifactId>myproject</artifactId> | |
<version>1.0.0-SNAPSHOT</version> | |
<properties> | |
<infinispan.starters.version>1.0.0.Alpha1</infinispan.starters.version> | |
<version.infinispan>8.2.4.Final</version.infinispan> | |
</properties> | |
<dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>org.infinispan</groupId> | |
<artifactId>infinispan-bom</artifactId> | |
<version>${version.infinispan}</version> | |
<type>pom</type> | |
<scope>import</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>${spring-boot.version}</version> | |
<type>pom</type> | |
<scope>import</scope> | |
</dependency> | |
</dependencies> | |
</dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>org.infinispan</groupId> | |
<artifactId>inifinispan-spring-boot-starter</artifactId> | |
<version>${infinispan.starters.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.infinispan</groupId> | |
<artifactId>infinispan-core</artifactId> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
<version>${spring-boot.version}</version> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
The starter will provide you a preconfigured EmbeddedCacheManager. In order to customize the configuration, use the following code snippet:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class InfinispanCacheConfiguration { | |
public static final String TEST_CLUSTER = "TEST_CLUSTER"; | |
public static final String TEST_CACHE_NAME = "test-simple-cache"; | |
public static final String TEST_GLOBAL_JMX_DOMAIN = "test.infinispan"; | |
@Bean | |
public InfinispanCacheConfigurer cacheConfigurer() { | |
return cacheManager -> { | |
final org.infinispan.configuration.cache.Configuration testCache = | |
new ConfigurationBuilder().simpleCache(true) | |
.eviction().size(1000L).strategy(EvictionStrategy.LRU) | |
.jmxStatistics().enable() | |
.build(); | |
cacheManager.defineConfiguration(TEST_CACHE_NAME, testCache); | |
}; | |
} | |
@Bean | |
public InfinispanGlobalConfigurer globalConfigurer() { | |
return () -> { | |
final GlobalConfiguration globalConfiguration = new GlobalConfigurationBuilder() | |
.transport().clusterName(TEST_CLUSTER) | |
.globalJmxStatistics().jmxDomain(TEST_GLOBAL_JMX_DOMAIN).enable() | |
.build(); | |
return globalConfiguration; | |
}; | |
} |
Further reading
There are two link I highly recommend you to read. The first is the Spring Boot tutorial and the second is the Github page of the Starters project.
Kudos
Special thanks go to Marco Yuen, who donated us with Spring Boot Starters code and Tomasz Zabłocki, who updated them to current version and Stéphane Nicoll who spent tremendous amount of time reviewing the Starters.