Add support for Hazelcast
This commit upgrades to Hazelcast 4.0.3, yet keeping compatibility with Hazelcast 3.x. Closes gh-20856 Closes gh-23475pull/23502/head
parent
7b183ef99d
commit
4009acf025
@ -1,8 +1,8 @@
|
||||
<hazelcast xmlns="http://www.hazelcast.com/schema/config"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.hazelcast.com/schema/config
|
||||
http://www.hazelcast.com/schema/config/hazelcast-config-4.0.xsd">
|
||||
<instance-name>actuator-hazelcast-4</instance-name>
|
||||
http://www.hazelcast.com/schema/config/hazelcast-config-3.12.xsd">
|
||||
<instance-name>actuator-hazelcast-3</instance-name>
|
||||
<map name="defaultCache"/>
|
||||
<network>
|
||||
<join>
|
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.hazelcast;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
|
||||
import com.hazelcast.client.config.ClientConfigRecognizer;
|
||||
import com.hazelcast.config.ConfigStream;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionMessage.Builder;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
/**
|
||||
* {@link HazelcastConfigResourceCondition} that checks if the
|
||||
* {@code spring.hazelcast.config} configuration key is defined.
|
||||
*/
|
||||
class HazelcastClientConfigAvailableCondition extends HazelcastConfigResourceCondition {
|
||||
|
||||
HazelcastClientConfigAvailableCondition() {
|
||||
super(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY, "file:./hazelcast-client.xml",
|
||||
"classpath:/hazelcast-client.xml", "file:./hazelcast-client.yaml", "classpath:/hazelcast-client.yaml");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
if (context.getEnvironment().containsProperty(HAZELCAST_CONFIG_PROPERTY)) {
|
||||
ConditionOutcome configValidationOutcome = Hazelcast4ClientValidation.clientConfigOutcome(context,
|
||||
HAZELCAST_CONFIG_PROPERTY, startConditionMessage());
|
||||
return (configValidationOutcome != null) ? configValidationOutcome : ConditionOutcome
|
||||
.match(startConditionMessage().foundExactly("property " + HAZELCAST_CONFIG_PROPERTY));
|
||||
}
|
||||
return getResourceOutcome(context, metadata);
|
||||
}
|
||||
|
||||
static class Hazelcast4ClientValidation {
|
||||
|
||||
static ConditionOutcome clientConfigOutcome(ConditionContext context, String propertyName, Builder builder) {
|
||||
String resourcePath = context.getEnvironment().getProperty(propertyName);
|
||||
Resource resource = context.getResourceLoader().getResource(resourcePath);
|
||||
if (!resource.exists()) {
|
||||
return ConditionOutcome.noMatch(builder.because("Hazelcast configuration does not exist"));
|
||||
}
|
||||
try (InputStream in = resource.getInputStream()) {
|
||||
boolean clientConfig = new ClientConfigRecognizer().isRecognized(new ConfigStream(in));
|
||||
return new ConditionOutcome(clientConfig, existingConfigurationOutcome(resource, clientConfig));
|
||||
}
|
||||
catch (Throwable ex) { // Hazelcast 4 specific API
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static String existingConfigurationOutcome(Resource resource, boolean client) throws IOException {
|
||||
URL location = resource.getURL();
|
||||
return client ? "Hazelcast client configuration detected at '" + location + "'"
|
||||
: "Hazelcast server configuration detected at '" + location + "'";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.hazelcast;
|
||||
|
||||
import com.hazelcast.client.impl.clientside.HazelcastClientProxy;
|
||||
import com.hazelcast.config.Config;
|
||||
import com.hazelcast.core.Hazelcast;
|
||||
import com.hazelcast.core.HazelcastInstance;
|
||||
import org.assertj.core.api.Condition;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.test.context.runner.ContextConsumer;
|
||||
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
|
||||
import org.springframework.boot.testsupport.classpath.ClassPathOverrides;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link HazelcastAutoConfiguration} with Hazelcast 3.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@ClassPathExclusions("hazelcast*.jar")
|
||||
@ClassPathOverrides({ "com.hazelcast:hazelcast:3.12.8", "com.hazelcast:hazelcast-client:3.12.8" })
|
||||
class Hazelcast3AutoConfigurationTests {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
void defaultConfigFile() {
|
||||
// no hazelcast-client.xml and hazelcast.xml is present in root classpath
|
||||
// this also asserts that XML has priority over YAML
|
||||
// as both hazelcast.yaml and hazelcast.xml in test classpath.
|
||||
this.contextRunner.run((context) -> {
|
||||
Config config = context.getBean(HazelcastInstance.class).getConfig();
|
||||
assertThat(config.getConfigurationUrl()).isEqualTo(new ClassPathResource("hazelcast.xml").getURL());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitConfigFileWithXml() {
|
||||
HazelcastInstance hazelcastServer = Hazelcast.newHazelcastInstance();
|
||||
try {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.hazelcast.config=org/springframework/boot/autoconfigure/"
|
||||
+ "hazelcast/hazelcast-client-specific.xml")
|
||||
.run(assertSpecificHazelcastClient("explicit-xml"));
|
||||
}
|
||||
finally {
|
||||
hazelcastServer.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
private ContextConsumer<AssertableApplicationContext> assertSpecificHazelcastClient(String label) {
|
||||
return (context) -> assertThat(context).getBean(HazelcastInstance.class).isInstanceOf(HazelcastInstance.class)
|
||||
.has(labelEqualTo(label));
|
||||
}
|
||||
|
||||
private static Condition<HazelcastInstance> labelEqualTo(String label) {
|
||||
return new Condition<>((o) -> ((HazelcastClientProxy) o).getClientConfig().getLabels().stream()
|
||||
.anyMatch((e) -> e.equals(label)), "Label equals to " + label);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.hazelcast;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link HazelcastClientConfigAvailableCondition}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class HazelcastClientConfigAvailableConditionTests {
|
||||
|
||||
private final HazelcastClientConfigAvailableCondition condition = new HazelcastClientConfigAvailableCondition();
|
||||
|
||||
@Test
|
||||
void explicitConfigurationWithClientConfigMatches() {
|
||||
ConditionOutcome outcome = getMatchOutcome(new MockEnvironment().withProperty("spring.hazelcast.config",
|
||||
"classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-specific.xml"));
|
||||
assertThat(outcome.isMatch()).isTrue();
|
||||
assertThat(outcome.getMessage()).contains("Hazelcast client configuration detected");
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitConfigurationWithServerConfigDoesNotMatch() {
|
||||
ConditionOutcome outcome = getMatchOutcome(new MockEnvironment().withProperty("spring.hazelcast.config",
|
||||
"classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"));
|
||||
assertThat(outcome.isMatch()).isFalse();
|
||||
assertThat(outcome.getMessage()).contains("Hazelcast server configuration detected");
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitConfigurationWithMissingConfigDoesNotMatch() {
|
||||
ConditionOutcome outcome = getMatchOutcome(new MockEnvironment().withProperty("spring.hazelcast.config",
|
||||
"classpath:org/springframework/boot/autoconfigure/hazelcast/test-config-does-not-exist.xml"));
|
||||
assertThat(outcome.isMatch()).isFalse();
|
||||
assertThat(outcome.getMessage()).contains("Hazelcast configuration does not exist");
|
||||
}
|
||||
|
||||
private ConditionOutcome getMatchOutcome(Environment environment) {
|
||||
ConditionContext conditionContext = mock(ConditionContext.class);
|
||||
given(conditionContext.getEnvironment()).willReturn(environment);
|
||||
given(conditionContext.getResourceLoader()).willReturn(new DefaultResourceLoader());
|
||||
return this.condition.getMatchOutcome(conditionContext, mock(AnnotatedTypeMetadata.class));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.session;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
|
||||
import org.springframework.boot.test.context.FilteredClassLoader;
|
||||
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
||||
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
|
||||
import org.springframework.boot.testsupport.classpath.ClassPathOverrides;
|
||||
import org.springframework.session.FlushMode;
|
||||
import org.springframework.session.SaveMode;
|
||||
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
|
||||
import org.springframework.session.data.redis.RedisIndexedSessionRepository;
|
||||
import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository;
|
||||
import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Hazelcast 3 specific tests for {@link SessionAutoConfiguration}.
|
||||
*
|
||||
* @author Vedran Pavic
|
||||
*/
|
||||
@ClassPathExclusions("hazelcast*.jar")
|
||||
@ClassPathOverrides("com.hazelcast:hazelcast:3.12.8")
|
||||
class SessionAutoConfigurationHazelcast3Tests extends AbstractSessionAutoConfigurationTests {
|
||||
|
||||
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class, SessionAutoConfiguration.class))
|
||||
.withPropertyValues(
|
||||
"spring.hazelcast.config=org/springframework/boot/autoconfigure/session/hazelcast-3.xml");
|
||||
|
||||
@Test
|
||||
void defaultConfig() {
|
||||
this.contextRunner.withPropertyValues("spring.session.store-type=hazelcast").run(this::validateDefaultConfig);
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultConfigWithUniqueStoreImplementation() {
|
||||
this.contextRunner
|
||||
.withClassLoader(new FilteredClassLoader(JdbcIndexedSessionRepository.class,
|
||||
RedisIndexedSessionRepository.class, MongoIndexedSessionRepository.class))
|
||||
.run(this::validateDefaultConfig);
|
||||
}
|
||||
|
||||
private void validateDefaultConfig(AssertableWebApplicationContext context) {
|
||||
validateSessionRepository(context, HazelcastIndexedSessionRepository.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void customMapName() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.session.store-type=hazelcast",
|
||||
"spring.session.hazelcast.map-name=foo:bar:biz")
|
||||
.run((context) -> validateSessionRepository(context, HazelcastIndexedSessionRepository.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void customFlushMode() {
|
||||
this.contextRunner.withPropertyValues("spring.session.store-type=hazelcast",
|
||||
"spring.session.hazelcast.flush-mode=immediate").run((context) -> {
|
||||
HazelcastIndexedSessionRepository repository = validateSessionRepository(context,
|
||||
HazelcastIndexedSessionRepository.class);
|
||||
assertThat(repository).hasFieldOrPropertyWithValue("flushMode", FlushMode.IMMEDIATE);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void customSaveMode() {
|
||||
this.contextRunner.withPropertyValues("spring.session.store-type=hazelcast",
|
||||
"spring.session.hazelcast.save-mode=on-get-attribute").run((context) -> {
|
||||
HazelcastIndexedSessionRepository repository = validateSessionRepository(context,
|
||||
HazelcastIndexedSessionRepository.class);
|
||||
assertThat(repository).hasFieldOrPropertyWithValue("saveMode", SaveMode.ON_GET_ATTRIBUTE);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.2.xsd"
|
||||
xmlns="http://www.hazelcast.com/schema/config"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
|
||||
<queue name="foobar"/>
|
||||
|
||||
<map name="foobar">
|
||||
<time-to-live-seconds>3600</time-to-live-seconds>
|
||||
<max-idle-seconds>600</max-idle-seconds>
|
||||
</map>
|
||||
|
||||
<network>
|
||||
<join>
|
||||
<tcp-ip enabled="false"/>
|
||||
<multicast enabled="false"/>
|
||||
</join>
|
||||
</network>
|
||||
|
||||
</hazelcast>
|
@ -1,7 +1,7 @@
|
||||
<hazelcast xmlns="http://www.hazelcast.com/schema/config"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.hazelcast.com/schema/config
|
||||
http://www.hazelcast.com/schema/config/hazelcast-config-4.0.xsd">
|
||||
http://www.hazelcast.com/schema/config/hazelcast-config-3.12.xsd">
|
||||
<map name="countries">
|
||||
<time-to-live-seconds>600</time-to-live-seconds>
|
||||
</map>
|
2
spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-hazelcast4/src/test/java/smoketest/hazelcast4/SampleHazelcast4ApplicationTests.java → spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-hazelcast3/src/test/java/smoketest/hazelcast3/SampleHazelcast4ApplicationTests.java
2
spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-hazelcast4/src/test/java/smoketest/hazelcast4/SampleHazelcast4ApplicationTests.java → spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-hazelcast3/src/test/java/smoketest/hazelcast3/SampleHazelcast4ApplicationTests.java
Loading…
Reference in New Issue