Skip to content

Commit 0aded58

Browse files
committed
Polish "Add auto-configuration support for Hazelcast client"
Closes gh-7469
1 parent 3fbf1a2 commit 0aded58

File tree

9 files changed

+204
-84
lines changed

9 files changed

+204
-84
lines changed

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616

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

19-
import com.hazelcast.client.HazelcastClient;
2019
import com.hazelcast.core.HazelcastInstance;
2120

2221
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2322
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
24-
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2523
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2624
import org.springframework.context.annotation.Configuration;
2725
import org.springframework.context.annotation.Import;
@@ -39,21 +37,7 @@
3937
@Configuration
4038
@ConditionalOnClass(HazelcastInstance.class)
4139
@EnableConfigurationProperties(HazelcastProperties.class)
40+
@Import({ HazelcastClientConfiguration.class, HazelcastServerConfiguration.class })
4241
public class HazelcastAutoConfiguration {
4342

44-
@Configuration
45-
@ConditionalOnMissingBean(HazelcastInstance.class)
46-
@Import(HazelcastServerConfiguration.class)
47-
static class ServerConfiguration {
48-
49-
}
50-
51-
@Configuration
52-
@ConditionalOnClass(HazelcastClient.class)
53-
@ConditionalOnMissingBean(HazelcastInstance.class)
54-
@Import(HazelcastClientConfiguration.class)
55-
static class ClientConfiguration {
56-
57-
}
58-
5943
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.hazelcast.client.config.ClientConfig;
2323
import com.hazelcast.core.HazelcastInstance;
2424

25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2526
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2627
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
2728
import org.springframework.context.annotation.Bean;
@@ -33,8 +34,10 @@
3334
* Configuration for Hazelcast client.
3435
*
3536
* @author Vedran Pavic
36-
* @since 2.0.0
37+
* @author Stephane Nicoll
3738
*/
39+
@ConditionalOnClass(HazelcastClient.class)
40+
@ConditionalOnMissingBean(HazelcastInstance.class)
3841
class HazelcastClientConfiguration {
3942

4043
static final String CONFIG_SYSTEM_PROPERTY = "hazelcast.client.config";
@@ -49,7 +52,7 @@ public HazelcastInstance hazelcastInstance(HazelcastProperties properties)
4952
throws IOException {
5053
Resource config = properties.resolveConfigLocation();
5154
if (config != null) {
52-
return HazelcastInstanceFactory.createHazelcastClient(config);
55+
return new HazelcastClientFactory(config).getHazelcastInstance();
5356
}
5457
return HazelcastClient.newHazelcastClient();
5558
}
@@ -62,7 +65,7 @@ static class HazelcastClientConfigConfiguration {
6265

6366
@Bean
6467
public HazelcastInstance hazelcastInstance(ClientConfig config) {
65-
return HazelcastInstanceFactory.createHazelcastClient(config);
68+
return new HazelcastClientFactory(config).getHazelcastInstance();
6669
}
6770

6871
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
import java.net.URL;
21+
22+
import com.hazelcast.client.HazelcastClient;
23+
import com.hazelcast.client.config.ClientConfig;
24+
import com.hazelcast.client.config.XmlClientConfigBuilder;
25+
import com.hazelcast.core.HazelcastInstance;
26+
27+
import org.springframework.core.io.Resource;
28+
import org.springframework.util.Assert;
29+
import org.springframework.util.StringUtils;
30+
31+
/**
32+
* Factory that can be used to create a client {@link HazelcastInstance}.
33+
*
34+
* @author Vedran Pavic
35+
* @since 2.0.0
36+
*/
37+
public class HazelcastClientFactory {
38+
39+
private final ClientConfig clientConfig;
40+
41+
/**
42+
* Create a {@link HazelcastClientFactory} for the specified configuration location.
43+
* @param clientConfigLocation the location of the configuration file
44+
* @throws IOException if the configuration location could not be read
45+
*/
46+
public HazelcastClientFactory(Resource clientConfigLocation) throws IOException {
47+
this.clientConfig = getClientConfig(clientConfigLocation);
48+
}
49+
50+
/**
51+
* Create a {@link HazelcastClientFactory} for the specified configuration.
52+
* @param clientConfig the configuration
53+
*/
54+
public HazelcastClientFactory(ClientConfig clientConfig) {
55+
Assert.notNull(clientConfig, "ClientConfig must not be null");
56+
this.clientConfig = clientConfig;
57+
}
58+
59+
60+
private ClientConfig getClientConfig(Resource clientConfigLocation)
61+
throws IOException {
62+
URL configUrl = clientConfigLocation.getURL();
63+
return new XmlClientConfigBuilder(configUrl).build();
64+
}
65+
66+
/**
67+
* Get the {@link HazelcastInstance}.
68+
* @return the {@link HazelcastInstance}
69+
*/
70+
public HazelcastInstance getHazelcastInstance() {
71+
if (StringUtils.hasText(this.clientConfig.getInstanceName())) {
72+
return HazelcastClient.getHazelcastClientByName(
73+
this.clientConfig.getInstanceName());
74+
}
75+
return HazelcastClient.newHazelcastClient(this.clientConfig);
76+
}
77+
78+
}

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

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
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;
2522
import com.hazelcast.config.Config;
2623
import com.hazelcast.config.XmlConfigBuilder;
2724
import com.hazelcast.core.Hazelcast;
@@ -37,33 +34,32 @@
3734
*
3835
* @author Stephane Nicoll
3936
* @author Phillip Webb
40-
* @author Vedran Pavic
4137
* @since 1.3.0
4238
*/
43-
public abstract class HazelcastInstanceFactory {
39+
public class HazelcastInstanceFactory {
4440

45-
/**
46-
* Get the {@link HazelcastInstance}.
47-
* @param config the configuration
48-
* @return the {@link HazelcastInstance}
49-
*/
50-
public static HazelcastInstance createHazelcastInstance(Config config) {
51-
Assert.notNull(config, "Config must not be null");
52-
if (StringUtils.hasText(config.getInstanceName())) {
53-
return Hazelcast.getOrCreateHazelcastInstance(config);
54-
}
55-
return Hazelcast.newHazelcastInstance(config);
56-
}
41+
private final Config config;
5742

5843
/**
59-
* Get the {@link HazelcastInstance}.
44+
* Create a {@link HazelcastInstanceFactory} for the specified configuration location.
6045
* @param configLocation the location of the configuration file
61-
* @return the {@link HazelcastInstance}
6246
* @throws IOException if the configuration location could not be read
6347
*/
64-
public static HazelcastInstance createHazelcastInstance(Resource configLocation)
65-
throws IOException {
48+
public HazelcastInstanceFactory(Resource configLocation) throws IOException {
6649
Assert.notNull(configLocation, "ConfigLocation must not be null");
50+
this.config = getConfig(configLocation);
51+
}
52+
53+
/**
54+
* Create a {@link HazelcastInstanceFactory} for the specified configuration.
55+
* @param config the configuration
56+
*/
57+
public HazelcastInstanceFactory(Config config) {
58+
Assert.notNull(config, "Config must not be null");
59+
this.config = config;
60+
}
61+
62+
private Config getConfig(Resource configLocation) throws IOException {
6763
URL configUrl = configLocation.getURL();
6864
Config config = new XmlConfigBuilder(configUrl).build();
6965
if (ResourceUtils.isFileURL(configUrl)) {
@@ -72,34 +68,18 @@ public static HazelcastInstance createHazelcastInstance(Resource configLocation)
7268
else {
7369
config.setConfigurationUrl(configUrl);
7470
}
75-
return createHazelcastInstance(config);
71+
return config;
7672
}
7773

7874
/**
79-
* Get the client {@link HazelcastInstance}.
80-
* @param config the client configuration
81-
* @return the client {@link HazelcastInstance}
75+
* Get the {@link HazelcastInstance}.
76+
* @return the {@link HazelcastInstance}
8277
*/
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());
78+
public HazelcastInstance getHazelcastInstance() {
79+
if (StringUtils.hasText(this.config.getInstanceName())) {
80+
return Hazelcast.getOrCreateHazelcastInstance(this.config);
8781
}
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);
82+
return Hazelcast.newHazelcastInstance(this.config);
10383
}
10484

10585
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
*
3535
* @author Stephane Nicoll
3636
* @author Vedran Pavic
37-
* @since 2.0.0
3837
*/
38+
@ConditionalOnMissingBean(HazelcastInstance.class)
3939
class HazelcastServerConfiguration {
4040

4141
static final String CONFIG_SYSTEM_PROPERTY = "hazelcast.config";
@@ -50,7 +50,7 @@ public HazelcastInstance hazelcastInstance(HazelcastProperties properties)
5050
throws IOException {
5151
Resource config = properties.resolveConfigLocation();
5252
if (config != null) {
53-
return HazelcastInstanceFactory.createHazelcastInstance(config);
53+
return new HazelcastInstanceFactory(config).getHazelcastInstance();
5454
}
5555
return Hazelcast.newHazelcastInstance();
5656
}
@@ -63,7 +63,7 @@ static class HazelcastServerConfigConfiguration {
6363

6464
@Bean
6565
public HazelcastInstance hazelcastInstance(Config config) {
66-
return HazelcastInstanceFactory.createHazelcastInstance(config);
66+
return new HazelcastInstanceFactory(config).getHazelcastInstance();
6767
}
6868

6969
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@
3939
import static org.assertj.core.api.Assertions.assertThat;
4040

4141
/**
42-
* Tests for client {@link HazelcastAutoConfiguration}.
42+
* Tests for {@link HazelcastAutoConfiguration} specific to the client.
4343
*
4444
* @author Vedran Pavic
45+
* @author Stephane Nicoll
4546
*/
4647
public class HazelcastAutoConfigurationClientTests {
4748

@@ -50,24 +51,27 @@ public class HazelcastAutoConfigurationClientTests {
5051

5152
private AnnotationConfigApplicationContext context;
5253

53-
@After
54-
public void closeContext() {
55-
if (this.context != null) {
56-
this.context.close();
57-
}
58-
}
59-
60-
private static HazelcastInstance hazelcastInstance;
54+
/**
55+
* Servers the test clients will connect to.
56+
*/
57+
private static HazelcastInstance hazelcastServer;
6158

6259
@BeforeClass
6360
public static void init() {
64-
hazelcastInstance = Hazelcast.newHazelcastInstance();
61+
hazelcastServer = Hazelcast.newHazelcastInstance();
6562
}
6663

6764
@AfterClass
6865
public static void close() {
69-
if (hazelcastInstance != null) {
70-
hazelcastInstance.shutdown();
66+
if (hazelcastServer != null) {
67+
hazelcastServer.shutdown();
68+
}
69+
}
70+
71+
@After
72+
public void closeContext() {
73+
if (this.context != null) {
74+
this.context.close();
7175
}
7276
}
7377

@@ -115,7 +119,7 @@ public void unknownConfigFile() {
115119
}
116120

117121
@Test
118-
public void clientConfigHasPriority() {
122+
public void clientConfigTakesPrecedence() {
119123
load(HazelcastServerAndClientConfig.class, "spring.hazelcast.config=this-is-ignored.xml");
120124
HazelcastInstance hazelcastInstance = this.context
121125
.getBean(HazelcastInstance.class);

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import static org.assertj.core.api.Assertions.assertThat;
4242

4343
/**
44-
* Tests for {@link HazelcastAutoConfiguration}.
44+
* Tests for {@link HazelcastAutoConfiguration} when the client library is not present.
4545
*
4646
* @author Stephane Nicoll
4747
*/

0 commit comments

Comments
 (0)