-
Notifications
You must be signed in to change notification settings - Fork 40.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide service connection support for Hazelcast
- Loading branch information
Showing
19 changed files
with
553 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...rg/springframework/boot/autoconfigure/hazelcast/HazelcastClientInstanceConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2012-2024 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.HazelcastClient; | ||
import com.hazelcast.client.config.ClientConfig; | ||
import com.hazelcast.core.HazelcastInstance; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* Configuration for Hazelcast client instance. | ||
* | ||
* @author Dmytro Nosan | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
@ConditionalOnBean(HazelcastConnectionDetails.class) | ||
class HazelcastClientInstanceConfiguration { | ||
|
||
@Bean | ||
HazelcastInstance hazelcastInstance(HazelcastConnectionDetails hazelcastConnectionDetails) { | ||
ClientConfig config = hazelcastConnectionDetails.getClientConfig(); | ||
if (StringUtils.hasText(config.getInstanceName())) { | ||
return HazelcastClient.getOrCreateHazelcastClient(config); | ||
} | ||
return HazelcastClient.newHazelcastClient(config); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...ain/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastConnectionDetails.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2012-2024 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.config.ClientConfig; | ||
|
||
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails; | ||
|
||
/** | ||
* Details required to establish a client connection to a Hazelcast instance. | ||
* | ||
* @author Dmytro Nosan | ||
* @since 3.4.0 | ||
*/ | ||
public interface HazelcastConnectionDetails extends ConnectionDetails { | ||
|
||
/** | ||
* The {@link ClientConfig} for Hazelcast client. | ||
* @return the client config | ||
*/ | ||
ClientConfig getClientConfig(); | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
...rg/springframework/boot/autoconfigure/hazelcast/PropertiesHazelcastConnectionDetails.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2012-2024 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.UncheckedIOException; | ||
import java.net.URL; | ||
|
||
import com.hazelcast.client.config.ClientConfig; | ||
import com.hazelcast.client.config.XmlClientConfigBuilder; | ||
import com.hazelcast.client.config.YamlClientConfigBuilder; | ||
|
||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.ResourceLoader; | ||
|
||
/** | ||
* Adapts {@link HazelcastProperties} to {@link HazelcastConnectionDetails}. | ||
* | ||
* @author Dmytro Nosan | ||
*/ | ||
class PropertiesHazelcastConnectionDetails implements HazelcastConnectionDetails { | ||
|
||
private final HazelcastProperties properties; | ||
|
||
private final ResourceLoader resourceLoader; | ||
|
||
PropertiesHazelcastConnectionDetails(HazelcastProperties properties, ResourceLoader resourceLoader) { | ||
this.properties = properties; | ||
this.resourceLoader = resourceLoader; | ||
} | ||
|
||
@Override | ||
public ClientConfig getClientConfig() { | ||
Resource configLocation = this.properties.resolveConfigLocation(); | ||
ClientConfig config = (configLocation != null) ? loadClientConfig(configLocation) : ClientConfig.load(); | ||
config.setClassLoader(this.resourceLoader.getClassLoader()); | ||
return config; | ||
} | ||
|
||
private ClientConfig loadClientConfig(Resource configLocation) { | ||
try { | ||
URL configUrl = configLocation.getURL(); | ||
String configFileName = configUrl.getPath(); | ||
if (configFileName.endsWith(".yaml") || configFileName.endsWith(".yml")) { | ||
return new YamlClientConfigBuilder(configUrl).build(); | ||
} | ||
return new XmlClientConfigBuilder(configUrl).build(); | ||
} | ||
catch (IOException ex) { | ||
throw new UncheckedIOException("Failed to load Hazelcast config", ex); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
.../connection/hazelcast/HazelcastDockerComposeConnectionDetailsFactoryIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2012-2024 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.docker.compose.service.connection.hazelcast; | ||
|
||
import com.hazelcast.client.HazelcastClient; | ||
import com.hazelcast.core.HazelcastInstance; | ||
import com.hazelcast.map.IMap; | ||
|
||
import org.springframework.boot.autoconfigure.hazelcast.HazelcastConnectionDetails; | ||
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest; | ||
import org.springframework.boot.testsupport.container.TestImage; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Integration tests for {@link HazelcastDockerComposeConnectionDetailsFactory}. | ||
* | ||
* @author Dmytro Nosan | ||
*/ | ||
class HazelcastDockerComposeConnectionDetailsFactoryIntegrationTests { | ||
|
||
@DockerComposeTest(composeFile = "hazelcast-compose.yaml", image = TestImage.HAZELCAST) | ||
void runCreatesConnectionDetails(HazelcastConnectionDetails connectionDetails) { | ||
HazelcastInstance hazelcastInstance = HazelcastClient.newHazelcastClient(connectionDetails.getClientConfig()); | ||
try { | ||
IMap<Object, Object> map = hazelcastInstance.getMap("spring-boot"); | ||
map.put("docker", "compose"); | ||
assertThat(map.get("docker")).isEqualTo("compose"); | ||
} | ||
finally { | ||
hazelcastInstance.shutdown(); | ||
} | ||
|
||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
...g/springframework/boot/docker/compose/service/connection/hazelcast/hazelcast-compose.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
services: | ||
hazelcast: | ||
image: '{imageName}' | ||
ports: | ||
- '5701' |
Oops, something went wrong.