Commit 14e54451 authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #20109 from nosan

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

Closes gh-20109
parents e8b97dbc 77bdf992
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -72,7 +72,7 @@ public class HazelcastClientFactory { ...@@ -72,7 +72,7 @@ public class HazelcastClientFactory {
*/ */
public HazelcastInstance getHazelcastInstance() { public HazelcastInstance getHazelcastInstance() {
if (StringUtils.hasText(this.clientConfig.getInstanceName())) { if (StringUtils.hasText(this.clientConfig.getInstanceName())) {
return HazelcastClient.getHazelcastClientByName(this.clientConfig.getInstanceName()); return HazelcastClient.getOrCreateHazelcastClient(this.clientConfig);
} }
return HazelcastClient.newHazelcastClient(this.clientConfig); return HazelcastClient.newHazelcastClient(this.clientConfig);
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.hazelcast; package org.springframework.boot.autoconfigure.hazelcast;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig; import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.impl.clientside.HazelcastClientProxy; import com.hazelcast.client.impl.clientside.HazelcastClientProxy;
import com.hazelcast.config.Config; import com.hazelcast.config.Config;
...@@ -124,6 +125,16 @@ class HazelcastAutoConfigurationClientTests { ...@@ -124,6 +125,16 @@ class HazelcastAutoConfigurationClientTests {
.getBean(HazelcastInstance.class).isInstanceOf(HazelcastClientProxy.class)); .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) { private ContextConsumer<AssertableApplicationContext> assertSpecificHazelcastClient(String label) {
return (context) -> assertThat(context).getBean(HazelcastInstance.class).isInstanceOf(HazelcastInstance.class) return (context) -> assertThat(context).getBean(HazelcastInstance.class).isInstanceOf(HazelcastInstance.class)
.has(labelEqualTo(label)); .has(labelEqualTo(label));
......
<?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>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment