Search in sources :

Example 1 with DefaultAwsRegionProviderChain

use of software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain in project para-cache-hazelcast by Erudika.

the class HazelcastUtils method getClient.

/**
 * Initializes a new Hazelcast instance with default settings.
 * @return a Hazelcast instance
 */
public static HazelcastInstance getClient() {
    if (hcInstance == null) {
        hcInstance = Hazelcast.getHazelcastInstanceByName(getNodeName());
        if (hcInstance != null) {
            return hcInstance;
        }
        com.hazelcast.config.Config cfg = new com.hazelcast.config.Config();
        cfg.setInstanceName(getNodeName());
        cfg.setClusterName(Para.getConfig().clusterName());
        MapConfig mapcfg = new MapConfig("default");
        mapcfg.setEvictionConfig(getEvictionPolicy());
        mapcfg.setTimeToLiveSeconds(Para.getConfig().getConfigInt("hc.ttl_seconds", 3600));
        mapcfg.setMaxIdleSeconds(mapcfg.getTimeToLiveSeconds() * 2);
        // mapcfg.setMapStoreConfig(new MapStoreConfig().setEnabled(false).setClassName(NODE_NAME));
        cfg.addMapConfig(mapcfg);
        cfg.setProperty("hazelcast.jmx", Boolean.toString(isJMXOn()));
        cfg.setProperty("hazelcast.logging.type", "slf4j");
        cfg.setProperty("hazelcast.health.monitoring.level", "SILENT");
        if (Para.getConfig().inProduction() && Para.getConfig().getConfigBoolean("hc.ec2_discovery_enabled", true)) {
            cfg.setProperty("hazelcast.discovery.enabled", "true");
            cfg.setProperty("hazelcast.discovery.public.ip.enabled", "true");
            Map<String, Comparable> awsConfig = new HashMap<>();
            awsConfig.put("access-key", Para.getConfig().getConfigParam("aws_access_key", System.getenv("AWS_ACCESS_KEY_ID")));
            awsConfig.put("secret-key", Para.getConfig().getConfigParam("aws_secret_key", System.getenv("AWS_SECRET_ACCESS_KEY")));
            awsConfig.put("region", new DefaultAwsRegionProviderChain().getRegion().id());
            awsConfig.put("host-header", "ec2.amazonaws.com");
            awsConfig.put("security-group-name", Para.getConfig().getConfigParam("hc.discovery_group", "hazelcast"));
            DiscoveryConfig ec2DiscoveryConfig = new DiscoveryConfig();
            ec2DiscoveryConfig.addDiscoveryStrategyConfig(new DiscoveryStrategyConfig("com.hazelcast.aws.AwsDiscoveryStrategy", awsConfig));
            cfg.setNetworkConfig(new NetworkConfig().setJoin(new JoinConfig().setMulticastConfig(new MulticastConfig().setEnabled(false)).setTcpIpConfig(new TcpIpConfig().setEnabled(false)).setAwsConfig(new AwsConfig().setEnabled(false)).setDiscoveryConfig(ec2DiscoveryConfig)));
        }
        hcInstance = Hazelcast.newHazelcastInstance(cfg);
        Para.addDestroyListener(new DestroyListener() {

            public void onDestroy() {
                shutdownClient();
            }
        });
    }
    return hcInstance;
}
Also used : DefaultAwsRegionProviderChain(software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain) AwsConfig(com.hazelcast.config.AwsConfig) HashMap(java.util.HashMap) DiscoveryStrategyConfig(com.hazelcast.config.DiscoveryStrategyConfig) DiscoveryStrategyConfig(com.hazelcast.config.DiscoveryStrategyConfig) MulticastConfig(com.hazelcast.config.MulticastConfig) AwsConfig(com.hazelcast.config.AwsConfig) DiscoveryConfig(com.hazelcast.config.DiscoveryConfig) MapConfig(com.hazelcast.config.MapConfig) NetworkConfig(com.hazelcast.config.NetworkConfig) TcpIpConfig(com.hazelcast.config.TcpIpConfig) JoinConfig(com.hazelcast.config.JoinConfig) EvictionConfig(com.hazelcast.config.EvictionConfig) Config(com.erudika.para.core.utils.Config) NetworkConfig(com.hazelcast.config.NetworkConfig) DestroyListener(com.erudika.para.core.listeners.DestroyListener) JoinConfig(com.hazelcast.config.JoinConfig) MulticastConfig(com.hazelcast.config.MulticastConfig) MapConfig(com.hazelcast.config.MapConfig) DiscoveryConfig(com.hazelcast.config.DiscoveryConfig) TcpIpConfig(com.hazelcast.config.TcpIpConfig)

Aggregations

DestroyListener (com.erudika.para.core.listeners.DestroyListener)1 Config (com.erudika.para.core.utils.Config)1 AwsConfig (com.hazelcast.config.AwsConfig)1 DiscoveryConfig (com.hazelcast.config.DiscoveryConfig)1 DiscoveryStrategyConfig (com.hazelcast.config.DiscoveryStrategyConfig)1 EvictionConfig (com.hazelcast.config.EvictionConfig)1 JoinConfig (com.hazelcast.config.JoinConfig)1 MapConfig (com.hazelcast.config.MapConfig)1 MulticastConfig (com.hazelcast.config.MulticastConfig)1 NetworkConfig (com.hazelcast.config.NetworkConfig)1 TcpIpConfig (com.hazelcast.config.TcpIpConfig)1 HashMap (java.util.HashMap)1 DefaultAwsRegionProviderChain (software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain)1