From 67dd9ad537ccc4ddce061f994541ff3923591c1e Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Tue, 4 Feb 2020 21:45:48 +0200 Subject: [PATCH 1/2] Create HazelCastClient if necessary This commit makes sure to create a HazelcastClient if an instance name is provided in configuration and if no such client already exists. This harmonizes the behaviour with of the server counter-part. See gh-20109 --- .../hazelcast/HazelcastClientFactory.java | 4 ++-- .../HazelcastAutoConfigurationClientTests.java | 11 ++++++++++- .../hazelcast/hazelcast-client-instance.xml | 7 +++++++ 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-instance.xml diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientFactory.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientFactory.java index 7bf5e0d305..b1c9753514 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientFactory.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientFactory.java @@ -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); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java index 87d15d0a02..f63dce65fb 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java @@ -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. @@ -124,6 +124,15 @@ class HazelcastAutoConfigurationClientTests { .getBean(HazelcastInstance.class).isInstanceOf(HazelcastClientProxy.class)); } + @Test + void clientConfigWithInstanceName() { + 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 assertSpecificHazelcastClient(String label) { return (context) -> assertThat(context).getBean(HazelcastInstance.class).isInstanceOf(HazelcastInstance.class) .has(labelEqualTo(label)); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-instance.xml b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-instance.xml new file mode 100644 index 0000000000..29bfac076a --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-instance.xml @@ -0,0 +1,7 @@ + + + + spring-boot + From 77bdf992ec3b9dd2ed12e54ed1c9898fac0b686d Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 10 Feb 2020 11:41:10 +0100 Subject: [PATCH 2/2] Polish "Create HazelCastClient if necessary" See gh-20109 --- .../hazelcast/HazelcastAutoConfigurationClientTests.java | 4 +++- .../autoconfigure/hazelcast/hazelcast-client-instance.xml | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java index f63dce65fb..2369424d77 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java @@ -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; @@ -125,7 +126,8 @@ class HazelcastAutoConfigurationClientTests { } @Test - void clientConfigWithInstanceName() { + void clientConfigWithInstanceNameCreatesClientIfNecessary() { + assertThat(HazelcastClient.getHazelcastClientByName("spring-boot")).isNull(); this.contextRunner .withPropertyValues("spring.hazelcast.config=classpath:org/springframework/" + "boot/autoconfigure/hazelcast/hazelcast-client-instance.xml") diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-instance.xml b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-instance.xml index 29bfac076a..0dbc09b64f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-instance.xml +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-instance.xml @@ -4,4 +4,5 @@ xsi:schemaLocation="http://www.hazelcast.com/schema/client-config hazelcast-client-config-3.12.xsd"> spring-boot +