Commit 710df3bb authored by Matija Obreza's avatar Matija Obreza
Browse files

Allow for HZ members configuration

parent 4cba5690
......@@ -30,6 +30,8 @@ import com.hazelcast.hibernate.HazelcastCacheRegionFactory;
import com.hazelcast.spring.context.SpringManagedContext;
import org.genesys2.spring.hazelcast.HazelcastCacheRegionFactoryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
......@@ -40,6 +42,8 @@ import org.springframework.context.annotation.Import;
@EnableCaching
@Import({ HazelcastConfigAWS.class, HazelcastConfigCacher.class })
public abstract class HazelcastConfig {
protected final Logger LOG = LoggerFactory.getLogger(HazelcastConfig.class);
/// Interfaces on which HZ should bind
@Value("${hazelcast.interfaces}")
......
......@@ -16,14 +16,18 @@
package org.genesys2.spring.config;
import java.util.List;
import com.hazelcast.config.Config;
import com.hazelcast.config.GroupConfig;
import com.hazelcast.config.JoinConfig;
import com.hazelcast.config.NetworkConfig;
import com.hazelcast.config.TcpIpConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.ManagedContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
......@@ -32,6 +36,9 @@ import org.springframework.context.annotation.Profile;
@Profile({ "cacher", "dev" })
public class HazelcastConfigCacher extends HazelcastConfig {
@Value("${hazelcast.members}")
private List<String> hazelcastMembers;
@Bean
public HazelcastInstance hazelcast(ManagedContext managedContext) {
Config cfg = new Config();
......@@ -52,12 +59,16 @@ public class HazelcastConfigCacher extends HazelcastConfig {
network.setPortAutoIncrement(true);
JoinConfig join = network.getJoin();
join.getMulticastConfig().setEnabled(true);
// TcpIpConfig tcpIpConfig = join.getTcpIpConfig();
// tcpIpConfig.setEnabled(true);
// tcpIpConfig.setConnectionTimeoutSeconds(20);
// See if there's a local HZ running
// tcpIpConfig.addMember("127.0.0.1:5702");
if (!hasConfiguredMembers()) {
LOG.warn("Enabling Hazelcast multicast, members={}", hazelcastMembers);
join.getMulticastConfig().setEnabled(false);
} else {
LOG.warn("Enabling TCP/IP discovery for members={}", hazelcastMembers);
TcpIpConfig tcpIpConfig = join.getTcpIpConfig();
tcpIpConfig.setEnabled(true);
tcpIpConfig.setConnectionTimeoutSeconds(20);
tcpIpConfig.setMembers(hazelcastMembers);
}
configureHazelcast(cfg);
......@@ -65,4 +76,9 @@ public class HazelcastConfigCacher extends HazelcastConfig {
return instance;
}
private boolean hasConfiguredMembers() {
LOG.debug("HZ members={}", hazelcastMembers);
return this.hazelcastMembers != null && this.hazelcastMembers.size() != 0 && this.hazelcastMembers.get(0).length() > 0;
}
}
/**
* Copyright 2014 Global Crop Diversity Trust
*
* 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
*
* http://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.genesys2.spring.config;
import com.hazelcast.config.Config;
import com.hazelcast.config.GroupConfig;
import com.hazelcast.config.JoinConfig;
import com.hazelcast.config.NetworkConfig;
import com.hazelcast.config.TcpIpConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.ManagedContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile({ "dev" })
public class HazelcastConfigDev extends HazelcastConfig {
@Value("${cache.time-to-live-seconds}")
private int defaultTimeToLiveSeconds;
@Value("${cache.max-idle-seconds}")
private int defaultMaxIdleSeconds;
@Bean
public HazelcastInstance hazelcast(ManagedContext managedContext) {
Config cfg = new Config();
cfg.setManagedContext(managedContext);
cfg.setInstanceName(instanceName);
GroupConfig groupConfig = cfg.getGroupConfig();
groupConfig.setName(name);
groupConfig.setPassword(password);
cfg.setProperty("hazelcast.merge.first.run.delay.seconds", "5");
cfg.setProperty("hazelcast.merge.next.run.delay.seconds", "5");
cfg.setProperty("hazelcast.logging.type", "log4j");
cfg.setProperty("hazelcast.icmp.enabled", "true");
NetworkConfig network = cfg.getNetworkConfig();
JoinConfig join = network.getJoin();
join.getMulticastConfig().setEnabled(false);
TcpIpConfig tcpIpConfig = join.getTcpIpConfig();
tcpIpConfig.setEnabled(false);
tcpIpConfig.setConnectionTimeoutSeconds(20);
configureHazelcast(cfg);
HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
return instance;
}
}
......@@ -115,6 +115,7 @@ hazelcast.password=hazelcastdevelopment
hazelcast.publicAddress=
hazelcast.interfaces=
hazelcast.port=5701
hazelcast.members=
# AWS Autodetection
hazelcast.aws.access-key=
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment