From 617599f98558dd6e25eb76671e0c4ee65a7572cf Mon Sep 17 00:00:00 2001 From: vcupelloni Date: Fri, 23 Aug 2024 21:07:40 +0200 Subject: [PATCH 1/7] Adding hazelcast support, first move --- .../config/CacheConfiguration.java.ejs | 186 ++++++++++++++++++ .../micronaut/templates/build.gradle.ejs | 4 +- .../main/resources/application-dev.yml.ejs | 6 + .../main/resources/application-prod.yml.ejs | 6 + .../src/main/resources/application.yml.ejs | 5 + 5 files changed, 205 insertions(+), 2 deletions(-) diff --git a/generators/micronaut-cache/templates/src/main/java/_package_/config/CacheConfiguration.java.ejs b/generators/micronaut-cache/templates/src/main/java/_package_/config/CacheConfiguration.java.ejs index 0299c65f..d1a0a2d3 100644 --- a/generators/micronaut-cache/templates/src/main/java/_package_/config/CacheConfiguration.java.ejs +++ b/generators/micronaut-cache/templates/src/main/java/_package_/config/CacheConfiguration.java.ejs @@ -4,7 +4,9 @@ import java.util.Properties; import <%=packageName%>.util.JHipsterProperties; import io.micronaut.context.ApplicationContext; +import io.micronaut.context.annotation.Context; import io.micronaut.context.annotation.Factory; +import io.micronaut.context.annotation.Primary; <%_ if (cacheProvider === 'ehcache') { _%> import java.time.Duration; @@ -26,7 +28,31 @@ import org.redisson.config.Config; import org.redisson.config.SingleServerConfig; import org.redisson.jcache.configuration.RedissonConfiguration; <%_ } _%> +<%_ if (cacheProvider === 'hazelcast') { _%> +import io.micronaut.cache.DynamicCacheManager; +import io.micronaut.cache.hazelcast.HazelcastCacheManager; +import io.micronaut.core.convert.ConversionService; +import io.micronaut.scheduling.TaskExecutors; +import io.reactivex.rxjava3.core.Flowable; +import com.hazelcast.config.Config; +import com.hazelcast.config.EvictionPolicy; +import com.hazelcast.config.ManagementCenterConfig; +import com.hazelcast.config.MapConfig; +import com.hazelcast.config.MaxSizePolicy; +import com.hazelcast.core.Hazelcast; +import com.hazelcast.core.HazelcastInstance; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import tech.jhipster.config.JHipsterConstants; +<%_ if (serviceDiscoveryAny) { _%> +import io.micronaut.context.env.Environment; +import io.micronaut.discovery.DiscoveryClient; +import io.micronaut.discovery.ServiceInstance; +import io.micronaut.runtime.server.EmbeddedServer; +<%_ } _%> +<%_ } _%> import javax.cache.CacheManager; <%_ if (cacheProvider === 'redis') { _%> import javax.cache.Caching; @@ -34,14 +60,46 @@ import javax.cache.configuration.MutableConfiguration; import javax.cache.expiry.CreatedExpiryPolicy; import javax.cache.expiry.Duration; <%_ } _%> +<%_ if (cacheProvider === 'hazelcast') { _%> +import jakarta.annotation.PreDestroy; +import jakarta.inject.Named; +<%_ } _%> import jakarta.inject.Singleton; <%_ if (cacheProvider === 'redis') { _%> import java.util.concurrent.TimeUnit; <%_ } _%> +<%_ if (cacheProvider === 'hazelcast') { _%> +import java.util.List; +import java.util.concurrent.ExecutorService; +<%_ } _%> @Factory public class CacheConfiguration { +<%_ if (cacheProvider === 'hazelcast') { _%> + + private final Logger log = LoggerFactory.getLogger(CacheConfiguration.class); + + private final Environment env; + + private final JHipsterProperties jHipsterProperties; + + <%_ if (serviceDiscoveryAny) { _%> + private final EmbeddedServer embeddedServer; + + private final DiscoveryClient discoveryClient; + <%_ } _%> + + public CacheConfiguration(Environment env<% if (serviceDiscoveryAny) { %>, EmbeddedServer embeddedServer, DiscoveryClient discoveryClient<%_ } _%>, JHipsterProperties jHipsterProperties) { + this.env = env; + <% if (serviceDiscoveryAny) { %> + this.embeddedServer = embeddedServer; + this.discoveryClient = discoveryClient; + <%_ } _%> + this.jHipsterProperties = jHipsterProperties; + } +<%_ } _%> +<%_ if (cacheProvider === 'ehcache' || cacheProvider === 'caffeine' || cacheProvider === 'redis') { _%> private final javax.cache.configuration.Configuration jcacheConfiguration; public CacheConfiguration(JHipsterProperties jHipsterProperties) { @@ -145,4 +203,132 @@ public class CacheConfiguration { } cm.createCache(cacheName, jcacheConfiguration); } +<%_ } _%> +<%_ if (cacheProvider === 'hazelcast') { _%> + @Singleton + @Context + @Primary + public HazelcastInstance hazelcastInstance(Config hazelcastConfig) { + return Hazelcast.newHazelcastInstance(hazelcastConfig); + } + + @Singleton + public DynamicCacheManager cacheManager(HazelcastInstance hazelcastInstance, + @Named(TaskExecutors.IO) ExecutorService executorService) { + log.debug("Starting HazelcastCacheManager"); + return new HazelcastCacheManager(ConversionService.SHARED, hazelcastInstance, executorService); + } + + @PreDestroy + public void destroy() { + log.info("Closing Cache Manager"); + Hazelcast.shutdownAll(); + } + + @Singleton + public Config hazelcastConfig() { + log.debug("Configuring Hazelcast"); + HazelcastInstance hazelCastInstance = Hazelcast.getHazelcastInstanceByName("<%= baseName %>"); + if (hazelCastInstance != null) { + log.debug("Hazelcast already initialized"); + return hazelCastInstance.getConfig(); + } + + Config config = new Config(); + config.setInstanceName("<%= baseName %>"); + <%_ if (serviceDiscoveryAny) { _%> + config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); + + if (this.discoveryClient == null) { + log.warn("No discovery service is set up, Hazelcast cannot create a cluster."); + } else { + + String serviceId = env.getProperty("micronaut.application.name", String.class).orElse("<%= baseName %>"); + log.debug("Configuring Hazelcast clustering for instanceId: {}", serviceId); + + // In development, everything goes through 127.0.0.1, with a different port + if (env.getActiveNames().contains(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)) { + log.debug( + "Application is running with the \"dev\" profile, Hazelcast " + "cluster will only work with localhost instances" + ); + + config.getNetworkConfig().setPort(embeddedServer.getPort() + 5701); + config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true); + + List instances = Flowable.fromPublisher(discoveryClient.getInstances(serviceId)).blockingFirst(); + for (ServiceInstance instance : instances) { + String clusterMember = "127.0.0.1:" + (instance.getPort() + 5701); + log.debug("Adding Hazelcast (dev) cluster member {}", clusterMember); + config.getNetworkConfig().getJoin().getTcpIpConfig().addMember(clusterMember); + } + + } else { // Production configuration, one host per instance all using port 5701 + config.getNetworkConfig().setPort(5701); + config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true); + + List instances = Flowable.fromPublisher(discoveryClient.getInstances(serviceId)).blockingFirst(); + for (ServiceInstance instance : instances) { + String clusterMember = instance.getHost() + ":5701"; + log.debug("Adding Hazelcast (prod) cluster member {}", clusterMember); + config.getNetworkConfig().getJoin().getTcpIpConfig().addMember(clusterMember); + } + } + } + <%_ } else { _%> + config.getNetworkConfig().setPort(5701); + config.getNetworkConfig().setPortAutoIncrement(true); + + // In development, remove multicast auto-configuration + if (env.acceptsProfiles(Profiles.of(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT))) { + System.setProperty("hazelcast.local.localAddress", "127.0.0.1"); + + config.getNetworkConfig().getJoin().getAwsConfig().setEnabled(false); + config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); + config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(false); + } + <%_ } _%> + config.setManagementCenterConfig(new ManagementCenterConfig()); + config.addMapConfig(initializeDefaultMapConfig(jHipsterProperties)); + <%_ if (enableHibernateCache) { _%> + config.addMapConfig(initializeDomainMapConfig(jHipsterProperties)); + <%_ } _%> + return config; + } + + private MapConfig initializeDefaultMapConfig(JHipsterProperties jHipsterProperties) { + MapConfig mapConfig = new MapConfig("default"); + + /* + Number of backups. If 1 is set as the backup-count for example, + then all entries of the map will be copied to another JVM for + fail-safety. Valid numbers are 0 (no backup), 1, 2, 3. + */ + mapConfig.setBackupCount(jHipsterProperties.getCache().getHazelcast().getBackupCount()); + + /* + Valid values are: + NONE (no eviction), + LRU (Least Recently Used), + LFU (Least Frequently Used). + NONE is the default. + */ + mapConfig.getEvictionConfig().setEvictionPolicy(EvictionPolicy.LRU); + + /* + Maximum size of the map. When max size is reached, + map is evicted based on the policy defined. + Any integer between 0 and Integer.MAX_VALUE. 0 means + Integer.MAX_VALUE. Default is 0. + */ + mapConfig.getEvictionConfig().setMaxSizePolicy(MaxSizePolicy.USED_HEAP_SIZE); + + return mapConfig; + } + + private MapConfig initializeDomainMapConfig(JHipsterProperties jHipsterProperties) { + MapConfig mapConfig = new MapConfig("<%= packageName %>.domain.*"); + mapConfig.setTimeToLiveSeconds(jHipsterProperties.getCache().getHazelcast().getTimeToLiveSeconds()); + return mapConfig; + } +<%_ } _%> } diff --git a/generators/micronaut/templates/build.gradle.ejs b/generators/micronaut/templates/build.gradle.ejs index 926f8e60..3e96466c 100644 --- a/generators/micronaut/templates/build.gradle.ejs +++ b/generators/micronaut/templates/build.gradle.ejs @@ -230,10 +230,10 @@ dependencies { implementation "com.hazelcast:hazelcast" <%_ } _%> <%_ if (cacheProvider === 'hazelcast' && enableHibernateCache) { _%> - implementation "com.hazelcast:hazelcast-hibernate53" + implementation "com.hazelcast:hazelcast-hibernate53:5.2.0" <%_ } _%> <%_ if (cacheProvider === 'hazelcast') { _%> - implementation "com.hazelcast:hazelcast-spring" + implementation "io.micronaut.cache:micronaut-cache-hazelcast" <%_ } _%> <%_ if (cacheProvider === 'infinispan') { _%> implementation "org.infinispan:infinispan-hibernate-cache-v53" diff --git a/generators/micronaut/templates/src/main/resources/application-dev.yml.ejs b/generators/micronaut/templates/src/main/resources/application-dev.yml.ejs index 4ca095eb..73e2036c 100644 --- a/generators/micronaut/templates/src/main/resources/application-dev.yml.ejs +++ b/generators/micronaut/templates/src/main/resources/application-dev.yml.ejs @@ -88,6 +88,12 @@ jpa: use_query_cache: false <%_ if (enableHibernateCache) { _%> use_second_level_cache: true + <%_ if (cacheProvider === 'hazelcast') { _%> + region.factory_class: com.hazelcast.hibernate.HazelcastCacheRegionFactory + use_minimal_puts: true + hazelcast.instance_name: <%= baseName %> + hazelcast.use_lite_member: true + <%_ } _%> <%_ } else { _%> use_second_level_cache: false <%_ } _%> diff --git a/generators/micronaut/templates/src/main/resources/application-prod.yml.ejs b/generators/micronaut/templates/src/main/resources/application-prod.yml.ejs index 5528e2b6..dc2e1617 100644 --- a/generators/micronaut/templates/src/main/resources/application-prod.yml.ejs +++ b/generators/micronaut/templates/src/main/resources/application-prod.yml.ejs @@ -84,6 +84,12 @@ jpa: use_query_cache: false <%_ if (enableHibernateCache) { _%> use_second_level_cache: true + <%_ if (cacheProvider === 'hazelcast') { _%> + region.factory_class: com.hazelcast.hibernate.HazelcastCacheRegionFactory + use_minimal_puts: true + hazelcast.instance_name: <%= baseName %> + hazelcast.use_lite_member: true + <%_ } _%> <%_ } else { _%> use_second_level_cache: false <%_ } _%> diff --git a/generators/micronaut/templates/src/main/resources/application.yml.ejs b/generators/micronaut/templates/src/main/resources/application.yml.ejs index da3179b6..a7a4a714 100644 --- a/generators/micronaut/templates/src/main/resources/application.yml.ejs +++ b/generators/micronaut/templates/src/main/resources/application.yml.ejs @@ -19,6 +19,11 @@ micronaut: <%_ if (serviceDiscoveryType === 'consul' || serviceDiscoveryType === 'eureka') { _%> instance: id: ${random.shortuuid} +<%_ } _%> +<%_ if (cacheProvider === 'hazelcast') { _%> + caches: + discovery-client: + enabled: false <%_ } _%> router: static-resources: From 18c5c0e34267fc30d88b1fd60f01c9ee12b5094a Mon Sep 17 00:00:00 2001 From: vcupelloni Date: Sat, 24 Aug 2024 12:10:14 +0200 Subject: [PATCH 2/7] Move import for monolith application --- .../main/java/_package_/config/CacheConfiguration.java.ejs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/generators/micronaut-cache/templates/src/main/java/_package_/config/CacheConfiguration.java.ejs b/generators/micronaut-cache/templates/src/main/java/_package_/config/CacheConfiguration.java.ejs index d1a0a2d3..ea0d47cd 100644 --- a/generators/micronaut-cache/templates/src/main/java/_package_/config/CacheConfiguration.java.ejs +++ b/generators/micronaut-cache/templates/src/main/java/_package_/config/CacheConfiguration.java.ejs @@ -46,8 +46,8 @@ import com.hazelcast.core.HazelcastInstance; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import tech.jhipster.config.JHipsterConstants; -<%_ if (serviceDiscoveryAny) { _%> import io.micronaut.context.env.Environment; +<%_ if (serviceDiscoveryAny) { _%> import io.micronaut.discovery.DiscoveryClient; import io.micronaut.discovery.ServiceInstance; import io.micronaut.runtime.server.EmbeddedServer; @@ -279,7 +279,7 @@ public class CacheConfiguration { config.getNetworkConfig().setPortAutoIncrement(true); // In development, remove multicast auto-configuration - if (env.acceptsProfiles(Profiles.of(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT))) { + if (env.getActiveNames().contains(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)) { System.setProperty("hazelcast.local.localAddress", "127.0.0.1"); config.getNetworkConfig().getJoin().getAwsConfig().setEnabled(false); From 0781ce5276a03873e98b7dfe5936472f5d9a5bad Mon Sep 17 00:00:00 2001 From: vcupelloni Date: Wed, 28 Aug 2024 18:00:15 +0200 Subject: [PATCH 3/7] Parameterized the creation of CacheConfiguration.java for Hazelcast support --- generators/micronaut-cache/generator.js | 2 +- .../CacheConfiguration_hazelcast.java.ejs | 190 ++++++++++++++++++ 2 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 generators/micronaut-cache/templates/src/main/java/_package_/config/CacheConfiguration_hazelcast.java.ejs diff --git a/generators/micronaut-cache/generator.js b/generators/micronaut-cache/generator.js index 1ad97662..42afe6cb 100644 --- a/generators/micronaut-cache/generator.js +++ b/generators/micronaut-cache/generator.js @@ -53,7 +53,7 @@ export default class extends BaseApplicationGenerator { files: [ { ...javaMainPackageTemplatesBlock(), - templates: ['config/CacheConfiguration.java'], + templates: [data => `config/CacheConfiguration${data.cacheProviderHazelcast ? '_hazelcast' : ''}.java`], }, ], }, diff --git a/generators/micronaut-cache/templates/src/main/java/_package_/config/CacheConfiguration_hazelcast.java.ejs b/generators/micronaut-cache/templates/src/main/java/_package_/config/CacheConfiguration_hazelcast.java.ejs new file mode 100644 index 00000000..a334319b --- /dev/null +++ b/generators/micronaut-cache/templates/src/main/java/_package_/config/CacheConfiguration_hazelcast.java.ejs @@ -0,0 +1,190 @@ +package <%=packageName%>.config; + +import java.util.Properties; + +import <%=packageName%>.util.JHipsterProperties; +import io.micronaut.context.ApplicationContext; +import io.micronaut.context.annotation.Context; +import io.micronaut.context.annotation.Factory; +import io.micronaut.context.annotation.Primary; +import io.micronaut.context.env.Environment; +<%_ if (serviceDiscoveryAny) { _%> +import io.micronaut.discovery.DiscoveryClient; +import io.micronaut.discovery.ServiceInstance; +import io.micronaut.runtime.server.EmbeddedServer; +<%_ } _%> + +import io.micronaut.cache.DynamicCacheManager; +import io.micronaut.cache.hazelcast.HazelcastCacheManager; +import io.micronaut.core.convert.ConversionService; +import io.micronaut.scheduling.TaskExecutors; +import io.reactivex.rxjava3.core.Flowable; + +import com.hazelcast.config.Config; +import com.hazelcast.config.EvictionPolicy; +import com.hazelcast.config.ManagementCenterConfig; +import com.hazelcast.config.MapConfig; +import com.hazelcast.config.MaxSizePolicy; +import com.hazelcast.core.Hazelcast; +import com.hazelcast.core.HazelcastInstance; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import tech.jhipster.config.JHipsterConstants; +import jakarta.annotation.PreDestroy; +import jakarta.inject.Named; +import jakarta.inject.Singleton; +import java.util.List; +import java.util.concurrent.ExecutorService; +import javax.cache.CacheManager; + +@Factory +public class CacheConfiguration { + + private final Logger log = LoggerFactory.getLogger(CacheConfiguration.class); + + private final Environment env; + + private final JHipsterProperties jHipsterProperties; + + <%_ if (serviceDiscoveryAny) { _%> + private final EmbeddedServer embeddedServer; + + private final DiscoveryClient discoveryClient; + <%_ } _%> + + public CacheConfiguration(Environment env<% if (serviceDiscoveryAny) { %>, EmbeddedServer embeddedServer, DiscoveryClient discoveryClient<%_ } _%>, JHipsterProperties jHipsterProperties) { + this.env = env; + <% if (serviceDiscoveryAny) { %> + this.embeddedServer = embeddedServer; + this.discoveryClient = discoveryClient; + <%_ } _%> + this.jHipsterProperties = jHipsterProperties; + } + + @Singleton + @Context + @Primary + public HazelcastInstance hazelcastInstance(Config hazelcastConfig) { + return Hazelcast.newHazelcastInstance(hazelcastConfig); + } + + @Singleton + public DynamicCacheManager cacheManager(HazelcastInstance hazelcastInstance, + @Named(TaskExecutors.IO) ExecutorService executorService) { + log.debug("Starting HazelcastCacheManager"); + return new HazelcastCacheManager(ConversionService.SHARED, hazelcastInstance, executorService); + } + + @PreDestroy + public void destroy() { + log.info("Closing Cache Manager"); + Hazelcast.shutdownAll(); + } + + @Singleton + public Config hazelcastConfig() { + log.debug("Configuring Hazelcast"); + HazelcastInstance hazelCastInstance = Hazelcast.getHazelcastInstanceByName("<%= baseName %>"); + if (hazelCastInstance != null) { + log.debug("Hazelcast already initialized"); + return hazelCastInstance.getConfig(); + } + + Config config = new Config(); + config.setInstanceName("<%= baseName %>"); + <%_ if (serviceDiscoveryAny) { _%> + config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); + + if (this.discoveryClient == null) { + log.warn("No discovery service is set up, Hazelcast cannot create a cluster."); + } else { + + String serviceId = env.getProperty("micronaut.application.name", String.class).orElse("<%= baseName %>"); + log.debug("Configuring Hazelcast clustering for instanceId: {}", serviceId); + + // In development, everything goes through 127.0.0.1, with a different port + if (env.getActiveNames().contains(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)) { + log.debug( + "Application is running with the \"dev\" profile, Hazelcast " + "cluster will only work with localhost instances" + ); + + config.getNetworkConfig().setPort(embeddedServer.getPort() + 5701); + config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true); + + List instances = Flowable.fromPublisher(discoveryClient.getInstances(serviceId)).blockingFirst(); + for (ServiceInstance instance : instances) { + String clusterMember = "127.0.0.1:" + (instance.getPort() + 5701); + log.debug("Adding Hazelcast (dev) cluster member {}", clusterMember); + config.getNetworkConfig().getJoin().getTcpIpConfig().addMember(clusterMember); + } + + } else { // Production configuration, one host per instance all using port 5701 + config.getNetworkConfig().setPort(5701); + config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true); + + List instances = Flowable.fromPublisher(discoveryClient.getInstances(serviceId)).blockingFirst(); + for (ServiceInstance instance : instances) { + String clusterMember = instance.getHost() + ":5701"; + log.debug("Adding Hazelcast (prod) cluster member {}", clusterMember); + config.getNetworkConfig().getJoin().getTcpIpConfig().addMember(clusterMember); + } + } + } + <%_ } else { _%> + config.getNetworkConfig().setPort(5701); + config.getNetworkConfig().setPortAutoIncrement(true); + + // In development, remove multicast auto-configuration + if (env.getActiveNames().contains(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)) { + System.setProperty("hazelcast.local.localAddress", "127.0.0.1"); + + config.getNetworkConfig().getJoin().getAwsConfig().setEnabled(false); + config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); + config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(false); + } + <%_ } _%> + config.setManagementCenterConfig(new ManagementCenterConfig()); + config.addMapConfig(initializeDefaultMapConfig(jHipsterProperties)); + <%_ if (enableHibernateCache) { _%> + config.addMapConfig(initializeDomainMapConfig(jHipsterProperties)); + <%_ } _%> + return config; + } + + private MapConfig initializeDefaultMapConfig(JHipsterProperties jHipsterProperties) { + MapConfig mapConfig = new MapConfig("default"); + + /* + Number of backups. If 1 is set as the backup-count for example, + then all entries of the map will be copied to another JVM for + fail-safety. Valid numbers are 0 (no backup), 1, 2, 3. + */ + mapConfig.setBackupCount(jHipsterProperties.getCache().getHazelcast().getBackupCount()); + + /* + Valid values are: + NONE (no eviction), + LRU (Least Recently Used), + LFU (Least Frequently Used). + NONE is the default. + */ + mapConfig.getEvictionConfig().setEvictionPolicy(EvictionPolicy.LRU); + + /* + Maximum size of the map. When max size is reached, + map is evicted based on the policy defined. + Any integer between 0 and Integer.MAX_VALUE. 0 means + Integer.MAX_VALUE. Default is 0. + */ + mapConfig.getEvictionConfig().setMaxSizePolicy(MaxSizePolicy.USED_HEAP_SIZE); + + return mapConfig; + } + + private MapConfig initializeDomainMapConfig(JHipsterProperties jHipsterProperties) { + MapConfig mapConfig = new MapConfig("<%= packageName %>.domain.*"); + mapConfig.setTimeToLiveSeconds(jHipsterProperties.getCache().getHazelcast().getTimeToLiveSeconds()); + return mapConfig; + } +} From 47707e05cf2bb3911b4d31bead612b66dafd36f2 Mon Sep 17 00:00:00 2001 From: vcupelloni Date: Thu, 29 Aug 2024 00:33:01 +0200 Subject: [PATCH 4/7] Restore previous version --- .../config/CacheConfiguration.java.ejs | 188 +----------------- 1 file changed, 1 insertion(+), 187 deletions(-) diff --git a/generators/micronaut-cache/templates/src/main/java/_package_/config/CacheConfiguration.java.ejs b/generators/micronaut-cache/templates/src/main/java/_package_/config/CacheConfiguration.java.ejs index ea0d47cd..4eaa4a57 100644 --- a/generators/micronaut-cache/templates/src/main/java/_package_/config/CacheConfiguration.java.ejs +++ b/generators/micronaut-cache/templates/src/main/java/_package_/config/CacheConfiguration.java.ejs @@ -4,9 +4,7 @@ import java.util.Properties; import <%=packageName%>.util.JHipsterProperties; import io.micronaut.context.ApplicationContext; -import io.micronaut.context.annotation.Context; import io.micronaut.context.annotation.Factory; -import io.micronaut.context.annotation.Primary; <%_ if (cacheProvider === 'ehcache') { _%> import java.time.Duration; @@ -28,31 +26,7 @@ import org.redisson.config.Config; import org.redisson.config.SingleServerConfig; import org.redisson.jcache.configuration.RedissonConfiguration; <%_ } _%> -<%_ if (cacheProvider === 'hazelcast') { _%> -import io.micronaut.cache.DynamicCacheManager; -import io.micronaut.cache.hazelcast.HazelcastCacheManager; -import io.micronaut.core.convert.ConversionService; -import io.micronaut.scheduling.TaskExecutors; -import io.reactivex.rxjava3.core.Flowable; -import com.hazelcast.config.Config; -import com.hazelcast.config.EvictionPolicy; -import com.hazelcast.config.ManagementCenterConfig; -import com.hazelcast.config.MapConfig; -import com.hazelcast.config.MaxSizePolicy; -import com.hazelcast.core.Hazelcast; -import com.hazelcast.core.HazelcastInstance; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import tech.jhipster.config.JHipsterConstants; -import io.micronaut.context.env.Environment; -<%_ if (serviceDiscoveryAny) { _%> -import io.micronaut.discovery.DiscoveryClient; -import io.micronaut.discovery.ServiceInstance; -import io.micronaut.runtime.server.EmbeddedServer; -<%_ } _%> -<%_ } _%> import javax.cache.CacheManager; <%_ if (cacheProvider === 'redis') { _%> import javax.cache.Caching; @@ -60,46 +34,14 @@ import javax.cache.configuration.MutableConfiguration; import javax.cache.expiry.CreatedExpiryPolicy; import javax.cache.expiry.Duration; <%_ } _%> -<%_ if (cacheProvider === 'hazelcast') { _%> -import jakarta.annotation.PreDestroy; -import jakarta.inject.Named; -<%_ } _%> import jakarta.inject.Singleton; <%_ if (cacheProvider === 'redis') { _%> import java.util.concurrent.TimeUnit; <%_ } _%> -<%_ if (cacheProvider === 'hazelcast') { _%> -import java.util.List; -import java.util.concurrent.ExecutorService; -<%_ } _%> @Factory public class CacheConfiguration { -<%_ if (cacheProvider === 'hazelcast') { _%> - - private final Logger log = LoggerFactory.getLogger(CacheConfiguration.class); - - private final Environment env; - - private final JHipsterProperties jHipsterProperties; - - <%_ if (serviceDiscoveryAny) { _%> - private final EmbeddedServer embeddedServer; - - private final DiscoveryClient discoveryClient; - <%_ } _%> - - public CacheConfiguration(Environment env<% if (serviceDiscoveryAny) { %>, EmbeddedServer embeddedServer, DiscoveryClient discoveryClient<%_ } _%>, JHipsterProperties jHipsterProperties) { - this.env = env; - <% if (serviceDiscoveryAny) { %> - this.embeddedServer = embeddedServer; - this.discoveryClient = discoveryClient; - <%_ } _%> - this.jHipsterProperties = jHipsterProperties; - } -<%_ } _%> -<%_ if (cacheProvider === 'ehcache' || cacheProvider === 'caffeine' || cacheProvider === 'redis') { _%> private final javax.cache.configuration.Configuration jcacheConfiguration; public CacheConfiguration(JHipsterProperties jHipsterProperties) { @@ -203,132 +145,4 @@ public class CacheConfiguration { } cm.createCache(cacheName, jcacheConfiguration); } -<%_ } _%> -<%_ if (cacheProvider === 'hazelcast') { _%> - @Singleton - @Context - @Primary - public HazelcastInstance hazelcastInstance(Config hazelcastConfig) { - return Hazelcast.newHazelcastInstance(hazelcastConfig); - } - - @Singleton - public DynamicCacheManager cacheManager(HazelcastInstance hazelcastInstance, - @Named(TaskExecutors.IO) ExecutorService executorService) { - log.debug("Starting HazelcastCacheManager"); - return new HazelcastCacheManager(ConversionService.SHARED, hazelcastInstance, executorService); - } - - @PreDestroy - public void destroy() { - log.info("Closing Cache Manager"); - Hazelcast.shutdownAll(); - } - - @Singleton - public Config hazelcastConfig() { - log.debug("Configuring Hazelcast"); - HazelcastInstance hazelCastInstance = Hazelcast.getHazelcastInstanceByName("<%= baseName %>"); - if (hazelCastInstance != null) { - log.debug("Hazelcast already initialized"); - return hazelCastInstance.getConfig(); - } - - Config config = new Config(); - config.setInstanceName("<%= baseName %>"); - <%_ if (serviceDiscoveryAny) { _%> - config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); - - if (this.discoveryClient == null) { - log.warn("No discovery service is set up, Hazelcast cannot create a cluster."); - } else { - - String serviceId = env.getProperty("micronaut.application.name", String.class).orElse("<%= baseName %>"); - log.debug("Configuring Hazelcast clustering for instanceId: {}", serviceId); - - // In development, everything goes through 127.0.0.1, with a different port - if (env.getActiveNames().contains(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)) { - log.debug( - "Application is running with the \"dev\" profile, Hazelcast " + "cluster will only work with localhost instances" - ); - - config.getNetworkConfig().setPort(embeddedServer.getPort() + 5701); - config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true); - - List instances = Flowable.fromPublisher(discoveryClient.getInstances(serviceId)).blockingFirst(); - for (ServiceInstance instance : instances) { - String clusterMember = "127.0.0.1:" + (instance.getPort() + 5701); - log.debug("Adding Hazelcast (dev) cluster member {}", clusterMember); - config.getNetworkConfig().getJoin().getTcpIpConfig().addMember(clusterMember); - } - - } else { // Production configuration, one host per instance all using port 5701 - config.getNetworkConfig().setPort(5701); - config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true); - - List instances = Flowable.fromPublisher(discoveryClient.getInstances(serviceId)).blockingFirst(); - for (ServiceInstance instance : instances) { - String clusterMember = instance.getHost() + ":5701"; - log.debug("Adding Hazelcast (prod) cluster member {}", clusterMember); - config.getNetworkConfig().getJoin().getTcpIpConfig().addMember(clusterMember); - } - } - } - <%_ } else { _%> - config.getNetworkConfig().setPort(5701); - config.getNetworkConfig().setPortAutoIncrement(true); - - // In development, remove multicast auto-configuration - if (env.getActiveNames().contains(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)) { - System.setProperty("hazelcast.local.localAddress", "127.0.0.1"); - - config.getNetworkConfig().getJoin().getAwsConfig().setEnabled(false); - config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); - config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(false); - } - <%_ } _%> - config.setManagementCenterConfig(new ManagementCenterConfig()); - config.addMapConfig(initializeDefaultMapConfig(jHipsterProperties)); - <%_ if (enableHibernateCache) { _%> - config.addMapConfig(initializeDomainMapConfig(jHipsterProperties)); - <%_ } _%> - return config; - } - - private MapConfig initializeDefaultMapConfig(JHipsterProperties jHipsterProperties) { - MapConfig mapConfig = new MapConfig("default"); - - /* - Number of backups. If 1 is set as the backup-count for example, - then all entries of the map will be copied to another JVM for - fail-safety. Valid numbers are 0 (no backup), 1, 2, 3. - */ - mapConfig.setBackupCount(jHipsterProperties.getCache().getHazelcast().getBackupCount()); - - /* - Valid values are: - NONE (no eviction), - LRU (Least Recently Used), - LFU (Least Frequently Used). - NONE is the default. - */ - mapConfig.getEvictionConfig().setEvictionPolicy(EvictionPolicy.LRU); - - /* - Maximum size of the map. When max size is reached, - map is evicted based on the policy defined. - Any integer between 0 and Integer.MAX_VALUE. 0 means - Integer.MAX_VALUE. Default is 0. - */ - mapConfig.getEvictionConfig().setMaxSizePolicy(MaxSizePolicy.USED_HEAP_SIZE); - - return mapConfig; - } - - private MapConfig initializeDomainMapConfig(JHipsterProperties jHipsterProperties) { - MapConfig mapConfig = new MapConfig("<%= packageName %>.domain.*"); - mapConfig.setTimeToLiveSeconds(jHipsterProperties.getCache().getHazelcast().getTimeToLiveSeconds()); - return mapConfig; - } -<%_ } _%> -} +} \ No newline at end of file From 524fb55636e6bcda2410cbf94049842c79783ab3 Mon Sep 17 00:00:00 2001 From: vcupelloni Date: Thu, 12 Sep 2024 09:20:51 +0200 Subject: [PATCH 5/7] Moved hazelcast-hibernate53 version in libs.versions.toml --- generators/micronaut/resources/gradle/libs.versions.toml | 3 +++ generators/micronaut/templates/build.gradle.ejs | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/generators/micronaut/resources/gradle/libs.versions.toml b/generators/micronaut/resources/gradle/libs.versions.toml index bce287d9..f0cf2620 100644 --- a/generators/micronaut/resources/gradle/libs.versions.toml +++ b/generators/micronaut/resources/gradle/libs.versions.toml @@ -29,3 +29,6 @@ gradle-git-properties = { id = 'com.gorylenko.gradle-git-properties', version = openapi-generator = { id = 'org.openapi.generator', version = '7.8.0' } shadow = { id = 'com.github.johnrengelman.shadow', version = '8.1.1' } + +# hazelcast-hibernate +hazelcast-hibernate53 = { id = 'com.hazelcast:hazelcast-hibernate53', version = '5.2.0' } \ No newline at end of file diff --git a/generators/micronaut/templates/build.gradle.ejs b/generators/micronaut/templates/build.gradle.ejs index 3e96466c..f459d6f9 100644 --- a/generators/micronaut/templates/build.gradle.ejs +++ b/generators/micronaut/templates/build.gradle.ejs @@ -230,7 +230,7 @@ dependencies { implementation "com.hazelcast:hazelcast" <%_ } _%> <%_ if (cacheProvider === 'hazelcast' && enableHibernateCache) { _%> - implementation "com.hazelcast:hazelcast-hibernate53:5.2.0" + implementation "com.hazelcast:hazelcast-hibernate53:<%- javaDependencies['hazelcast-hibernate53'] %>" <%_ } _%> <%_ if (cacheProvider === 'hazelcast') { _%> implementation "io.micronaut.cache:micronaut-cache-hazelcast" From 7fd6890a13301246bba047d50ccbfad34cb3be5c Mon Sep 17 00:00:00 2001 From: vcupelloni Date: Thu, 12 Sep 2024 10:36:11 +0200 Subject: [PATCH 6/7] Added dependency injection for maven --- .../micronaut-cache/internal/dependencies.mjs | 14 ++++++++++---- .../micronaut/resources/gradle/libs.versions.toml | 6 +++++- .../micronaut/resources/micronaut-platform.pom | 8 +++++++- generators/micronaut/templates/build.gradle.ejs | 4 +--- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/generators/micronaut-cache/internal/dependencies.mjs b/generators/micronaut-cache/internal/dependencies.mjs index bd08d1c4..acf32d43 100644 --- a/generators/micronaut-cache/internal/dependencies.mjs +++ b/generators/micronaut-cache/internal/dependencies.mjs @@ -132,17 +132,23 @@ export const getCacheProviderMavenDefinition = (cacheProvider, javaDependencies) base: { properties: [ { - property: 'hazelcast-spring.version', - value: javaDependencies['hazelcast-spring'], + property: 'hazelcast.version', + value: javaDependencies['hazelcast'], }, ], dependencies: [ javaxCacheApi, { groupId: 'com.hazelcast', - artifactId: 'hazelcast-spring', + artifactId: 'hazelcast', - version: '${hazelcast-spring.version}', + version: '${hazelcast.version}', + }, + { + groupId: 'io.micronaut.cache', + artifactId: 'micronaut-cache-hazelcast', + + version: '${micronaut.cache.version}', }, ], }, diff --git a/generators/micronaut/resources/gradle/libs.versions.toml b/generators/micronaut/resources/gradle/libs.versions.toml index f0cf2620..b2a1aec2 100644 --- a/generators/micronaut/resources/gradle/libs.versions.toml +++ b/generators/micronaut/resources/gradle/libs.versions.toml @@ -30,5 +30,9 @@ openapi-generator = { id = 'org.openapi.generator', version = '7.8.0' } shadow = { id = 'com.github.johnrengelman.shadow', version = '8.1.1' } +# hazelcast +hazelcast = { id = 'com.hazelcast:hazelcast', version = '5.4.0' } # hazelcast-hibernate -hazelcast-hibernate53 = { id = 'com.hazelcast:hazelcast-hibernate53', version = '5.2.0' } \ No newline at end of file +hazelcast-hibernate53 = { id = 'com.hazelcast:hazelcast-hibernate53', version = '5.2.0' } +# micronaut-cache-hazelcast +micronaut-cache-hazelcast = { id = 'io.micronaut.cache:micronaut-cache-hazelcast', version = '5.0.0' } \ No newline at end of file diff --git a/generators/micronaut/resources/micronaut-platform.pom b/generators/micronaut/resources/micronaut-platform.pom index d74654d0..d881eb2e 100644 --- a/generators/micronaut/resources/micronaut-platform.pom +++ b/generators/micronaut/resources/micronaut-platform.pom @@ -86,7 +86,8 @@ 2.3.232 3.0 4.3.1 - 5.3.7 + 5.4.0 + 5.2.0 2.3.1.Final 6.5.2.Final 5.1.0 @@ -664,6 +665,11 @@ hazelcast ${hazelcast.version} + + com.hazelcast + hazelcast-hibernate53 + ${hazelcast-hibernate.version} + org.infinispan infinispan-client-hotrod diff --git a/generators/micronaut/templates/build.gradle.ejs b/generators/micronaut/templates/build.gradle.ejs index f459d6f9..4e70152d 100644 --- a/generators/micronaut/templates/build.gradle.ejs +++ b/generators/micronaut/templates/build.gradle.ejs @@ -228,13 +228,11 @@ dependencies { <%_ } _%> <%_ if (cacheProvider === 'hazelcast') { _%> implementation "com.hazelcast:hazelcast" + implementation "io.micronaut.cache:micronaut-cache-hazelcast" <%_ } _%> <%_ if (cacheProvider === 'hazelcast' && enableHibernateCache) { _%> implementation "com.hazelcast:hazelcast-hibernate53:<%- javaDependencies['hazelcast-hibernate53'] %>" <%_ } _%> - <%_ if (cacheProvider === 'hazelcast') { _%> - implementation "io.micronaut.cache:micronaut-cache-hazelcast" - <%_ } _%> <%_ if (cacheProvider === 'infinispan') { _%> implementation "org.infinispan:infinispan-hibernate-cache-v53" implementation "org.infinispan:infinispan-spring-boot-starter-embedded" From 58d80fb25cffa3ffb34a13ada8c01f2a973b805f Mon Sep 17 00:00:00 2001 From: vcupelloni Date: Thu, 12 Sep 2024 12:52:49 +0200 Subject: [PATCH 7/7] Added dependency injection for gradle --- generators/micronaut-cache/generator.js | 23 ++++++-- .../micronaut-cache/internal/dependencies.mjs | 57 ++++++++++++++++++- .../resources/gradle/libs.versions.toml | 4 +- .../micronaut/templates/build.gradle.ejs | 7 --- .../micronaut/templates/gradle.properties.ejs | 7 +++ 5 files changed, 80 insertions(+), 18 deletions(-) diff --git a/generators/micronaut-cache/generator.js b/generators/micronaut-cache/generator.js index 42afe6cb..61d37d4d 100644 --- a/generators/micronaut-cache/generator.js +++ b/generators/micronaut-cache/generator.js @@ -2,7 +2,7 @@ import { GENERATOR_BOOTSTRAP_APPLICATION } from 'generator-jhipster/generators'; import BaseApplicationGenerator from 'generator-jhipster/generators/base-application'; import { createNeedleCallback } from 'generator-jhipster/generators/base/support'; import { javaMainPackageTemplatesBlock } from 'generator-jhipster/generators/java/support'; -import { getCacheProviderMavenDefinition } from './internal/dependencies.mjs'; +import { getCacheProviderMavenDefinition, getCacheProviderGradleDefinition } from './internal/dependencies.mjs'; export default class extends BaseApplicationGenerator { constructor(args, opts, features) { @@ -66,10 +66,23 @@ export default class extends BaseApplicationGenerator { get [BaseApplicationGenerator.POST_WRITING_ENTITIES]() { return this.asPostWritingTaskGroup({ addDependencies({ application, source }) { - const definition = getCacheProviderMavenDefinition(application.cacheProvider, application.javaDependencies); - source.addMavenDefinition?.(definition.base); - if (application.enableHibernateCache && definition.hibernateCache) { - source.addMavenDefinition?.(definition.hibernateCache); + + if(application.buildToolMaven){ + const definition = getCacheProviderMavenDefinition(application.cacheProvider, application.javaDependencies); + + source.addMavenDefinition?.(definition.base); + if (application.enableHibernateCache && definition.hibernateCache) { + source.addMavenDefinition?.(definition.hibernateCache); + } + }else if(application.buildToolGradle){ + const definition = getCacheProviderGradleDefinition(application.cacheProvider, application.javaDependencies); + + if(definition){ + source.addGradleDependencies?.(definition.base.dependencies); + if (application.enableHibernateCache && definition.hibernateCache) { + source.addGradleDependencies?.(definition.hibernateCache.dependencies); + } + } } }, customizeFiles({ source, entities, application: { cacheProvider, enableHibernateCache } }) { diff --git a/generators/micronaut-cache/internal/dependencies.mjs b/generators/micronaut-cache/internal/dependencies.mjs index acf32d43..8c64c00d 100644 --- a/generators/micronaut-cache/internal/dependencies.mjs +++ b/generators/micronaut-cache/internal/dependencies.mjs @@ -155,8 +155,8 @@ export const getCacheProviderMavenDefinition = (cacheProvider, javaDependencies) hibernateCache: { properties: [ { - property: 'hazelcast-hibernate53.version', - value: javaDependencies['hazelcast-hibernate53'], + property: 'hazelcast-hibernate.version', + value: javaDependencies['hazelcast-hibernate'], }, ], dependencies: [ @@ -164,7 +164,7 @@ export const getCacheProviderMavenDefinition = (cacheProvider, javaDependencies) groupId: 'com.hazelcast', artifactId: 'hazelcast-hibernate53', - version: '${hazelcast-hibernate53.version}', + version: '${hazelcast-hibernate.version}', }, ], }, @@ -219,3 +219,54 @@ export const getCacheProviderMavenDefinition = (cacheProvider, javaDependencies) }; return dependenciesForCache[cacheProvider]; }; + +export const getCacheProviderGradleDefinition = (cacheProvider, javaDependencies) => { + const dependenciesForCache = { + hazelcast: { + base: { + properties: [ + { + property: 'hazelcast.version', + value: javaDependencies['hazelcast'], + }, + ], + dependencies: [ + javaxCacheApi, + { + groupId: 'com.hazelcast', + artifactId: 'hazelcast', + scope: 'implementation', + + version: '${hazelcast_version}', + }, + { + groupId: 'io.micronaut.cache', + artifactId: 'micronaut-cache-hazelcast', + scope: 'implementation', + + version: '${micronaut_cache_version}', + }, + ], + }, + hibernateCache: { + properties: [ + { + property: 'hazelcast-hibernate.version', + value: javaDependencies['hazelcast-hibernate'], + }, + ], + dependencies: [ + { + groupId: 'com.hazelcast', + artifactId: 'hazelcast-hibernate53', + scope: 'implementation', + + version: '${hazelcast_hibernate_version}', + }, + ], + }, + }, + }; + + return dependenciesForCache[cacheProvider]; +}; diff --git a/generators/micronaut/resources/gradle/libs.versions.toml b/generators/micronaut/resources/gradle/libs.versions.toml index b2a1aec2..bf54b4f1 100644 --- a/generators/micronaut/resources/gradle/libs.versions.toml +++ b/generators/micronaut/resources/gradle/libs.versions.toml @@ -33,6 +33,4 @@ shadow = { id = 'com.github.johnrengelman.shadow', version = '8.1.1' } # hazelcast hazelcast = { id = 'com.hazelcast:hazelcast', version = '5.4.0' } # hazelcast-hibernate -hazelcast-hibernate53 = { id = 'com.hazelcast:hazelcast-hibernate53', version = '5.2.0' } -# micronaut-cache-hazelcast -micronaut-cache-hazelcast = { id = 'io.micronaut.cache:micronaut-cache-hazelcast', version = '5.0.0' } \ No newline at end of file +hazelcast-hibernate = { id = 'com.hazelcast:hazelcast-hibernate53', version = '5.2.0' } \ No newline at end of file diff --git a/generators/micronaut/templates/build.gradle.ejs b/generators/micronaut/templates/build.gradle.ejs index 4e70152d..abacc80e 100644 --- a/generators/micronaut/templates/build.gradle.ejs +++ b/generators/micronaut/templates/build.gradle.ejs @@ -226,13 +226,6 @@ dependencies { <%_ if (applicationTypeGateway) { _%> implementation "org.apache.httpcomponents:httpclient" <%_ } _%> - <%_ if (cacheProvider === 'hazelcast') { _%> - implementation "com.hazelcast:hazelcast" - implementation "io.micronaut.cache:micronaut-cache-hazelcast" - <%_ } _%> - <%_ if (cacheProvider === 'hazelcast' && enableHibernateCache) { _%> - implementation "com.hazelcast:hazelcast-hibernate53:<%- javaDependencies['hazelcast-hibernate53'] %>" - <%_ } _%> <%_ if (cacheProvider === 'infinispan') { _%> implementation "org.infinispan:infinispan-hibernate-cache-v53" implementation "org.infinispan:infinispan-spring-boot-starter-embedded" diff --git a/generators/micronaut/templates/gradle.properties.ejs b/generators/micronaut/templates/gradle.properties.ejs index c15c31cb..8a479dbf 100644 --- a/generators/micronaut/templates/gradle.properties.ejs +++ b/generators/micronaut/templates/gradle.properties.ejs @@ -39,6 +39,13 @@ liquibaseTaskPrefix=liquibase jaxb_runtime_version=<%= javaDependencies['jaxb-runtime'] %> <%_ } _%> +<%_ if (cacheProvider === 'hazelcast') { _%> +hazelcast_version=<%= javaManagedProperties['hazelcast.version'] %> +micronaut_cache_version=<%= javaManagedProperties['micronaut.cache.version'] %> + <%_ if (enableHibernateCache) { _%> +hazelcast_hibernate_version=<%= javaManagedProperties['hazelcast-hibernate.version'] %> + <%_ } _%> +<%_ } _%> # jhipster-needle-gradle-property - JHipster will add additional properties here ## below are some of the gradle performance improvement settings that can be used as required, these are not enabled by default