Skip to content

Commit 14e5445

Browse files
committed
Merge pull request #20109 from nosan
* pr/20109: Polish "Create HazelCastClient if necessary" Create HazelCastClient if necessary Closes gh-20109
2 parents e8b97db + 77bdf99 commit 14e5445

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -72,7 +72,7 @@ private ClientConfig getClientConfig(Resource clientConfigLocation) throws IOExc
7272
*/
7373
public HazelcastInstance getHazelcastInstance() {
7474
if (StringUtils.hasText(this.clientConfig.getInstanceName())) {
75-
return HazelcastClient.getHazelcastClientByName(this.clientConfig.getInstanceName());
75+
return HazelcastClient.getOrCreateHazelcastClient(this.clientConfig);
7676
}
7777
return HazelcastClient.newHazelcastClient(this.clientConfig);
7878
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -16,6 +16,7 @@
1616

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

19+
import com.hazelcast.client.HazelcastClient;
1920
import com.hazelcast.client.config.ClientConfig;
2021
import com.hazelcast.client.impl.clientside.HazelcastClientProxy;
2122
import com.hazelcast.config.Config;
@@ -124,6 +125,16 @@ void clientConfigTakesPrecedence() {
124125
.getBean(HazelcastInstance.class).isInstanceOf(HazelcastClientProxy.class));
125126
}
126127

128+
@Test
129+
void clientConfigWithInstanceNameCreatesClientIfNecessary() {
130+
assertThat(HazelcastClient.getHazelcastClientByName("spring-boot")).isNull();
131+
this.contextRunner
132+
.withPropertyValues("spring.hazelcast.config=classpath:org/springframework/"
133+
+ "boot/autoconfigure/hazelcast/hazelcast-client-instance.xml")
134+
.run((context) -> assertThat(context).getBean(HazelcastInstance.class)
135+
.extracting(HazelcastInstance::getName).isEqualTo("spring-boot"));
136+
}
137+
127138
private ContextConsumer<AssertableApplicationContext> assertSpecificHazelcastClient(String label) {
128139
return (context) -> assertThat(context).getBean(HazelcastInstance.class).isInstanceOf(HazelcastInstance.class)
129140
.has(labelEqualTo(label));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<hazelcast-client xmlns="http://www.hazelcast.com/schema/client-config"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.hazelcast.com/schema/client-config hazelcast-client-config-3.12.xsd">
5+
6+
<instance-name>spring-boot</instance-name>
7+
8+
</hazelcast-client>

0 commit comments

Comments
 (0)