Skip to content

Commit 3fbf1a2

Browse files
vpavicsnicoll
authored andcommitted
Add auto-configuration support for Hazelcast client
See gh-7469
1 parent ae3225e commit 3fbf1a2

File tree

10 files changed

+412
-81
lines changed

10 files changed

+412
-81
lines changed

spring-boot-autoconfigure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@
7575
<artifactId>hazelcast</artifactId>
7676
<optional>true</optional>
7777
</dependency>
78+
<dependency>
79+
<groupId>com.hazelcast</groupId>
80+
<artifactId>hazelcast-client</artifactId>
81+
<optional>true</optional>
82+
</dependency>
7883
<dependency>
7984
<groupId>com.hazelcast</groupId>
8085
<artifactId>hazelcast-spring</artifactId>

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfiguration.java

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,80 +16,43 @@
1616

1717
package org.springframework.boot.autoconfigure.hazelcast;
1818

19-
import java.io.IOException;
20-
21-
import com.hazelcast.config.Config;
22-
import com.hazelcast.core.Hazelcast;
19+
import com.hazelcast.client.HazelcastClient;
2320
import com.hazelcast.core.HazelcastInstance;
2421

2522
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2623
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2724
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
28-
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
2925
import org.springframework.boot.context.properties.EnableConfigurationProperties;
30-
import org.springframework.context.annotation.Bean;
31-
import org.springframework.context.annotation.Conditional;
3226
import org.springframework.context.annotation.Configuration;
33-
import org.springframework.core.io.Resource;
27+
import org.springframework.context.annotation.Import;
3428

