Skip to content

Commit 002a7c6

Browse files
Ali Gurbuzsnicoll
authored andcommitted
Disable Hazelcast auto-configuration when Jet is present
See gh-20729
1 parent dfac3a2 commit 002a7c6

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2222
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2323
import org.springframework.boot.context.properties.EnableConfigurationProperties;
24+
import org.springframework.context.annotation.Conditional;
2425
import org.springframework.context.annotation.Configuration;
2526
import org.springframework.context.annotation.Import;
2627

@@ -35,6 +36,7 @@
3536
* @see HazelcastConfigResourceCondition
3637
*/
3738
@Configuration(proxyBeanMethods = false)
39+
@Conditional(HazelcastJetConfigMissingCondition.class)
3840
@ConditionalOnClass(HazelcastInstance.class)
3941
@EnableConfigurationProperties(HazelcastProperties.class)
4042
@Import({ HazelcastClientConfiguration.class, HazelcastServerConfiguration.class })
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.hazelcast;
18+
19+
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
20+
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
21+
import org.springframework.context.annotation.ConditionContext;
22+
import org.springframework.core.io.Resource;
23+
import org.springframework.core.type.AnnotatedTypeMetadata;
24+
25+
/**
26+
* {@link SpringBootCondition} that checks if the Hazelcast Jet is missing on the
27+
* classpath by looking up the default configuration file.
28+
*
29+
* @author Ali Gurbuz
30+
* @since 2.3.0
31+
*/
32+
public class HazelcastJetConfigMissingCondition extends SpringBootCondition {
33+
34+
private static final String HAZELCAST_JET_CONFIG_FILE = "classpath:/hazelcast-jet-default.yaml";
35+
36+
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
37+
Resource resource = context.getResourceLoader().getResource(HAZELCAST_JET_CONFIG_FILE);
38+
if (resource.exists()) {
39+
return ConditionOutcome.noMatch("Found Hazelcast Jet default config file on the classpath");
40+
}
41+
return ConditionOutcome.match("Hazelcast Jet default config file is missing on the classpath");
42+
}
43+
44+
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616

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

19+
import java.net.MalformedURLException;
20+
import java.net.URL;
21+
1922
import com.hazelcast.config.Config;
2023
import com.hazelcast.core.HazelcastInstance;
24+
import org.jetbrains.annotations.Nullable;
2125
import org.junit.jupiter.api.Test;
2226

2327
import org.springframework.boot.autoconfigure.AutoConfigurations;
@@ -47,4 +51,35 @@ void defaultConfigFile() {
4751
});
4852
}
4953

54+
@Test
55+
void testHazelcastInstanceNotCreatedWhenJetIsPresent() {
56+
this.contextRunner.withClassLoader(new JetConfigClassLoader())
57+
.run((context) -> assertThat(context).doesNotHaveBean(HazelcastInstance.class));
58+
}
59+
60+
/**
61+
* A specific class loader which emulates that default Hazelcast Jet configuration
62+
* file exists on the classpath.
63+
*/
64+
static class JetConfigClassLoader extends ClassLoader {
65+
66+
JetConfigClassLoader() {
67+
super(JetConfigClassLoader.class.getClassLoader());
68+
}
69+
70+
@Nullable
71+
@Override
72+
public URL getResource(String name) {
73+
if (name.equals("hazelcast-jet-default.yaml")) {
74+
try {
75+
return new URL("file://hazelcast-jet-default.yaml");
76+
}
77+
catch (MalformedURLException ignored) {
78+
}
79+
}
80+
return super.getResource(name);
81+
}
82+
83+
}
84+
5085
}

0 commit comments

Comments
 (0)