-
-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix code generation for Hazelcast cache provider #387
base: main
Are you sure you want to change the base?
Changes from all commits
617599f
18c5c0e
0781ce5
47707e0
f37ec17
7886958
524fb55
7fd6890
58d80fb
8ddb78f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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) { | ||||||||||||||||||||
|
@@ -53,7 +53,7 @@ export default class extends BaseApplicationGenerator { | |||||||||||||||||||
files: [ | ||||||||||||||||||||
{ | ||||||||||||||||||||
...javaMainPackageTemplatesBlock(), | ||||||||||||||||||||
templates: ['config/CacheConfiguration.java'], | ||||||||||||||||||||
templates: [data => `config/CacheConfiguration${data.cacheProviderHazelcast ? '_hazelcast' : ''}.java`], | ||||||||||||||||||||
}, | ||||||||||||||||||||
], | ||||||||||||||||||||
}, | ||||||||||||||||||||
|
@@ -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); | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
Comment on lines
+77
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
} | ||||||||||||||||||||
}, | ||||||||||||||||||||
customizeFiles({ source, entities, application: { cacheProvider, enableHibernateCache } }) { | ||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -132,33 +132,39 @@ 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}', | ||||||||
}, | ||||||||
], | ||||||||
}, | ||||||||
hibernateCache: { | ||||||||
properties: [ | ||||||||
{ | ||||||||
property: 'hazelcast-hibernate53.version', | ||||||||
value: javaDependencies['hazelcast-hibernate53'], | ||||||||
property: 'hazelcast-hibernate.version', | ||||||||
value: javaDependencies['hazelcast-hibernate'], | ||||||||
}, | ||||||||
], | ||||||||
dependencies: [ | ||||||||
{ | ||||||||
groupId: 'com.hazelcast', | ||||||||
artifactId: 'hazelcast-hibernate53', | ||||||||
|
||||||||
version: '${hazelcast-hibernate53.version}', | ||||||||
version: '${hazelcast-hibernate.version}', | ||||||||
}, | ||||||||
], | ||||||||
}, | ||||||||
|
@@ -213,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}', | ||||||||
Comment on lines
+239
to
+240
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
}, | ||||||||
{ | ||||||||
groupId: 'io.micronaut.cache', | ||||||||
artifactId: 'micronaut-cache-hazelcast', | ||||||||
scope: 'implementation', | ||||||||
|
||||||||
version: '${micronaut_cache_version}', | ||||||||
Comment on lines
+246
to
+247
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inherited by the micronaut-platform bom.
Suggested change
|
||||||||
}, | ||||||||
], | ||||||||
}, | ||||||||
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]; | ||||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,4 +145,4 @@ public class CacheConfiguration { | |
} | ||
cm.createCache(cacheName, jcacheConfiguration); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ServiceInstance> 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<ServiceInstance> 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; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.