mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
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>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.infinispan</groupId>
|
||||
<artifactId>infinispan-jcache</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.infinispan</groupId>
|
||||
<artifactId>infinispan-spring4</artifactId>
|
||||
|
@ -16,8 +16,15 @@
|
||||
|
||||
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.EmbeddedCacheManager;
|
||||
import org.infinispan.spring.provider.SpringEmbeddedCacheManager;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
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.util.CollectionUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Infinispan cache configuration.
|
||||
*
|
||||
* @author Eddú Meléndez
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.3.0
|
||||
*/
|
||||
@Configuration
|
||||
@ -46,24 +51,46 @@ public class InfinispanCacheConfiguration {
|
||||
@Autowired
|
||||
private CacheProperties cacheProperties;
|
||||
|
||||
@Autowired(required = false)
|
||||
private ConfigurationBuilder defaultConfigurationBuilder;
|
||||
|
||||
@Bean
|
||||
public CacheManager cacheManager() throws IOException {
|
||||
DefaultCacheManager defaultCacheManager = createCacheManager();
|
||||
public CacheManager cacheManager(EmbeddedCacheManager embeddedCacheManager) {
|
||||
return new SpringEmbeddedCacheManager(embeddedCacheManager);
|
||||
}
|
||||
|
||||
@Bean(destroyMethod = "stop")
|
||||
@ConditionalOnMissingBean
|
||||
public EmbeddedCacheManager infinispanCacheManager() throws IOException {
|
||||
EmbeddedCacheManager infinispanCacheManager = createEmbeddedCacheManager();
|
||||
List<String> cacheNames = this.cacheProperties.getCacheNames();
|
||||
if (!CollectionUtils.isEmpty(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();
|
||||
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();
|
||||
}
|
||||
|
||||
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.spring.cache.HazelcastCacheManager;
|
||||
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.Rule;
|
||||
import org.junit.Test;
|
||||
@ -71,6 +74,7 @@ import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ -381,29 +385,58 @@ public class CacheAutoConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void infinispanCacheWithCaches() {
|
||||
SpringEmbeddedCacheManager cacheManager = null;
|
||||
try {
|
||||
load(DefaultCacheConfiguration.class, "spring.cache.type=infinispan",
|
||||
"spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar");
|
||||
cacheManager = validateCacheManager(SpringEmbeddedCacheManager.class);
|
||||
assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar"));
|
||||
} finally {
|
||||
cacheManager.stop();
|
||||
}
|
||||
public void infinispanCacheWithConfig() {
|
||||
load(DefaultCacheConfiguration.class, "spring.cache.type=infinispan",
|
||||
"spring.cache.config=infinispan.xml");
|
||||
SpringEmbeddedCacheManager cacheManager = validateCacheManager(SpringEmbeddedCacheManager.class);
|
||||
assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void infinispanCacheWithConfig() {
|
||||
SpringEmbeddedCacheManager cacheManager = null;
|
||||
try {
|
||||
load(DefaultCacheConfiguration.class, "spring.cache.type=infinispan",
|
||||
"spring.cache.config=infinispan.xml");
|
||||
cacheManager = validateCacheManager(SpringEmbeddedCacheManager.class);
|
||||
assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar"));
|
||||
} finally {
|
||||
cacheManager.stop();
|
||||
}
|
||||
public void infinispanCacheWithCaches() {
|
||||
load(DefaultCacheConfiguration.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));
|
||||
}
|
||||
|
||||
@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
|
||||
@ -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
|
||||
@Import({ GenericCacheConfiguration.class, RedisCacheConfiguration.class })
|
||||
static class CustomCacheManagerConfiguration {
|
||||
|
@ -1277,6 +1277,11 @@
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<version>${hsqldb.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.infinispan</groupId>
|
||||
<artifactId>infinispan-jcache</artifactId>
|
||||
<version>${infinispan.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.infinispan</groupId>
|
||||
<artifactId>infinispan-spring4</artifactId>
|
||||
@ -1855,4 +1860,4 @@
|
||||
<id>integration-test</id>
|
||||
</profile>
|
||||
</profiles>
|
||||
</project>
|
||||
</project>
|
Loading…
Reference in New Issue
Block a user