Merge pull request #20109 from nosan

* pr/20109:
  Polish "Create HazelCastClient if necessary"
  Create HazelCastClient if necessary

Closes gh-20109
This commit is contained in:
Stephane Nicoll
2020-02-10 11:48:12 +01:00
3 changed files with 22 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -72,7 +72,7 @@ public class HazelcastClientFactory {
*/
public HazelcastInstance getHazelcastInstance() {
if (StringUtils.hasText(this.clientConfig.getInstanceName())) {
return HazelcastClient.getHazelcastClientByName(this.clientConfig.getInstanceName());
return HazelcastClient.getOrCreateHazelcastClient(this.clientConfig);
}
return HazelcastClient.newHazelcastClient(this.clientConfig);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.hazelcast;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.impl.clientside.HazelcastClientProxy;
import com.hazelcast.config.Config;
@@ -124,6 +125,16 @@ class HazelcastAutoConfigurationClientTests {
.getBean(HazelcastInstance.class).isInstanceOf(HazelcastClientProxy.class));
}
@Test
void clientConfigWithInstanceNameCreatesClientIfNecessary() {
assertThat(HazelcastClient.getHazelcastClientByName("spring-boot")).isNull();
this.contextRunner
.withPropertyValues("spring.hazelcast.config=classpath:org/springframework/"
+ "boot/autoconfigure/hazelcast/hazelcast-client-instance.xml")
.run((context) -> assertThat(context).getBean(HazelcastInstance.class)
.extracting(HazelcastInstance::getName).isEqualTo("spring-boot"));
}
private ContextConsumer<AssertableApplicationContext> assertSpecificHazelcastClient(String label) {
return (context) -> assertThat(context).getBean(HazelcastInstance.class).isInstanceOf(HazelcastInstance.class)
.has(labelEqualTo(label));

View File

@@ -0,0 +1,8 @@
<?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.12.xsd">
<instance-name>spring-boot</instance-name>
</hazelcast-client>