Polish Infinispan support
Updated to the `EmbeddedCacheManager` interface and added support for default cache configuration. Added dependencies management for the JCache support with tests Fixes gh-2906, see gh-2633
This commit is contained in:
parent
76ac781a65
commit
0a9b8cbb41
|
|
@ -235,6 +235,11 @@
|
||||||
<artifactId>hornetq-jms-server</artifactId>
|
<artifactId>hornetq-jms-server</artifactId>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.infinispan</groupId>
|
||||||
|
<artifactId>infinispan-jcache</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.infinispan</groupId>
|
<groupId>org.infinispan</groupId>
|
||||||
<artifactId>infinispan-spring4</artifactId>
|
<artifactId>infinispan-spring4</artifactId>
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,15 @@
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.cache;
|
package org.springframework.boot.autoconfigure.cache;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.infinispan.configuration.cache.ConfigurationBuilder;
|
||||||
import org.infinispan.manager.DefaultCacheManager;
|
import org.infinispan.manager.DefaultCacheManager;
|
||||||
|
import org.infinispan.manager.EmbeddedCacheManager;
|
||||||
import org.infinispan.spring.provider.SpringEmbeddedCacheManager;
|
import org.infinispan.spring.provider.SpringEmbeddedCacheManager;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
|
@ -28,13 +35,11 @@ import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Infinispan cache configuration.
|
* Infinispan cache configuration.
|
||||||
*
|
*
|
||||||
* @author Eddú Meléndez
|
* @author Eddú Meléndez
|
||||||
|
* @author Stephane Nicoll
|
||||||
* @since 1.3.0
|
* @since 1.3.0
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
|
|
@ -46,24 +51,46 @@ public class InfinispanCacheConfiguration {
|
||||||
@Autowired
|
@Autowired
|
||||||
private CacheProperties cacheProperties;
|
private CacheProperties cacheProperties;
|
||||||
|
|
||||||
|
@Autowired(required = false)
|
||||||
|
private ConfigurationBuilder defaultConfigurationBuilder;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public CacheManager cacheManager() throws IOException {
|
public CacheManager cacheManager(EmbeddedCacheManager embeddedCacheManager) {
|
||||||
DefaultCacheManager defaultCacheManager = createCacheManager();
|
return new SpringEmbeddedCacheManager(embeddedCacheManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(destroyMethod = "stop")
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
public EmbeddedCacheManager infinispanCacheManager() throws IOException {
|
||||||
|
EmbeddedCacheManager infinispanCacheManager = createEmbeddedCacheManager();
|
||||||
List<String> cacheNames = this.cacheProperties.getCacheNames();
|
List<String> cacheNames = this.cacheProperties.getCacheNames();
|
||||||
if (!CollectionUtils.isEmpty(cacheNames)) {
|
if (!CollectionUtils.isEmpty(cacheNames)) {
|
||||||
for (String cacheName : cacheNames) {
|
for (String cacheName : cacheNames) {
|
||||||
defaultCacheManager.startCache(cacheName);
|
infinispanCacheManager.defineConfiguration(cacheName, getDefaultCacheConfiguration());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new SpringEmbeddedCacheManager(defaultCacheManager);
|
return infinispanCacheManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
private DefaultCacheManager createCacheManager() throws IOException {
|
private EmbeddedCacheManager createEmbeddedCacheManager() throws IOException {
|
||||||
Resource location = this.cacheProperties.resolveConfigLocation();
|
Resource location = this.cacheProperties.resolveConfigLocation();
|
||||||
if (location != null) {
|
if (location != null) {
|
||||||
return new DefaultCacheManager(this.cacheProperties.getConfig().getInputStream());
|
InputStream in = this.cacheProperties.getConfig().getInputStream();
|
||||||
|
try {
|
||||||
|
return new DefaultCacheManager(in);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return new DefaultCacheManager();
|
return new DefaultCacheManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private org.infinispan.configuration.cache.Configuration getDefaultCacheConfiguration() {
|
||||||
|
if (this.defaultConfigurationBuilder != null) {
|
||||||
|
return defaultConfigurationBuilder.build();
|
||||||
|
}
|
||||||
|
return new ConfigurationBuilder().build();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,9 @@ import com.hazelcast.cache.HazelcastCachingProvider;
|
||||||
import com.hazelcast.core.HazelcastInstance;
|
import com.hazelcast.core.HazelcastInstance;
|
||||||
import com.hazelcast.spring.cache.HazelcastCacheManager;
|
import com.hazelcast.spring.cache.HazelcastCacheManager;
|
||||||
import net.sf.ehcache.Status;
|
import net.sf.ehcache.Status;
|
||||||
|
import org.infinispan.configuration.cache.ConfigurationBuilder;
|
||||||
|
import org.infinispan.jcache.embedded.JCachingProvider;
|
||||||
|
import org.infinispan.spring.provider.SpringEmbeddedCacheManager;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
@ -71,6 +74,7 @@ import static org.hamcrest.Matchers.hasSize;
|
||||||
import static org.hamcrest.core.Is.is;
|
import static org.hamcrest.core.Is.is;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
|
@ -381,29 +385,58 @@ public class CacheAutoConfigurationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void infinispanCacheWithCaches() {
|
public void infinispanCacheWithConfig() {
|
||||||
SpringEmbeddedCacheManager cacheManager = null;
|
|
||||||
try {
|
|
||||||
load(DefaultCacheConfiguration.class, "spring.cache.type=infinispan",
|
load(DefaultCacheConfiguration.class, "spring.cache.type=infinispan",
|
||||||
"spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar");
|
"spring.cache.config=infinispan.xml");
|
||||||
cacheManager = validateCacheManager(SpringEmbeddedCacheManager.class);
|
SpringEmbeddedCacheManager cacheManager = validateCacheManager(SpringEmbeddedCacheManager.class);
|
||||||
assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar"));
|
assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar"));
|
||||||
} finally {
|
|
||||||
cacheManager.stop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void infinispanCacheWithConfig() {
|
public void infinispanCacheWithCaches() {
|
||||||
SpringEmbeddedCacheManager cacheManager = null;
|
|
||||||
try {
|
|
||||||
load(DefaultCacheConfiguration.class, "spring.cache.type=infinispan",
|
load(DefaultCacheConfiguration.class, "spring.cache.type=infinispan",
|
||||||
"spring.cache.config=infinispan.xml");
|
"spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar");
|
||||||
cacheManager = validateCacheManager(SpringEmbeddedCacheManager.class);
|
SpringEmbeddedCacheManager cacheManager = validateCacheManager(SpringEmbeddedCacheManager.class);
|
||||||
assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar"));
|
assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar"));
|
||||||
} finally {
|
assertThat(cacheManager.getCacheNames(), hasSize(2));
|
||||||
cacheManager.stop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void infinispanCacheWithCachesAndCustomConfig() {
|
||||||
|
load(InfinispanCustomConfiguration.class, "spring.cache.type=infinispan",
|
||||||
|
"spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar");
|
||||||
|
SpringEmbeddedCacheManager cacheManager = validateCacheManager(SpringEmbeddedCacheManager.class);
|
||||||
|
assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar"));
|
||||||
|
assertThat(cacheManager.getCacheNames(), hasSize(2));
|
||||||
|
|
||||||
|
ConfigurationBuilder defaultConfigurationBuilder = this.context
|
||||||
|
.getBean(ConfigurationBuilder.class);
|
||||||
|
verify(defaultConfigurationBuilder, times(2)).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void infinispanAsJCacheWithCaches() {
|
||||||
|
String cachingProviderFqn = JCachingProvider.class.getName();
|
||||||
|
load(DefaultCacheConfiguration.class, "spring.cache.type=jcache",
|
||||||
|
"spring.cache.jcache.provider=" + cachingProviderFqn,
|
||||||
|
"spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar");
|
||||||
|
JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class);
|
||||||
|
assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar"));
|
||||||
|
assertThat(cacheManager.getCacheNames(), hasSize(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void infinispanAsJCacheWithConfig() throws IOException {
|
||||||
|
String cachingProviderFqn = JCachingProvider.class.getName();
|
||||||
|
String configLocation = "infinispan.xml";
|
||||||
|
load(DefaultCacheConfiguration.class, "spring.cache.type=jcache",
|
||||||
|
"spring.cache.jcache.provider=" + cachingProviderFqn,
|
||||||
|
"spring.cache.config=" + configLocation);
|
||||||
|
JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class);
|
||||||
|
|
||||||
|
Resource configResource = new ClassPathResource(configLocation);
|
||||||
|
assertThat(cacheManager.getCacheManager().getURI(),
|
||||||
|
is(configResource.getURI()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -581,6 +614,19 @@ public class CacheAutoConfigurationTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableCaching
|
||||||
|
static class InfinispanCustomConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ConfigurationBuilder configurationBuilder() {
|
||||||
|
ConfigurationBuilder builder = mock(ConfigurationBuilder.class);
|
||||||
|
when(builder.build()).thenReturn(new ConfigurationBuilder().build());
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@Import({ GenericCacheConfiguration.class, RedisCacheConfiguration.class })
|
@Import({ GenericCacheConfiguration.class, RedisCacheConfiguration.class })
|
||||||
static class CustomCacheManagerConfiguration {
|
static class CustomCacheManagerConfiguration {
|
||||||
|
|
|
||||||
|
|
@ -1277,6 +1277,11 @@
|
||||||
<artifactId>hsqldb</artifactId>
|
<artifactId>hsqldb</artifactId>
|
||||||
<version>${hsqldb.version}</version>
|
<version>${hsqldb.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.infinispan</groupId>
|
||||||
|
<artifactId>infinispan-jcache</artifactId>
|
||||||
|
<version>${infinispan.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.infinispan</groupId>
|
<groupId>org.infinispan</groupId>
|
||||||
<artifactId>infinispan-spring4</artifactId>
|
<artifactId>infinispan-spring4</artifactId>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue