Commit c6ef5dbb authored by Matija Obreza's avatar Matija Obreza
Browse files

Hazelcast Java config

parent 7553b35e
/**
* 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 org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import com.hazelcast.config.MapConfig.EvictionPolicy;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.ManagedContext;
import com.hazelcast.spring.context.SpringManagedContext;
@Configuration
@EnableCaching
@Import({ HazelcastConfigDev.class, HazelcastConfigAWS.class })
public abstract class HazelcastConfig {
@Value("${hazelcast.port}")
protected int hazelPort;
@Value("${hazelcast.instanceName}")
protected String instanceName = "genesys";
@Value("${hazelcast.password}")
protected String password;
@Value("${hazelcast.name}")
protected String name;
@Value("${cache.tileserver.max-size}")
protected int tileserverMaxSize;
@Value("${cache.tileserver.max-idle-seconds}")
protected int tileserverMaxIdle;
@Value("${cache.tileserver.time-to-live-seconds}")
protected int tileserverTTL;
@Value("${cache.tileserver.eviction-policy}")
protected EvictionPolicy tileserverEvictionPolicy;
@Bean
public ManagedContext managedContext() {
return new SpringManagedContext();
}
}
/**
* 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 org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import com.hazelcast.config.AwsConfig;
import com.hazelcast.config.Config;
import com.hazelcast.config.ExecutorConfig;
import com.hazelcast.config.GroupConfig;
import com.hazelcast.config.JoinConfig;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.MaxSizeConfig;
import com.hazelcast.config.MaxSizeConfig.MaxSizePolicy;
import com.hazelcast.config.NetworkConfig;
import com.hazelcast.config.QueueConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.ManagedContext;
@Configuration
@Profile("aws")
public class HazelcastConfigAWS extends HazelcastConfig {
@Value("${hazelcast.aws.region}")
private String region;
@Value("${hazelcast.aws.access-key}")
private String accessKey;
@Value("${hazelcast.aws.secret-key}")
private String secretKey;
@Value("${hazelcast.aws.security-group}")
private String securityGroupName;
@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();
network.setPort(hazelPort);
network.setPortAutoIncrement(false);
JoinConfig join = network.getJoin();
join.getMulticastConfig().setEnabled(false);
AwsConfig awsConfig = join.getAwsConfig();
awsConfig.setEnabled(true);
awsConfig.setRegion(region);
awsConfig.setAccessKey(accessKey);
awsConfig.setSecretKey(secretKey);
awsConfig.setConnectionTimeoutSeconds(20);
awsConfig.setSecurityGroupName(securityGroupName);
MapConfig tileserverMapConfig = new MapConfig();
tileserverMapConfig.setName("tileserver");
tileserverMapConfig.setTimeToLiveSeconds(tileserverTTL);
tileserverMapConfig.setEvictionPolicy(tileserverEvictionPolicy);
tileserverMapConfig.setMaxIdleSeconds(tileserverMaxIdle);
MaxSizeConfig tileserverMaxSizeConfig = new MaxSizeConfig();
tileserverMaxSizeConfig.setSize(tileserverMaxSize);
tileserverMaxSizeConfig.setMaxSizePolicy(MaxSizePolicy.PER_NODE);
tileserverMapConfig.setMaxSizeConfig(tileserverMaxSizeConfig);
cfg.addMapConfig(tileserverMapConfig);
ExecutorConfig execConfig = new ExecutorConfig();
execConfig.setName("hazel-exec");
execConfig.setPoolSize(4);
execConfig.setQueueCapacity(2);
execConfig.setStatisticsEnabled(true);
cfg.addExecutorConfig(execConfig);
QueueConfig queueConfig = new QueueConfig();
queueConfig.setName("elasticsearchQueue");
queueConfig.setMaxSize(100);
cfg.addQueueConfig(queueConfig);
HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
return instance;
}
}
/**
* 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 org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.hazelcast.config.Config;
import com.hazelcast.config.ExecutorConfig;
import com.hazelcast.config.GroupConfig;
import com.hazelcast.config.JoinConfig;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.MaxSizeConfig;
import com.hazelcast.config.MaxSizeConfig.MaxSizePolicy;
import com.hazelcast.config.NetworkConfig;
import com.hazelcast.config.QueueConfig;
import com.hazelcast.config.TcpIpConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.ManagedContext;
@Configuration
public class HazelcastConfigDev extends HazelcastConfig {
@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();
network.setPort(hazelPort);
network.setPortAutoIncrement(false);
JoinConfig join = network.getJoin();
join.getMulticastConfig().setEnabled(false);
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");
MapConfig tileserverMapConfig = new MapConfig();
tileserverMapConfig.setName("tileserver");
tileserverMapConfig.setTimeToLiveSeconds(tileserverTTL);
tileserverMapConfig.setEvictionPolicy(tileserverEvictionPolicy);
tileserverMapConfig.setMaxIdleSeconds(tileserverMaxIdle);
MaxSizeConfig tileserverMaxSizeConfig = new MaxSizeConfig();
tileserverMaxSizeConfig.setSize(tileserverMaxSize);
tileserverMaxSizeConfig.setMaxSizePolicy(MaxSizePolicy.PER_NODE);
tileserverMapConfig.setMaxSizeConfig(tileserverMaxSizeConfig);
cfg.addMapConfig(tileserverMapConfig);
ExecutorConfig execConfig = new ExecutorConfig();
execConfig.setName("hazel-exec");
execConfig.setPoolSize(4);
execConfig.setQueueCapacity(2);
execConfig.setStatisticsEnabled(true);
cfg.addExecutorConfig(execConfig);
QueueConfig queueConfig = new QueueConfig();
queueConfig.setName("elasticsearchQueue");
queueConfig.setMaxSize(100);
cfg.addQueueConfig(queueConfig);
HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
return instance;
}
}
......@@ -22,14 +22,22 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Import;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IExecutorService;
import com.hazelcast.core.IMap;
import com.hazelcast.core.ManagedContext;
import com.hazelcast.spring.cache.HazelcastCacheManager;
import com.hazelcast.spring.context.SpringManagedContext;
import com.hazelcast.web.WebFilter;
@Configuration
@EnableCaching
@ImportResource("classpath:/spring/spring-cache.xml")
@Import({ HazelcastConfig.class })
public class SpringCacheConfig {
@Value("${hazelcast.instanceName}")
protected String instanceName = "genesys";
@Value("${base.cookie-domain}")
private String cookieDomain;
......@@ -39,6 +47,24 @@ public class SpringCacheConfig {
@Value("${base.cookie-http-only}")
private String cookieHttpOnly;
@Bean
public HazelcastCacheManager cacheManager(HazelcastInstance hazelcastInstance) {
HazelcastCacheManager cm = new HazelcastCacheManager(hazelcastInstance);
return cm;
}
@Bean
public IMap<Object, Object> tileserverMap(HazelcastInstance hazelcast) {
IMap<Object, Object> x = hazelcast.getMap("tileserver");
return x;
}
@Bean
public IExecutorService distributedExecutor(HazelcastInstance hazelcast) {
IExecutorService executorService = hazelcast.getExecutorService("hazel-exec");
return executorService;
}
@Bean
public WebFilter hazelcastWebFilter() {
......@@ -55,7 +81,7 @@ public class SpringCacheConfig {
properties.setProperty("cookie-secure", cookieSecure);
properties.setProperty("cookie-http-only", cookieHttpOnly);
properties.setProperty("debug", "true");
properties.setProperty("instance-name", "genesys");
properties.setProperty("instance-name", instanceName);
properties.setProperty("shutdown-on-destroy", "true");
return properties;
}
......
......@@ -54,10 +54,10 @@ public class SpringServletConfig extends WebMvcConfigurerAdapter {
@Value("${theme.defaultThemeName}")
private String defaultThemeName;
@Autowired
private AddStuffInterceptor addStuffInterceptor;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
......@@ -134,7 +134,7 @@ public class SpringServletConfig extends WebMvcConfigurerAdapter {
supportedLocales.add("pt");
supportedLocales.add("ru");
supportedLocales.add("zh");
supportedLocales.add("sl");
// supportedLocales.add("sl");
final BetterSessionLocaleResolver resolver = new BetterSessionLocaleResolver();
resolver.setDefaultLocale(Locale.forLanguageTag("en"));
......
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
~
~ 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.
-->
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.1.xsd"
xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</hazelcast>
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:hz="http://www.hazelcast.com/schema/spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
http://www.hazelcast.com/schema/spring
http://www.hazelcast.com/schema/spring/hazelcast-spring-3.1.xsd">
<beans profile="aws">
<hz:hazelcast id="theHazelcast">
<hz:config>
<hz:instance-name>genesys</hz:instance-name>
<hz:group name="${hazelcast.name}" password="${hazelcast.password}" />
<hz:properties>
<hz:property name="hazelcast.merge.first.run.delay.seconds">5</hz:property>
<hz:property name="hazelcast.merge.next.run.delay.seconds">5</hz:property>
<hz:property name="hazelcast.logging.type">log4j</hz:property>
<hz:property name="hazelcast.icmp.enabled">true</hz:property>
</hz:properties>
<hz:network port="${hazelcast.port}" port-auto-increment="false">
<hz:join>
<hz:multicast enabled="false" />
<hz:tcp-ip connection-timeout-seconds="20" enabled="false" />
<hz:aws connection-timeout-seconds="5" enabled="true"
access-key="${hazelcast.aws.access-key}" region="${hazelcast.aws.region}"
secret-key="${hazelcast.aws.secret-key}" security-group-name="${hazelcast.aws.security-group}" />
</hz:join>
</hz:network>
<hz:map name="default" max-idle-seconds="3600" max-size="5000"
time-to-live-seconds="0" eviction-policy="LRU" />
<hz:map name="tileserver" max-size="${cache.tileserver.max-size}" max-idle-seconds="${cache.tileserver.max-idle-seconds}" time-to-live-seconds="${cache.tileserver.time-to-live-seconds}" eviction-policy="${cache.tileserver.eviction-policy}" />
</hz:config>
</hz:hazelcast>
<bean id="hzCacheManager" class="com.hazelcast.spring.cache.HazelcastCacheManager">
<constructor-arg ref="theHazelcast" />
</bean>
</beans>
<beans profile="cacher">
<hz:hazelcast id="theHazelcast">
<hz:config>
<hz:instance-name>genesys</hz:instance-name>
<hz:group name="${hazelcast.name}" password="${hazelcast.password}" />
<hz:properties>
<hz:property name="hazelcast.merge.first.run.delay.seconds">5</hz:property>
<hz:property name="hazelcast.merge.next.run.delay.seconds">5</hz:property>
<hz:property name="hazelcast.logging.type">log4j</hz:property>
</hz:properties>
<hz:network port="${hazelcast.port}" port-auto-increment="false">
<hz:join>
<hz:multicast enabled="false" />
<hz:tcp-ip enabled="true">
<hz:required-member>127.0.0.1:5702</hz:required-member>
</hz:tcp-ip>
</hz:join>
</hz:network>
<hz:map name="default" max-idle-seconds="300" max-size="1000"
time-to-live-seconds="0" eviction-policy="LRU" />
<hz:map name="tileserver" max-size="${cache.tileserver.max-size}" max-idle-seconds="${cache.tileserver.max-idle-seconds}" time-to-live-seconds="${cache.tileserver.time-to-live-seconds}" eviction-policy="${cache.tileserver.eviction-policy}" />
</hz:config>
</hz:hazelcast>
<bean id="hzCacheManager" class="com.hazelcast.spring.cache.HazelcastCacheManager">
<constructor-arg ref="theHazelcast" />
</bean>
</beans>
<beans profile="dev">
<hz:hazelcast id="theHazelcast">
<hz:config>
<hz:instance-name>genesys</hz:instance-name>
<hz:group name="${hazelcast.name}" password="${hazelcast.password}" />
<hz:properties>
<hz:property name="hazelcast.merge.first.run.delay.seconds">5</hz:property>
<hz:property name="hazelcast.merge.next.run.delay.seconds">5</hz:property>
<hz:property name="hazelcast.logging.type">log4j</hz:property>
</hz:properties>
<hz:network port="${hazelcast.port}" port-auto-increment="false">
<hz:join>
<hz:multicast enabled="false" />
<hz:tcp-ip enabled="false" />
</hz:join>
</hz:network>
<hz:map name="default" max-idle-seconds="300" max-size="1000"
time-to-live-seconds="0" eviction-policy="LRU" />
<hz:map name="tileserver" max-size="${cache.tileserver.max-size}" max-idle-seconds="${cache.tileserver.max-idle-seconds}" time-to-live-seconds="${cache.tileserver.time-to-live-seconds}" eviction-policy="${cache.tileserver.eviction-policy}" />
</hz:config>
</hz:hazelcast>
<bean id="hzCacheManager" class="com.hazelcast.spring.cache.HazelcastCacheManager">
<constructor-arg ref="theHazelcast" />
</bean>
</beans>
</beans>
......@@ -98,6 +98,7 @@ executor.queue.capacity=100
scheduler.max.pool.size=16
# Hazelcast
hazelcast.instanceName=genesys
hazelcast.name=genesys
hazelcast.password=hazelcasts
hazelcast.port=5701
......
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