3529
/**
3630
* {@link EnableAutoConfiguration Auto-configuration} for Hazelcast. Creates a
3731
* {@link HazelcastInstance} based on explicit configuration or when a default
3832
* configuration file is found in the environment.
3933
*
4034
* @author Stephane Nicoll
41-
* @author Madhura Bhave
35+
* @author Vedran Pavic
4236
* @since 1.3.0
4337
* @see HazelcastConfigResourceCondition
4438
*/
4539
@Configuration
4640
@ConditionalOnClass(HazelcastInstance.class)
47-
@ConditionalOnMissingBean(HazelcastInstance.class)
4841
@EnableConfigurationProperties(HazelcastProperties.class)
4942
public class HazelcastAutoConfiguration {
5043

5144
@Configuration
52-
@ConditionalOnMissingBean(Config.class)
53-
@Conditional(ConfigAvailableCondition.class)
54-
static class HazelcastConfigFileConfiguration {
55-
56-
private final HazelcastProperties hazelcastProperties;
57-
58-
HazelcastConfigFileConfiguration(HazelcastProperties hazelcastProperties) {
59-
this.hazelcastProperties = hazelcastProperties;
60-
}
61-
62-
@Bean
63-
public HazelcastInstance hazelcastInstance() throws IOException {
64-
Resource config = this.hazelcastProperties.resolveConfigLocation();
65-
if (config != null) {
66-
return new HazelcastInstanceFactory(config).getHazelcastInstance();
67-
}
68-
return Hazelcast.newHazelcastInstance();
69-
}
45+
@ConditionalOnMissingBean(HazelcastInstance.class)
46+
@Import(HazelcastServerConfiguration.class)
47+
static class ServerConfiguration {
7048

7149
}
7250

7351
@Configuration
74-
@ConditionalOnSingleCandidate(Config.class)
75-
static class HazelcastConfigConfiguration {
76-
77-
@Bean
78-
public HazelcastInstance hazelcastInstance(Config config) {
79-
return new HazelcastInstanceFactory(config).getHazelcastInstance();
80-
}
81-
82-
}
83-
84-
/**
85-
* {@link HazelcastConfigResourceCondition} that checks if the
86-
* {@code spring.hazelcast.config} configuration key is defined.
87-
*/
88-
static class ConfigAvailableCondition extends HazelcastConfigResourceCondition {
89-
90-
ConfigAvailableCondition() {
91-
super("spring.hazelcast.config");
92-
}
52+
@ConditionalOnClass(HazelcastClient.class)
53+
@ConditionalOnMissingBean(HazelcastInstance.class)
54+
@Import(HazelcastClientConfiguration.class)
55+
static class ClientConfiguration {
9356

9457
}
9558

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.hazelcast;
18+
19+
import java.io.IOException;
20+
21+
import com.hazelcast.client.HazelcastClient;
22+
import com.hazelcast.client.config.ClientConfig;
23+
import com.hazelcast.core.HazelcastInstance;
24+
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Conditional;
29+
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.core.io.Resource;
31+
32+
/**
33+
* Configuration for Hazelcast client.
34+
*
35+
* @author Vedran Pavic
36+
* @since 2.0.0
37+
*/
38+
class HazelcastClientConfiguration {
39+
40+
static final String CONFIG_SYSTEM_PROPERTY = "hazelcast.client.config";
41+
42+
@Configuration
43+
@ConditionalOnMissingBean(ClientConfig.class)
44+
@Conditional(ConfigAvailableCondition.class)
45+
static class HazelcastClientConfigFileConfiguration {
46+
47+
@Bean
48+
public HazelcastInstance hazelcastInstance(HazelcastProperties properties)
49+
throws IOException {
50+
Resource config = properties.resolveConfigLocation();
51+
if (config != null) {
52+
return HazelcastInstanceFactory.createHazelcastClient(config);
53+
}
54+
return HazelcastClient.newHazelcastClient();
55+
}
56+
57+
}
58+
59+
@Configuration
60+
@ConditionalOnSingleCandidate(ClientConfig.class)
61+
static class HazelcastClientConfigConfiguration {
62+
63+
@Bean
64+
public HazelcastInstance hazelcastInstance(ClientConfig config) {
65+
return HazelcastInstanceFactory.createHazelcastClient(config);
66+
}
67+
68+
}
69+
70+
/**
71+
* {@link HazelcastConfigResourceCondition} that checks if the
72+
* {@code spring.hazelcast.config} configuration key is defined.
73+
*/
74+
static class ConfigAvailableCondition extends HazelcastConfigResourceCondition {
75+
76+
ConfigAvailableCondition() {
77+
super(CONFIG_SYSTEM_PROPERTY, "file:./hazelcast-client.xml",
78+
"classpath:/hazelcast-client.xml");
79+
}
80+
81+
}
82+
83+
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastConfigResourceCondition.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
2222
import org.springframework.context.annotation.ConditionContext;
2323
import org.springframework.core.type.AnnotatedTypeMetadata;
24+
import org.springframework.util.Assert;
2425

2526
/**
2627
* {@link SpringBootCondition} used to check if the Hazelcast configuration is available.
@@ -29,22 +30,26 @@
2930
*
3031
* @author Stephane Nicoll
3132
* @author Madhura Bhave
33+
* @author Vedran Pavic
3234
* @since 1.3.0
3335
*/
3436
public abstract class HazelcastConfigResourceCondition extends ResourceCondition {
3537

36-
static final String CONFIG_SYSTEM_PROPERTY = "hazelcast.config";
38+
private final String configSystemProperty;
3739

38-
protected HazelcastConfigResourceCondition(String property) {
39-
super("Hazelcast", property, "file:./hazelcast.xml", "classpath:/hazelcast.xml");
40+
protected HazelcastConfigResourceCondition(String configSystemProperty,
41+
String... resourceLocations) {
42+
super("Hazelcast", "spring.hazelcast.config", resourceLocations);
43+
Assert.notNull(configSystemProperty, "ConfigSystemProperty must not be null");
44+
this.configSystemProperty = configSystemProperty;
4045
}
4146

4247
@Override
4348
protected ConditionOutcome getResourceOutcome(ConditionContext context,
4449
AnnotatedTypeMetadata metadata) {
45-
if (System.getProperty(CONFIG_SYSTEM_PROPERTY) != null) {
50+
if (System.getProperty(this.configSystemProperty) != null) {
4651
return ConditionOutcome.match(startConditionMessage()
47-
.because("System property '" + CONFIG_SYSTEM_PROPERTY + "' is set."));
52+
.because("System property '" + this.configSystemProperty + "' is set."));
4853
}
4954
return super.getResourceOutcome(context, metadata);
5055
}
Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,9 @@
1919
import java.io.IOException;
2020
import java.net.URL;
2121

22+
import com.hazelcast.client.HazelcastClient;
23+
import com.hazelcast.client.config.ClientConfig;
24+
import com.hazelcast.client.config.XmlClientConfigBuilder;
2225
import com.hazelcast.config.Config;
2326
import com.hazelcast.config.XmlConfigBuilder;
2427
import com.hazelcast.core.Hazelcast;
@@ -34,32 +37,33 @@
3437
*
3538
* @author Stephane Nicoll
3639
* @author Phillip Webb
40+
* @author Vedran Pavic
3741
* @since 1.3.0
3842
*/
39-
public class HazelcastInstanceFactory {
40-
41-
private Config config;
42-
43-
/**
44-
* Create a {@link HazelcastInstanceFactory} for the specified configuration location.
45-
* @param configLocation the location of the configuration file
46-
* @throws IOException if the configuration location could not be read
47-
*/
48-
public HazelcastInstanceFactory(Resource configLocation) throws IOException {
49-
Assert.notNull(configLocation, "ConfigLocation must not be null");
50-
this.config = getConfig(configLocation);
51-
}
43+
public abstract class HazelcastInstanceFactory {
5244

5345
/**
54-
* Create a {@link HazelcastInstanceFactory} for the specified configuration.
46+
* Get the {@link HazelcastInstance}.
5547
* @param config the configuration
48+
* @return the {@link HazelcastInstance}
5649
*/
57-
public HazelcastInstanceFactory(Config config) {
50+
public static HazelcastInstance createHazelcastInstance(Config config) {
5851
Assert.notNull(config, "Config must not be null");
59-
this.config = config;
52+
if (StringUtils.hasText(config.getInstanceName())) {
53+
return Hazelcast.getOrCreateHazelcastInstance(config);
54+
}
55+
return Hazelcast.newHazelcastInstance(config);
6056
}
6157

62-
private Config getConfig(Resource configLocation) throws IOException {
58+
/**
59+
* Get the {@link HazelcastInstance}.
60+
* @param configLocation the location of the configuration file
61+
* @return the {@link HazelcastInstance}
62+
* @throws IOException if the configuration location could not be read
63+
*/
64+
public static HazelcastInstance createHazelcastInstance(Resource configLocation)
65+
throws IOException {
66+
Assert.notNull(configLocation, "ConfigLocation must not be null");
6367
URL configUrl = configLocation.getURL();
6468
Config config = new XmlConfigBuilder(configUrl).build();
6569
if (ResourceUtils.isFileURL(configUrl)) {
@@ -68,18 +72,34 @@ private Config getConfig(Resource configLocation) throws IOException {
6872
else {
6973
config.setConfigurationUrl(configUrl);
7074
}
71-
return config;
75+
return createHazelcastInstance(config);
7276
}
7377

7478
/**
75-
* Get the {@link HazelcastInstance}.
76-
* @return the {@link HazelcastInstance}
79+
* Get the client {@link HazelcastInstance}.
80+
* @param config the client configuration
81+
* @return the client {@link HazelcastInstance}
7782
*/
78-
public HazelcastInstance getHazelcastInstance() {
79-
if (StringUtils.hasText(this.config.getInstanceName())) {
80-
return Hazelcast.getOrCreateHazelcastInstance(this.config);
83+
public static HazelcastInstance createHazelcastClient(ClientConfig config) {
84+
Assert.notNull(config, "Config must not be null");
85+
if (StringUtils.hasText(config.getInstanceName())) {
86+
return HazelcastClient.getHazelcastClientByName(config.getInstanceName());
8187
}
82-
return Hazelcast.newHazelcastInstance(this.config);
88+
return HazelcastClient.newHazelcastClient(config);
89+
}
90+
91+
/**
92+
* Get the client {@link HazelcastInstance}.
93+
* @param configLocation the location of the client configuration file
94+
* @return the client {@link HazelcastInstance}
95+
* @throws IOException if the configuration location could not be read
96+
*/
97+
public static HazelcastInstance createHazelcastClient(Resource configLocation)
98+
throws IOException {
99+
Assert.notNull(configLocation, "ConfigLocation must not be null");
100+
URL configUrl = configLocation.getURL();
101+
ClientConfig config = new XmlClientConfigBuilder(configUrl).build();
102+
return createHazelcastClient(config);
83103
}
84104

85105
}

0 commit comments

Comments
 (0)