Skip to content

Commit 5511987

Browse files
committed
spring-projectsGH-172: Do not require local HZ instances
Fixes spring-projects#172 Since we can have an application based on the `HazelcastClient`, we don't need to require `Hazelcast.getAllHazelcastInstances()` be presented. * Rework `HazelcastLocalInstanceRegistrar` to be able to accept external `HazelcastInstance` for `MultiMap` and `MembershipListener` registration * If there is on local `Hazelcast.getAllHazelcastInstances()` just log a warn that we can't register `MembershipListener` * Allow for `AbstractHazelcastMessageProducer` to accept events in the `CacheListeningPolicyType.SINGLE` mode when there is no local `Hazelcast.getAllHazelcastInstances()`
1 parent 894a60b commit 5511987

File tree

4 files changed

+57
-15
lines changed

4 files changed

+57
-15
lines changed

spring-integration-hazelcast/src/main/java/org/springframework/integration/hazelcast/HazelcastLocalInstanceRegistrar.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import java.net.SocketAddress;
2020
import java.util.concurrent.locks.Lock;
2121

22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
24+
2225
import org.springframework.beans.factory.SmartInitializingSingleton;
2326
import org.springframework.integration.hazelcast.listener.HazelcastMembershipListener;
2427

@@ -33,10 +36,19 @@
3336
* membership updates.
3437
*
3538
* @author Eren Avsarogullari
39+
* @author Artem Bilan
40+
*
3641
* @since 1.0.0
3742
*/
3843
public class HazelcastLocalInstanceRegistrar implements SmartInitializingSingleton {
3944

45+
private static final Log logger = LogFactory.getLog(HazelcastLocalInstanceRegistrar.class);
46+
47+
/**
48+
* The bean name for the {@link HazelcastLocalInstanceRegistrar} instance.
49+
*/
50+
public static final String BEAN_NAME = "hazelcastLocalInstanceRegistrar";
51+
4052
/**
4153
* The name for the Hazelcast MultiMap used for membership registration.
4254
*/
@@ -48,15 +60,38 @@ public class HazelcastLocalInstanceRegistrar implements SmartInitializingSinglet
4860
*/
4961
public static final String SPRING_INTEGRATION_INTERNAL_CLUSTER_LOCK = "SPRING_INTEGRATION_INTERNAL_CLUSTER_LOCK";
5062

63+
private final HazelcastInstance hazelcastInstance;
64+
65+
/**
66+
* Construct {@link HazelcastLocalInstanceRegistrar} based on the local JVM {@link HazelcastInstance}s if any.
67+
*/
68+
public HazelcastLocalInstanceRegistrar() {
69+
this.hazelcastInstance = null;
70+
}
71+
72+
/**
73+
* Construct {@link HazelcastLocalInstanceRegistrar} based on the provided {@link HazelcastInstance}.
74+
* @param hazelcastInstance the {@link HazelcastInstance} to use.
75+
*/
76+
public HazelcastLocalInstanceRegistrar(HazelcastInstance hazelcastInstance) {
77+
this.hazelcastInstance = hazelcastInstance;
78+
}
79+
5180
@Override
5281
public void afterSingletonsInstantiated() {
53-
if (!Hazelcast.getAllHazelcastInstances().isEmpty()) {
54-
HazelcastInstance hazelcastInstance = Hazelcast.getAllHazelcastInstances().iterator().next();
55-
hazelcastInstance.getCluster().addMembershipListener(new HazelcastMembershipListener());
56-
syncConfigurationMultiMap(hazelcastInstance);
82+
if (this.hazelcastInstance == null) {
83+
if (!Hazelcast.getAllHazelcastInstances().isEmpty()) {
84+
HazelcastInstance hazelcastInstance = Hazelcast.getAllHazelcastInstances().iterator().next();
85+
hazelcastInstance.getCluster().addMembershipListener(new HazelcastMembershipListener());
86+
syncConfigurationMultiMap(hazelcastInstance);
87+
}
88+
else {
89+
logger.warn("No HazelcastInstances for MembershipListener registration");
90+
}
5791
}
5892
else {
59-
throw new IllegalStateException("No Active Local Hazelcast Instance found.");
93+
syncConfigurationMultiMap(this.hazelcastInstance);
94+
this.hazelcastInstance.getCluster().addMembershipListener(new HazelcastMembershipListener());
6095
}
6196
}
6297

spring-integration-hazelcast/src/main/java/org/springframework/integration/hazelcast/config/HazelcastIntegrationConfigurationInitializer.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,18 +27,17 @@
2727
* The Hazelcast Integration infrastructure {@code beanFactory} initializer.
2828
*
2929
* @author Eren Avsarogullari
30+
* @author Artem Bilan
31+
*
3032
* @since 1.0.0
3133
*/
3234
public class HazelcastIntegrationConfigurationInitializer implements IntegrationConfigurationInitializer {
3335

34-
private static final String HAZELCAST_LOCAL_INSTANCE_REGISTRAR_BEAN_NAME =
35-
HazelcastLocalInstanceRegistrar.class.getName();
36-
3736
@Override
3837
public void initialize(ConfigurableListableBeanFactory beanFactory) throws BeansException {
3938
BeanDefinitionRegistry beanDefinitionRegistry = (BeanDefinitionRegistry) beanFactory;
40-
if (!beanDefinitionRegistry.containsBeanDefinition(HAZELCAST_LOCAL_INSTANCE_REGISTRAR_BEAN_NAME)) {
41-
beanDefinitionRegistry.registerBeanDefinition(HAZELCAST_LOCAL_INSTANCE_REGISTRAR_BEAN_NAME,
39+
if (!beanDefinitionRegistry.containsBeanDefinition(HazelcastLocalInstanceRegistrar.BEAN_NAME)) {
40+
beanDefinitionRegistry.registerBeanDefinition(HazelcastLocalInstanceRegistrar.BEAN_NAME,
4241
new RootBeanDefinition(HazelcastLocalInstanceRegistrar.class));
4342
}
4443
}

spring-integration-hazelcast/src/main/java/org/springframework/integration/hazelcast/inbound/AbstractHazelcastMessageProducer.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
*
4949
* @author Eren Avsarogullari
5050
* @author Artem Bilan
51+
*
5152
* @since 1.0.0
5253
*/
5354
public abstract class AbstractHazelcastMessageProducer extends MessageProducerSupport {
@@ -116,10 +117,11 @@ protected void sendMessage(E event, InetSocketAddress socketAddress,
116117
private boolean isEventAcceptable(final InetSocketAddress socketAddress) {
117118
final Set<HazelcastInstance> hazelcastInstanceSet = Hazelcast.getAllHazelcastInstances();
118119
final Set<SocketAddress> localSocketAddressesSet = getLocalSocketAddresses(hazelcastInstanceSet);
119-
return (!localSocketAddressesSet.isEmpty())
120-
&& (localSocketAddressesSet.contains(socketAddress) ||
121-
isEventComingFromNonRegisteredHazelcastInstance(hazelcastInstanceSet.iterator().next(),
122-
localSocketAddressesSet, socketAddress));
120+
return localSocketAddressesSet.isEmpty() ||
121+
(!localSocketAddressesSet.isEmpty()
122+
&& (localSocketAddressesSet.contains(socketAddress) ||
123+
isEventComingFromNonRegisteredHazelcastInstance(hazelcastInstanceSet.iterator().next(),
124+
localSocketAddressesSet, socketAddress)));
123125

124126
}
125127

spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastIntegrationInboundTestConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.integration.config.EnableIntegration;
2525
import org.springframework.integration.hazelcast.DistributedSQLIterationType;
2626
import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser;
27+
import org.springframework.integration.hazelcast.HazelcastLocalInstanceRegistrar;
2728
import org.springframework.integration.hazelcast.inbound.HazelcastClusterMonitorMessageProducer;
2829
import org.springframework.integration.hazelcast.inbound.HazelcastContinuousQueryMessageProducer;
2930
import org.springframework.integration.hazelcast.inbound.HazelcastDistributedSQLMessageSource;
@@ -215,6 +216,11 @@ public HazelcastInstance testHazelcastInstance() {
215216
return Hazelcast.newHazelcastInstance();
216217
}
217218

219+
@Bean(HazelcastLocalInstanceRegistrar.BEAN_NAME)
220+
public HazelcastLocalInstanceRegistrar hazelcastLocalInstanceRegistrar() {
221+
return new HazelcastLocalInstanceRegistrar(testHazelcastInstance());
222+
}
223+
218224
@Bean
219225
public HazelcastEventDrivenMessageProducer hazelcastEventDrivenMessageProducer() {
220226
final HazelcastEventDrivenMessageProducer producer =

0 commit comments

Comments
 (0)