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 gh-8484pull/8582/head
parent
d32c3a7f43
commit
07c56c60ba
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.hazelcast.core.HazelcastInstance;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* JCache customization for Hazelcast.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(HazelcastInstance.class)
|
||||
class HazelcastJCacheCustomizationConfiguration {
|
||||
|
||||
@Bean
|
||||
public HazelcastPropertiesCustomizer hazelcastPropertiesCustomizer(
|
||||
ObjectProvider<HazelcastInstance> hazelcastInstance) {
|
||||
return new HazelcastPropertiesCustomizer(
|
||||
hazelcastInstance.getIfUnique());
|
||||
}
|
||||
|
||||
private static class HazelcastPropertiesCustomizer
|
||||
implements JCachePropertiesCustomizer {
|
||||
|
||||
private final HazelcastInstance hazelcastInstance;
|
||||
|
||||
HazelcastPropertiesCustomizer(HazelcastInstance hazelcastInstance) {
|
||||
this.hazelcastInstance = hazelcastInstance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customize(CacheProperties cacheProperties, Properties properties) {
|
||||
Resource configLocation = cacheProperties
|
||||
.resolveConfigLocation(cacheProperties.getJcache().getConfig());
|
||||
if (configLocation != null) {
|
||||
// Hazelcast does not use the URI as a mean to specify a custom config.
|
||||
properties.setProperty("hazelcast.config.location",
|
||||
toUri(configLocation).toString());
|
||||
}
|
||||
else if (this.hazelcastInstance != null) {
|
||||
String name = this.hazelcastInstance.getName();
|
||||
if (name != null) {
|
||||
properties.setProperty("hazelcast.instance.name", name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static URI toUri(Resource config) {
|
||||
try {
|
||||
return config.getURI();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalArgumentException(
|
||||
"Could not get URI from " + config, ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.util.Properties;
|
||||
|
||||
import javax.cache.CacheManager;
|
||||
import javax.cache.spi.CachingProvider;
|
||||
|
||||
/**
|
||||
* Callback interface that can be implemented by beans wishing to customize the properties
|
||||
* used by the {@link CachingProvider} to create the {@link CacheManager}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
interface JCachePropertiesCustomizer {
|
||||
|
||||
/**
|
||||
* Customize the properties.
|
||||
* @param cacheProperties the cache properties
|
||||
* @param properties the current properties
|
||||
* @see CachingProvider#getCacheManager(java.net.URI, ClassLoader, Properties)
|
||||
*/
|
||||
void customize(CacheProperties cacheProperties, Properties properties);
|
||||
|
||||
}
|
Loading…
Reference in New Issue