diff --git a/src/main/java/org/genesys2/spring/config/HazelcastConfig.java b/src/main/java/org/genesys2/spring/config/HazelcastConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..ab0d63e90fc9176fea57073d12b0a80f73febe2a --- /dev/null +++ b/src/main/java/org/genesys2/spring/config/HazelcastConfig.java @@ -0,0 +1,57 @@ +/** + * 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(); + } + +} diff --git a/src/main/java/org/genesys2/spring/config/HazelcastConfigAWS.java b/src/main/java/org/genesys2/spring/config/HazelcastConfigAWS.java new file mode 100644 index 0000000000000000000000000000000000000000..88b62048226c8655d0b3e89affa98914b393bcce --- /dev/null +++ b/src/main/java/org/genesys2/spring/config/HazelcastConfigAWS.java @@ -0,0 +1,107 @@ +/** + * 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; + } + +} diff --git a/src/main/java/org/genesys2/spring/config/HazelcastConfigDev.java b/src/main/java/org/genesys2/spring/config/HazelcastConfigDev.java new file mode 100644 index 0000000000000000000000000000000000000000..c5ea48a3f3e976054f66e63a54950162e415e84f --- /dev/null +++ b/src/main/java/org/genesys2/spring/config/HazelcastConfigDev.java @@ -0,0 +1,93 @@ +/** + * 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; + } + +} diff --git a/src/main/java/org/genesys2/spring/config/SpringCacheConfig.java b/src/main/java/org/genesys2/spring/config/SpringCacheConfig.java index b5a8a3901f3fd1f26ba53f775b4715fb088a82e0..68fe7df4a388f9688759edbe8978447bee314303 100644 --- a/src/main/java/org/genesys2/spring/config/SpringCacheConfig.java +++ b/src/main/java/org/genesys2/spring/config/SpringCacheConfig.java @@ -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 tileserverMap(HazelcastInstance hazelcast) { + IMap 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; } diff --git a/src/main/java/org/genesys2/spring/config/SpringServletConfig.java b/src/main/java/org/genesys2/spring/config/SpringServletConfig.java index 3c135e2b4bf45b54cac125bf38b1153a4f5399af..7b8be59662331f9dd369a80d0befe74a5eb884e1 100644 --- a/src/main/java/org/genesys2/spring/config/SpringServletConfig.java +++ b/src/main/java/org/genesys2/spring/config/SpringServletConfig.java @@ -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")); diff --git a/src/main/resources/hazelcast.xml b/src/main/resources/hazelcast.xml deleted file mode 100644 index 7658157e1a31c097485366813674b3b8ecbbfad4..0000000000000000000000000000000000000000 --- a/src/main/resources/hazelcast.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - diff --git a/src/main/resources/spring/spring-cache.xml b/src/main/resources/spring/spring-cache.xml deleted file mode 100644 index 7fb1fb42ead59e8f5bd61d77295595f7d0f2b47a..0000000000000000000000000000000000000000 --- a/src/main/resources/spring/spring-cache.xml +++ /dev/null @@ -1,116 +0,0 @@ - - - - - - - - - genesys - - - 5 - 5 - log4j - true - - - - - - - - - - - - - - - - - - - - - - - genesys - - - 5 - 5 - log4j - - - - - - 127.0.0.1:5702 - - - - - - - - - - - - - - - - - genesys - - - 5 - 5 - log4j - - - - - - - - - - - - - - - - - - diff --git a/src/main/resources/spring/spring.properties b/src/main/resources/spring/spring.properties index 6d24a1f27fc403be0b01545b426b3c1aa931e551..1f430992f10567afb0c5632215855aba224df2ee 100644 --- a/src/main/resources/spring/spring.properties +++ b/src/main/resources/spring/spring.properties @@ -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