Add auto-configuration support for Hazelcast client

See gh-7469
pull/2934/merge
Vedran Pavic 9 years ago committed by Stephane Nicoll
parent ae3225e76e
commit 3fbf1a2ea6

@ -75,6 +75,11 @@
<artifactId>hazelcast</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-client</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-spring</artifactId>

@ -16,21 +16,15 @@
package org.springframework.boot.autoconfigure.hazelcast;
import java.io.IOException;
import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.core.HazelcastInstance;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.context.annotation.Import;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Hazelcast. Creates a
@ -38,58 +32,27 @@ import org.springframework.core.io.Resource;
* configuration file is found in the environment.
*
* @author Stephane Nicoll
* @author Madhura Bhave
* @author Vedran Pavic
* @since 1.3.0
* @see HazelcastConfigResourceCondition
*/
@Configuration
@ConditionalOnClass(HazelcastInstance.class)
@ConditionalOnMissingBean(HazelcastInstance.class)
@EnableConfigurationProperties(HazelcastProperties.class)
public class HazelcastAutoConfiguration {
@Configuration
@ConditionalOnMissingBean(Config.class)
@Conditional(ConfigAvailableCondition.class)
static class HazelcastConfigFileConfiguration {
private final HazelcastProperties hazelcastProperties;
HazelcastConfigFileConfiguration(HazelcastProperties hazelcastProperties) {
this.hazelcastProperties = hazelcastProperties;
}
@Bean
public HazelcastInstance hazelcastInstance() throws IOException {
Resource config = this.hazelcastProperties.resolveConfigLocation();
if (config != null) {
return new HazelcastInstanceFactory(config).getHazelcastInstance();
}
return Hazelcast.newHazelcastInstance();
}
@ConditionalOnMissingBean(HazelcastInstance.class)
@Import(HazelcastServerConfiguration.class)
static class ServerConfiguration {
}
@Configuration
@ConditionalOnSingleCandidate(Config.class)
static class HazelcastConfigConfiguration {
@Bean
public HazelcastInstance hazelcastInstance(Config config) {
return new HazelcastInstanceFactory(config).getHazelcastInstance();
}
}
/**
* {@link HazelcastConfigResourceCondition} that checks if the
* {@code spring.hazelcast.config} configuration key is defined.
*/
static class ConfigAvailableCondition extends HazelcastConfigResourceCondition {
ConfigAvailableCondition() {
super("spring.hazelcast.config");
}
@ConditionalOnClass(HazelcastClient.class)
@ConditionalOnMissingBean(HazelcastInstance.class)
@Import(HazelcastClientConfiguration.class)
static class ClientConfiguration {
}

@ -0,0 +1,83 @@
/*
* 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.hazelcast;
import java.io.IOException;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.core.HazelcastInstance;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
/**
* Configuration for Hazelcast client.
*
* @author Vedran Pavic
* @since 2.0.0
*/
class HazelcastClientConfiguration {
static final String CONFIG_SYSTEM_PROPERTY = "hazelcast.client.config";
@Configuration
@ConditionalOnMissingBean(ClientConfig.class)
@Conditional(ConfigAvailableCondition.class)
static class HazelcastClientConfigFileConfiguration {
@Bean
public HazelcastInstance hazelcastInstance(HazelcastProperties properties)
throws IOException {
Resource config = properties.resolveConfigLocation();
if (config != null) {
return HazelcastInstanceFactory.createHazelcastClient(config);
}
return HazelcastClient.newHazelcastClient();
}
}
@Configuration
@ConditionalOnSingleCandidate(ClientConfig.class)
static class HazelcastClientConfigConfiguration {
@Bean
public HazelcastInstance hazelcastInstance(ClientConfig config) {
return HazelcastInstanceFactory.createHazelcastClient(config);
}
}
/**
* {@link HazelcastConfigResourceCondition} that checks if the
* {@code spring.hazelcast.config} configuration key is defined.
*/
static class ConfigAvailableCondition extends HazelcastConfigResourceCondition {
ConfigAvailableCondition() {
super(CONFIG_SYSTEM_PROPERTY, "file:./hazelcast-client.xml",
"classpath:/hazelcast-client.xml");
}
}
}

@ -21,6 +21,7 @@ import org.springframework.boot.autoconfigure.condition.ResourceCondition;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.Assert;
/**
* {@link SpringBootCondition} used to check if the Hazelcast configuration is available.
@ -29,22 +30,26 @@ import org.springframework.core.type.AnnotatedTypeMetadata;
*
* @author Stephane Nicoll
* @author Madhura Bhave
* @author Vedran Pavic
* @since 1.3.0
*/
public abstract class HazelcastConfigResourceCondition extends ResourceCondition {
static final String CONFIG_SYSTEM_PROPERTY = "hazelcast.config";
private final String configSystemProperty;
protected HazelcastConfigResourceCondition(String property) {
super("Hazelcast", property, "file:./hazelcast.xml", "classpath:/hazelcast.xml");
protected HazelcastConfigResourceCondition(String configSystemProperty,
String... resourceLocations) {
super("Hazelcast", "spring.hazelcast.config", resourceLocations);
Assert.notNull(configSystemProperty, "ConfigSystemProperty must not be null");
this.configSystemProperty = configSystemProperty;
}
@Override
protected ConditionOutcome getResourceOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
if (System.getProperty(CONFIG_SYSTEM_PROPERTY) != null) {
if (System.getProperty(this.configSystemProperty) != null) {
return ConditionOutcome.match(startConditionMessage()
.because("System property '" + CONFIG_SYSTEM_PROPERTY + "' is set."));
.because("System property '" + this.configSystemProperty + "' is set."));
}
return super.getResourceOutcome(context, metadata);
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* 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.
@ -19,6 +19,9 @@ package org.springframework.boot.autoconfigure.hazelcast;
import java.io.IOException;
import java.net.URL;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.config.XmlClientConfigBuilder;
import com.hazelcast.config.Config;
import com.hazelcast.config.XmlConfigBuilder;
import com.hazelcast.core.Hazelcast;
@ -34,32 +37,33 @@ import org.springframework.util.StringUtils;
*
* @author Stephane Nicoll
* @author Phillip Webb
* @author Vedran Pavic
* @since 1.3.0
*/
public class HazelcastInstanceFactory {
private Config config;
/**
* Create a {@link HazelcastInstanceFactory} for the specified configuration location.
* @param configLocation the location of the configuration file
* @throws IOException if the configuration location could not be read
*/
public HazelcastInstanceFactory(Resource configLocation) throws IOException {
Assert.notNull(configLocation, "ConfigLocation must not be null");
this.config = getConfig(configLocation);
}
public abstract class HazelcastInstanceFactory {
/**
* Create a {@link HazelcastInstanceFactory} for the specified configuration.
* Get the {@link HazelcastInstance}.
* @param config the configuration
* @return the {@link HazelcastInstance}
*/
public HazelcastInstanceFactory(Config config) {
public static HazelcastInstance createHazelcastInstance(Config config) {
Assert.notNull(config, "Config must not be null");
this.config = config;
if (StringUtils.hasText(config.getInstanceName())) {
return Hazelcast.getOrCreateHazelcastInstance(config);
}
return Hazelcast.newHazelcastInstance(config);
}
private Config getConfig(Resource configLocation) throws IOException {
/**
* Get the {@link HazelcastInstance}.
* @param configLocation the location of the configuration file
* @return the {@link HazelcastInstance}
* @throws IOException if the configuration location could not be read
*/
public static HazelcastInstance createHazelcastInstance(Resource configLocation)
throws IOException {
Assert.notNull(configLocation, "ConfigLocation must not be null");
URL configUrl = configLocation.getURL();
Config config = new XmlConfigBuilder(configUrl).build();
if (ResourceUtils.isFileURL(configUrl)) {
@ -68,18 +72,34 @@ public class HazelcastInstanceFactory {
else {
config.setConfigurationUrl(configUrl);
}
return config;
return createHazelcastInstance(config);
}
/**
* Get the {@link HazelcastInstance}.
* @return the {@link HazelcastInstance}
* Get the client {@link HazelcastInstance}.
* @param config the client configuration
* @return the client {@link HazelcastInstance}
*/
public HazelcastInstance getHazelcastInstance() {
if (StringUtils.hasText(this.config.getInstanceName())) {
return Hazelcast.getOrCreateHazelcastInstance(this.config);
public static HazelcastInstance createHazelcastClient(ClientConfig config) {
Assert.notNull(config, "Config must not be null");
if (StringUtils.hasText(config.getInstanceName())) {
return HazelcastClient.getHazelcastClientByName(config.getInstanceName());
}
return Hazelcast.newHazelcastInstance(this.config);
return HazelcastClient.newHazelcastClient(config);
}
/**
* Get the client {@link HazelcastInstance}.
* @param configLocation the location of the client configuration file
* @return the client {@link HazelcastInstance}
* @throws IOException if the configuration location could not be read
*/
public static HazelcastInstance createHazelcastClient(Resource configLocation)
throws IOException {
Assert.notNull(configLocation, "ConfigLocation must not be null");
URL configUrl = configLocation.getURL();
ClientConfig config = new XmlClientConfigBuilder(configUrl).build();
return createHazelcastClient(config);
}
}

@ -0,0 +1,84 @@
/*
* 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.hazelcast;
import java.io.IOException;
import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
/**
* Configuration for Hazelcast server.
*
* @author Stephane Nicoll
* @author Vedran Pavic
* @since 2.0.0
*/
class HazelcastServerConfiguration {
static final String CONFIG_SYSTEM_PROPERTY = "hazelcast.config";
@Configuration
@ConditionalOnMissingBean(Config.class)
@Conditional(ConfigAvailableCondition.class)
static class HazelcastServerConfigFileConfiguration {
@Bean
public HazelcastInstance hazelcastInstance(HazelcastProperties properties)
throws IOException {
Resource config = properties.resolveConfigLocation();
if (config != null) {
return HazelcastInstanceFactory.createHazelcastInstance(config);
}
return Hazelcast.newHazelcastInstance();
}
}
@Configuration
@ConditionalOnSingleCandidate(Config.class)
static class HazelcastServerConfigConfiguration {
@Bean
public HazelcastInstance hazelcastInstance(Config config) {
return HazelcastInstanceFactory.createHazelcastInstance(config);
}
}
/**
* {@link HazelcastConfigResourceCondition} that checks if the
* {@code spring.hazelcast.config} configuration key is defined.
*/
static class ConfigAvailableCondition extends HazelcastConfigResourceCondition {
ConfigAvailableCondition() {
super(CONFIG_SYSTEM_PROPERTY, "file:./hazelcast.xml",
"classpath:/hazelcast.xml");
}
}
}

@ -49,12 +49,15 @@ import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.cache.support.MockCachingProvider;
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
import org.springframework.boot.junit.runner.classpath.ClassPathExclusions;
import org.springframework.boot.junit.runner.classpath.ModifiedClassPathRunner;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
@ -91,6 +94,8 @@ import static org.mockito.Mockito.verify;
* @author Stephane Nicoll
* @author Eddú Meléndez
*/
@RunWith(ModifiedClassPathRunner.class)
@ClassPathExclusions("hazelcast-client-*.jar")
public class CacheAutoConfigurationTests {
@Rule

@ -0,0 +1,155 @@
/*
* 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.hazelcast;
import java.io.IOException;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.impl.HazelcastClientProxy;
import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for client {@link HazelcastAutoConfiguration}.
*
* @author Vedran Pavic
*/
public class HazelcastAutoConfigurationClientTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
private AnnotationConfigApplicationContext context;
@After
public void closeContext() {
if (this.context != null) {
this.context.close();
}
}
private static HazelcastInstance hazelcastInstance;
@BeforeClass
public static void init() {
hazelcastInstance = Hazelcast.newHazelcastInstance();
}
@AfterClass
public static void close() {
if (hazelcastInstance != null) {
hazelcastInstance.shutdown();
}
}
@Test
public void systemProperty() throws IOException {
System.setProperty(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY,
"classpath:org/springframework/boot/autoconfigure/hazelcast/" +
"hazelcast-client-specific.xml");
try {
load();
HazelcastInstance hazelcastInstance = this.context
.getBean(HazelcastInstance.class);
assertThat(hazelcastInstance).isInstanceOf(HazelcastClientProxy.class);
assertThat(hazelcastInstance.getName()).startsWith("hz.client_");
}
finally {
System.clearProperty(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY);
}
}
@Test
public void explicitConfigFile() throws IOException {
load("spring.hazelcast.config=org/springframework/boot/autoconfigure/"
+ "hazelcast/hazelcast-client-specific.xml");
HazelcastInstance hazelcastInstance = this.context
.getBean(HazelcastInstance.class);
assertThat(hazelcastInstance).isInstanceOf(HazelcastClientProxy.class);
assertThat(hazelcastInstance.getName()).startsWith("hz.client_");
}
@Test
public void explicitConfigUrl() throws IOException {
load("spring.hazelcast.config=hazelcast-client-default.xml");
HazelcastInstance hazelcastInstance = this.context
.getBean(HazelcastInstance.class);
assertThat(hazelcastInstance).isInstanceOf(HazelcastClientProxy.class);
assertThat(hazelcastInstance.getName()).startsWith("hz.client_");
}
@Test
public void unknownConfigFile() {
this.thrown.expect(BeanCreationException.class);
this.thrown.expectMessage("foo/bar/unknown.xml");
load("spring.hazelcast.config=foo/bar/unknown.xml");
}
@Test
public void clientConfigHasPriority() {
load(HazelcastServerAndClientConfig.class, "spring.hazelcast.config=this-is-ignored.xml");
HazelcastInstance hazelcastInstance = this.context
.getBean(HazelcastInstance.class);
assertThat(hazelcastInstance).isInstanceOf(HazelcastClientProxy.class);
}
private void load(String... environment) {
load(null, environment);
}
private void load(Class<?> config, String... environment) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
if (config != null) {
applicationContext.register(config);
}
applicationContext.register(HazelcastAutoConfiguration.class);
applicationContext.refresh();
this.context = applicationContext;
}
@Configuration
static class HazelcastServerAndClientConfig {
@Bean
public Config config() {
return new Config();
}
@Bean
public ClientConfig clientConfig() {
return new ClientConfig();
}
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* 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.
@ -27,8 +27,11 @@ import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.junit.runner.classpath.ClassPathExclusions;
import org.springframework.boot.junit.runner.classpath.ModifiedClassPathRunner;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
@ -42,7 +45,9 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Stephane Nicoll
*/
public class HazelcastAutoConfigurationTests {
@RunWith(ModifiedClassPathRunner.class)
@ClassPathExclusions("hazelcast-client-*.jar")
public class HazelcastAutoConfigurationServerTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@ -67,7 +72,7 @@ public class HazelcastAutoConfigurationTests {
@Test
public void systemProperty() throws IOException {
System.setProperty(HazelcastConfigResourceCondition.CONFIG_SYSTEM_PROPERTY,
System.setProperty(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY,
"classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml");
try {
load();
@ -78,7 +83,7 @@ public class HazelcastAutoConfigurationTests {
assertThat(queueConfigs).hasSize(1).containsKey("foobar");
}
finally {
System.clearProperty(HazelcastConfigResourceCondition.CONFIG_SYSTEM_PROPERTY);
System.clearProperty(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY);
}
}

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<hazelcast-client xmlns="http://www.hazelcast.com/schema/client-config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hazelcast.com/schema/client-config hazelcast-client-config-3.8.xsd">
</hazelcast-client>
Loading…
Cancel
Save