Remove spring.cache.hazelcast.config
Remove the creation of a separate `HazelcastInstance` specific to caching. Instead, the general `HazelcastAutoConfiguration` is used and if a `HazelcastInstance` bean is present we wrap it in a `CacheManager`. Closes gh-8470
This commit is contained in:
parent
d811b5fb1d
commit
28a8e3db2f
|
|
@ -51,8 +51,6 @@ public class CacheProperties {
|
|||
|
||||
private final EhCache ehcache = new EhCache();
|
||||
|
||||
private final Hazelcast hazelcast = new Hazelcast();
|
||||
|
||||
private final Infinispan infinispan = new Infinispan();
|
||||
|
||||
private final JCache jcache = new JCache();
|
||||
|
|
@ -85,11 +83,6 @@ public class CacheProperties {
|
|||
return this.ehcache;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public Hazelcast getHazelcast() {
|
||||
return this.hazelcast;
|
||||
}
|
||||
|
||||
public Infinispan getInfinispan() {
|
||||
return this.infinispan;
|
||||
}
|
||||
|
|
@ -184,30 +177,6 @@ public class CacheProperties {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Hazelcast specific cache properties.
|
||||
*/
|
||||
@Deprecated
|
||||
public static class Hazelcast {
|
||||
|
||||
/**
|
||||
* The location of the configuration file to use to initialize Hazelcast.
|
||||
*/
|
||||
private Resource config;
|
||||
|
||||
@DeprecatedConfigurationProperty(replacement = "spring.hazelcast.config",
|
||||
reason = "Use general hazelcast auto-configuration instead.")
|
||||
@Deprecated
|
||||
public Resource getConfig() {
|
||||
return this.config;
|
||||
}
|
||||
|
||||
public void setConfig(Resource config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Infinispan specific cache properties.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -16,17 +16,20 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.cache;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.hazelcast.core.HazelcastInstance;
|
||||
import com.hazelcast.spring.cache.HazelcastCacheManager;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
|
||||
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.hazelcast.HazelcastConfigResourceCondition;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* Hazelcast cache configuration. Can either reuse the {@link HazelcastInstance} that has
|
||||
|
|
@ -44,8 +47,21 @@ import org.springframework.context.annotation.Import;
|
|||
@ConditionalOnClass({ HazelcastInstance.class, HazelcastCacheManager.class })
|
||||
@ConditionalOnMissingBean(CacheManager.class)
|
||||
@Conditional(CacheCondition.class)
|
||||
@Import({ HazelcastInstanceConfiguration.Existing.class,
|
||||
HazelcastInstanceConfiguration.Specific.class })
|
||||
@ConditionalOnSingleCandidate(HazelcastInstance.class)
|
||||
class HazelcastCacheConfiguration {
|
||||
|
||||
private final CacheManagerCustomizers customizers;
|
||||
|
||||
HazelcastCacheConfiguration(CacheManagerCustomizers customizers) {
|
||||
this.customizers = customizers;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public HazelcastCacheManager cacheManager(
|
||||
HazelcastInstance existingHazelcastInstance) throws IOException {
|
||||
HazelcastCacheManager cacheManager = new HazelcastCacheManager(
|
||||
existingHazelcastInstance);
|
||||
return this.customizers.customize(cacheManager);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,135 +0,0 @@
|
|||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.cache;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.hazelcast.core.Hazelcast;
|
||||
import com.hazelcast.core.HazelcastInstance;
|
||||
import com.hazelcast.spring.cache.HazelcastCacheManager;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
|
||||
import org.springframework.boot.autoconfigure.hazelcast.HazelcastConfigResourceCondition;
|
||||
import org.springframework.boot.autoconfigure.hazelcast.HazelcastInstanceFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* Actual {@link HazelcastInstance} configurations imported by
|
||||
* {@link HazelcastCacheConfiguration}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
abstract class HazelcastInstanceConfiguration {
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnSingleCandidate(HazelcastInstance.class)
|
||||
static class Existing {
|
||||
|
||||
private final CacheProperties cacheProperties;
|
||||
|
||||
private final CacheManagerCustomizers customizers;
|
||||
|
||||
Existing(CacheProperties cacheProperties, CacheManagerCustomizers customizers) {
|
||||
this.cacheProperties = cacheProperties;
|
||||
this.customizers = customizers;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public HazelcastCacheManager cacheManager(
|
||||
HazelcastInstance existingHazelcastInstance) throws IOException {
|
||||
Resource config = this.cacheProperties.getHazelcast().getConfig();
|
||||
Resource location = this.cacheProperties.resolveConfigLocation(config);
|
||||
if (location != null) {
|
||||
HazelcastInstance cacheHazelcastInstance = new HazelcastInstanceFactory(
|
||||
location).getHazelcastInstance();
|
||||
return new CloseableHazelcastCacheManager(cacheHazelcastInstance);
|
||||
}
|
||||
HazelcastCacheManager cacheManager = new HazelcastCacheManager(
|
||||
existingHazelcastInstance);
|
||||
return this.customizers.customize(cacheManager);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(HazelcastInstance.class)
|
||||
@Conditional(ConfigAvailableCondition.class)
|
||||
static class Specific {
|
||||
|
||||
private final CacheProperties cacheProperties;
|
||||
|
||||
private final CacheManagerCustomizers customizers;
|
||||
|
||||
Specific(CacheProperties cacheProperties, CacheManagerCustomizers customizers) {
|
||||
this.cacheProperties = cacheProperties;
|
||||
this.customizers = customizers;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public HazelcastInstance hazelcastInstance() throws IOException {
|
||||
Resource config = this.cacheProperties.getHazelcast().getConfig();
|
||||
Resource location = this.cacheProperties.resolveConfigLocation(config);
|
||||
if (location != null) {
|
||||
return new HazelcastInstanceFactory(location).getHazelcastInstance();
|
||||
}
|
||||
return Hazelcast.newHazelcastInstance();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public HazelcastCacheManager cacheManager() throws IOException {
|
||||
HazelcastCacheManager cacheManager = new HazelcastCacheManager(
|
||||
hazelcastInstance());
|
||||
return this.customizers.customize(cacheManager);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link HazelcastConfigResourceCondition} that checks if the
|
||||
* {@code spring.cache.hazelcast.config} configuration key is defined.
|
||||
*/
|
||||
static class ConfigAvailableCondition extends HazelcastConfigResourceCondition {
|
||||
|
||||
ConfigAvailableCondition() {
|
||||
super("spring.cache.hazelcast", "config");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class CloseableHazelcastCacheManager extends HazelcastCacheManager
|
||||
implements Closeable {
|
||||
|
||||
private final HazelcastInstance hazelcastInstance;
|
||||
|
||||
CloseableHazelcastCacheManager(HazelcastInstance hazelcastInstance) {
|
||||
super(hazelcastInstance);
|
||||
this.hazelcastInstance = hazelcastInstance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
this.hazelcastInstance.shutdown();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -52,7 +52,6 @@ import org.junit.rules.ExpectedException;
|
|||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.cache.support.MockCachingProvider;
|
||||
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
|
||||
import org.springframework.boot.test.util.EnvironmentTestUtils;
|
||||
|
|
@ -347,7 +346,7 @@ public class CacheAutoConfigurationTests {
|
|||
@Test
|
||||
public void jCacheCacheWithConfig() throws IOException {
|
||||
String cachingProviderFqn = MockCachingProvider.class.getName();
|
||||
String configLocation = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml";
|
||||
String configLocation = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml";
|
||||
load(JCacheCustomConfiguration.class, "spring.cache.type=jcache",
|
||||
"spring.cache.jcache.provider=" + cachingProviderFqn,
|
||||
"spring.cache.jcache.config=" + configLocation);
|
||||
|
|
@ -431,7 +430,8 @@ public class CacheAutoConfigurationTests {
|
|||
|
||||
@Test
|
||||
public void hazelcastCacheExplicit() {
|
||||
load(DefaultCacheConfiguration.class, "spring.cache.type=hazelcast");
|
||||
load(new Class[] { HazelcastAutoConfiguration.class,
|
||||
DefaultCacheConfiguration.class }, "spring.cache.type=hazelcast");
|
||||
HazelcastCacheManager cacheManager = validateCacheManager(
|
||||
HazelcastCacheManager.class);
|
||||
// NOTE: the hazelcast implementation knows about a cache in a lazy manner.
|
||||
|
|
@ -443,38 +443,10 @@ public class CacheAutoConfigurationTests {
|
|||
|
||||
@Test
|
||||
public void hazelcastCacheWithCustomizers() {
|
||||
testCustomizers(DefaultCacheAndCustomizersConfiguration.class, "hazelcast",
|
||||
testCustomizers(HazelcastCacheAndCustomizersConfiguration.class, "hazelcast",
|
||||
"allCacheManagerCustomizer", "hazelcastCacheManagerCustomizer");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Deprecated
|
||||
public void hazelcastCacheWithConfig() throws IOException {
|
||||
load(DefaultCacheConfiguration.class, "spring.cache.type=hazelcast",
|
||||
"spring.cache.hazelcast.config=org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml");
|
||||
HazelcastInstance hazelcastInstance = this.context
|
||||
.getBean(HazelcastInstance.class);
|
||||
HazelcastCacheManager cacheManager = validateCacheManager(
|
||||
HazelcastCacheManager.class);
|
||||
HazelcastInstance actual = cacheManager.getHazelcastInstance();
|
||||
assertThat(actual).isSameAs(hazelcastInstance);
|
||||
assertThat(actual.getConfig().getConfigurationUrl())
|
||||
.isEqualTo(new ClassPathResource(
|
||||
"org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml")
|
||||
.getURL());
|
||||
cacheManager.getCache("foobar");
|
||||
assertThat(cacheManager.getCacheNames()).containsOnly("foobar");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Deprecated
|
||||
public void hazelcastWithWrongConfig() {
|
||||
this.thrown.expect(BeanCreationException.class);
|
||||
this.thrown.expectMessage("foo/bar/unknown.xml");
|
||||
load(DefaultCacheConfiguration.class, "spring.cache.type=hazelcast",
|
||||
"spring.cache.hazelcast.config=foo/bar/unknown.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hazelcastCacheWithExistingHazelcastInstance() {
|
||||
load(HazelcastCustomHazelcastInstance.class, "spring.cache.type=hazelcast");
|
||||
|
|
@ -485,49 +457,20 @@ public class CacheAutoConfigurationTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void hazelcastCacheWithMainHazelcastAutoConfiguration() throws IOException {
|
||||
String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml";
|
||||
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(applicationContext,
|
||||
"spring.cache.type=hazelcast", "spring.hazelcast.config=" + mainConfig);
|
||||
applicationContext.register(DefaultCacheConfiguration.class);
|
||||
applicationContext.register(HazelcastAndCacheConfiguration.class);
|
||||
applicationContext.refresh();
|
||||
this.context = applicationContext;
|
||||
public void hazelcastCacheWithHazelcastAutoConfiguration() throws IOException {
|
||||
String hazelcastConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml";
|
||||
load(new Class[] { HazelcastAutoConfiguration.class,
|
||||
DefaultCacheConfiguration.class }, "spring.cache.type=hazelcast",
|
||||
"spring.hazelcast.config=" + hazelcastConfig);
|
||||
HazelcastCacheManager cacheManager = validateCacheManager(
|
||||
HazelcastCacheManager.class);
|
||||
HazelcastInstance hazelcastInstance = this.context
|
||||
.getBean(HazelcastInstance.class);
|
||||
assertThat(cacheManager.getHazelcastInstance()).isEqualTo(hazelcastInstance);
|
||||
assertThat(cacheManager.getHazelcastInstance()).isSameAs(hazelcastInstance);
|
||||
assertThat(hazelcastInstance.getConfig().getConfigurationFile())
|
||||
.isEqualTo(new ClassPathResource(mainConfig).getFile());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Deprecated
|
||||
public void hazelcastCacheWithMainHazelcastAutoConfigurationAndSeparateCacheConfig()
|
||||
throws IOException {
|
||||
String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml";
|
||||
String cacheConfig = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml";
|
||||
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(applicationContext,
|
||||
"spring.cache.type=hazelcast",
|
||||
"spring.cache.hazelcast.config=" + cacheConfig,
|
||||
"spring.hazelcast.config=" + mainConfig);
|
||||
applicationContext.register(DefaultCacheConfiguration.class);
|
||||
applicationContext.register(HazelcastAndCacheConfiguration.class);
|
||||
applicationContext.refresh();
|
||||
this.context = applicationContext;
|
||||
HazelcastInstance hazelcastInstance = this.context
|
||||
.getBean(HazelcastInstance.class);
|
||||
HazelcastCacheManager cacheManager = validateCacheManager(
|
||||
HazelcastCacheManager.class);
|
||||
HazelcastInstance cacheHazelcastInstance = cacheManager.getHazelcastInstance();
|
||||
assertThat(cacheHazelcastInstance).isNotEqualTo(hazelcastInstance); // Our custom
|
||||
assertThat(hazelcastInstance.getConfig().getConfigurationFile())
|
||||
.isEqualTo(new ClassPathResource(mainConfig).getFile());
|
||||
assertThat(cacheHazelcastInstance.getConfig().getConfigurationFile())
|
||||
.isEqualTo(new ClassPathResource(cacheConfig).getFile());
|
||||
.isEqualTo(new ClassPathResource(hazelcastConfig).getFile());
|
||||
assertThat(cacheManager.getCache("foobar")).isNotNull();
|
||||
assertThat(cacheManager.getCacheNames()).containsOnly("foobar");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -549,7 +492,7 @@ public class CacheAutoConfigurationTests {
|
|||
@Test
|
||||
public void hazelcastAsJCacheWithConfig() throws IOException {
|
||||
String cachingProviderFqn = HazelcastCachingProvider.class.getName();
|
||||
String configLocation = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml";
|
||||
String configLocation = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml";
|
||||
load(DefaultCacheConfiguration.class, "spring.cache.type=jcache",
|
||||
"spring.cache.jcache.provider=" + cachingProviderFqn,
|
||||
"spring.cache.jcache.config=" + configLocation);
|
||||
|
|
@ -711,9 +654,13 @@ public class CacheAutoConfigurationTests {
|
|||
}
|
||||
|
||||
private void load(Class<?> config, String... environment) {
|
||||
load(new Class[]{config}, environment);
|
||||
}
|
||||
|
||||
private void load(Class<?>[] configs, String... environment) {
|
||||
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
|
||||
applicationContext.register(config);
|
||||
applicationContext.register(configs);
|
||||
applicationContext.register(CacheAutoConfiguration.class);
|
||||
applicationContext.refresh();
|
||||
this.context = applicationContext;
|
||||
|
|
@ -760,6 +707,14 @@ public class CacheAutoConfigurationTests {
|
|||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
@Import({ HazelcastAutoConfiguration.class,
|
||||
CacheManagerCustomizersConfiguration.class })
|
||||
static class HazelcastCacheAndCustomizersConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
static class CouchbaseCacheConfiguration {
|
||||
|
|
@ -870,13 +825,6 @@ public class CacheAutoConfigurationTests {
|
|||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ImportAutoConfiguration({ CacheAutoConfiguration.class,
|
||||
HazelcastAutoConfiguration.class })
|
||||
static class HazelcastAndCacheConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
static class InfinispanCustomConfiguration {
|
||||
|
|
|
|||
|
|
@ -1,17 +0,0 @@
|
|||
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.7.xsd"
|
||||
xmlns="http://www.hazelcast.com/schema/config"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
|
||||
<map name="foobar">
|
||||
<time-to-live-seconds>3600</time-to-live-seconds>
|
||||
<max-idle-seconds>600</max-idle-seconds>
|
||||
</map>
|
||||
|
||||
<network>
|
||||
<join>
|
||||
<tcp-ip enabled="false"/>
|
||||
<multicast enabled="false"/>
|
||||
</join>
|
||||
</network>
|
||||
|
||||
</hazelcast>
|
||||
|
|
@ -4,6 +4,11 @@
|
|||
|
||||
<queue name="foobar"/>
|
||||
|
||||
<map name="foobar">
|
||||
<time-to-live-seconds>3600</time-to-live-seconds>
|
||||
<max-idle-seconds>600</max-idle-seconds>
|
||||
</map>
|
||||
|
||||
<network>
|
||||
<join>
|
||||
<tcp-ip enabled="false"/>
|
||||
|
|
|
|||
Loading…
Reference in New Issue