Skip to content

Commit 07c56c6

Browse files
committed
Make sure Hazelcast bootstraps before any JCache setup
This commit makes sure that if a `javax.cache.CacheManager` is required, an auto-configured `HazelcastInstance` is fully resolved first. This prevents the case where the JCache bootstrap actually starts an instance early, followed by a second (potentially unwanted) instance created by the regular auto-configuration. Since the JCache implementation works with an `HazelcastInstance` behind the scenes, if there is one `HazelcastInstance` configured and it has a name, then we configure the `CacheProvider` to use that. Future Hazelcast version will allow to pass the instance directly (i.e. not requiring an actual name). Closes spring-projectsgh-8484
1 parent d32c3a7 commit 07c56c6

File tree

6 files changed

+242
-49
lines changed

6 files changed

+242
-49
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.cache;
18+
19+
import java.io.IOException;
20+
import java.net.URI;
21+
import java.util.Properties;
22+
23+
import com.hazelcast.core.HazelcastInstance;
24+
25+
import org.springframework.beans.factory.ObjectProvider;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.core.io.Resource;
30+
31+
/**
32+
* JCache customization for Hazelcast.
33+
*
34+
* @author Stephane Nicoll
35+
*/
36+
@Configuration
37+
@ConditionalOnClass(HazelcastInstance.class)
38+
class HazelcastJCacheCustomizationConfiguration {
39+
40+
@Bean
41+
public HazelcastPropertiesCustomizer hazelcastPropertiesCustomizer(
42+
ObjectProvider<HazelcastInstance> hazelcastInstance) {
43+
return new HazelcastPropertiesCustomizer(
44+
hazelcastInstance.getIfUnique());
45+
}
46+
47+
private static class HazelcastPropertiesCustomizer
48+
implements JCachePropertiesCustomizer {
49+
50+
private final HazelcastInstance hazelcastInstance;
51+
52+
HazelcastPropertiesCustomizer(HazelcastInstance hazelcastInstance) {
53+
this.hazelcastInstance = hazelcastInstance;
54+
}
55+
56+
@Override
57+
public void customize(CacheProperties cacheProperties, Properties properties) {
58+
Resource configLocation = cacheProperties
59+
.resolveConfigLocation(cacheProperties.getJcache().getConfig());
60+
if (configLocation != null) {
61+
// Hazelcast does not use the URI as a mean to specify a custom config.
62+
properties.setProperty("hazelcast.config.location",
63+
toUri(configLocation).toString());
64+
}
65+
else if (this.hazelcastInstance != null) {
66+
String name = this.hazelcastInstance.getName();
67+
if (name != null) {
68+
properties.setProperty("hazelcast.instance.name", name);
69+
}
70+
}
71+
}
72+
73+
private static URI toUri(Resource config) {
74+
try {
75+
return config.getURI();
76+
}
77+
catch (IOException ex) {
78+
throw new IllegalArgumentException(
79+
"Could not get URI from " + config, ex);
80+
}
81+
}
82+
83+
}
84+
85+
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.context.annotation.ConditionContext;
4141
import org.springframework.context.annotation.Conditional;
4242
import org.springframework.context.annotation.Configuration;
43+
import org.springframework.context.annotation.Import;
4344
import org.springframework.core.Ordered;
4445
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
4546
import org.springframework.core.annotation.Order;
@@ -59,6 +60,7 @@
5960
@ConditionalOnMissingBean(org.springframework.cache.CacheManager.class)
6061
@Conditional({ CacheCondition.class,
6162
JCacheCacheConfiguration.JCacheAvailableCondition.class })
63+
@Import(HazelcastJCacheCustomizationConfiguration.class)
6264
class JCacheCacheConfiguration {
6365

6466
private final CacheProperties cacheProperties;
@@ -69,15 +71,19 @@ class JCacheCacheConfiguration {
6971

7072
private final List<JCacheManagerCustomizer> cacheManagerCustomizers;
7173

74+
private final List<JCachePropertiesCustomizer> cachePropertiesCustomizers;
75+
7276
JCacheCacheConfiguration(CacheProperties cacheProperties,
7377
CacheManagerCustomizers customizers,
7478
ObjectProvider<javax.cache.configuration.Configuration<?, ?>> defaultCacheConfigurationProvider,
75-
ObjectProvider<List<JCacheManagerCustomizer>> cacheManagerCustomizersProvider) {
79+
ObjectProvider<List<JCacheManagerCustomizer>> cacheManagerCustomizersProvider,
80+
ObjectProvider<List<JCachePropertiesCustomizer>> cachePropertiesCustomizers) {
7681
this.cacheProperties = cacheProperties;
7782
this.customizers = customizers;
7883
this.defaultCacheConfiguration = defaultCacheConfigurationProvider
7984
.getIfAvailable();
8085
this.cacheManagerCustomizers = cacheManagerCustomizersProvider.getIfAvailable();
86+
this.cachePropertiesCustomizers = cachePropertiesCustomizers.getIfAvailable();
8187
}
8288

8389
@Bean
@@ -103,14 +109,14 @@ public CacheManager jCacheCacheManager() throws IOException {
103109
private CacheManager createCacheManager() throws IOException {
104110
CachingProvider cachingProvider = getCachingProvider(
105111
this.cacheProperties.getJcache().getProvider());
112+
Properties properties = createCacheManagerProperties();
106113
Resource configLocation = this.cacheProperties
107114
.resolveConfigLocation(this.cacheProperties.getJcache().getConfig());
108115
if (configLocation != null) {
109116
return cachingProvider.getCacheManager(configLocation.getURI(),
110-
cachingProvider.getDefaultClassLoader(),
111-
createCacheManagerProperties(configLocation));
117+
cachingProvider.getDefaultClassLoader(), properties);
112118
}
113-
return cachingProvider.getCacheManager();
119+
return cachingProvider.getCacheManager(null, null, properties);
114120
}
115121

116122
private CachingProvider getCachingProvider(String cachingProviderFqn) {
@@ -120,12 +126,13 @@ private CachingProvider getCachingProvider(String cachingProviderFqn) {
120126
return Caching.getCachingProvider();
121127
}
122128

123-
private Properties createCacheManagerProperties(Resource configLocation)
124-
throws IOException {
129+
private Properties createCacheManagerProperties() {
125130
Properties properties = new Properties();
126-
// Hazelcast does not use the URI as a mean to specify a custom config.
127-
properties.setProperty("hazelcast.config.location",
128-
configLocation.getURI().toString());
131+
if (this.cachePropertiesCustomizers != null) {
132+
for (JCachePropertiesCustomizer customizer : this.cachePropertiesCustomizers) {
133+
customizer.customize(this.cacheProperties, properties);
134+
}
135+
}
129136
return properties;
130137
}
131138

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.cache;
18+
19+
import java.util.Properties;
20+
21+
import javax.cache.CacheManager;
22+
import javax.cache.spi.CachingProvider;
23+
24+
/**
25+
* Callback interface that can be implemented by beans wishing to customize the properties
26+
* used by the {@link CachingProvider} to create the {@link CacheManager}.
27+
*
28+
* @author Stephane Nicoll
29+
*/
30+
interface JCachePropertiesCustomizer {
31+
32+
/**
33+
* Customize the properties.
34+
* @param cacheProperties the cache properties
35+
* @param properties the current properties
36+
* @see CachingProvider#getCacheManager(java.net.URI, ClassLoader, Properties)
37+
*/
38+
void customize(CacheProperties cacheProperties, Properties properties);
39+
40+
}

0 commit comments

Comments
 (0)