Search in sources :

Example 1 with HazelcastInstance

use of com.hazelcast.core.HazelcastInstance in project camel by apache.

the class HazelcastComponent method doStop.

@Override
public void doStop() throws Exception {
    for (HazelcastInstance hazelcastInstance : customHazelcastInstances) {
        hazelcastInstance.getLifecycleService().shutdown();
    }
    customHazelcastInstances.clear();
    super.doStop();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance)

Example 2 with HazelcastInstance

use of com.hazelcast.core.HazelcastInstance in project camel by apache.

the class HazelcastComponent method getOrCreateHzInstance.

private HazelcastInstance getOrCreateHzInstance(CamelContext context, Map<String, Object> parameters) throws Exception {
    HazelcastInstance hzInstance = null;
    Config config = null;
    // Query param named 'hazelcastInstance' (if exists) overrides the instance that was set
    hzInstance = resolveAndRemoveReferenceParameter(parameters, HAZELCAST_INSTANCE_PARAM, HazelcastInstance.class);
    // Check if an already created instance is given then just get instance by its name.
    if (hzInstance == null && parameters.get(HAZELCAST_INSTANCE_NAME_PARAM) != null) {
        hzInstance = Hazelcast.getHazelcastInstanceByName((String) parameters.get(HAZELCAST_INSTANCE_NAME_PARAM));
    }
    // as reference or as xml configuration file.
    if (hzInstance == null) {
        config = resolveAndRemoveReferenceParameter(parameters, HAZELCAST_CONFIGU_PARAM, Config.class);
        if (config == null) {
            String configUri = getAndRemoveParameter(parameters, HAZELCAST_CONFIGU_URI_PARAM, String.class);
            if (configUri != null) {
                configUri = getCamelContext().resolvePropertyPlaceholders(configUri);
            }
            if (configUri != null) {
                InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context, configUri);
                config = new XmlConfigBuilder(is).build();
            }
        }
        if (hazelcastInstance == null && config == null) {
            config = new XmlConfigBuilder().build();
            // Disable the version check
            config.getProperties().setProperty("hazelcast.version.check.enabled", "false");
            config.getProperties().setProperty("hazelcast.phone.home.enabled", "false");
            hzInstance = Hazelcast.newHazelcastInstance(config);
        } else if (config != null) {
            if (ObjectHelper.isNotEmpty(config.getInstanceName())) {
                hzInstance = Hazelcast.getOrCreateHazelcastInstance(config);
            } else {
                hzInstance = Hazelcast.newHazelcastInstance(config);
            }
        }
        if (hzInstance != null) {
            if (this.customHazelcastInstances.add(hzInstance)) {
                LOGGER.debug("Add managed HZ instance {}", hzInstance.getName());
            }
        }
    }
    return hzInstance == null ? hazelcastInstance : hzInstance;
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) XmlConfigBuilder(com.hazelcast.config.XmlConfigBuilder) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) InputStream(java.io.InputStream)

Example 3 with HazelcastInstance

use of com.hazelcast.core.HazelcastInstance in project camel by apache.

the class HazelcastComponent method getOrCreateHzClientInstance.

private HazelcastInstance getOrCreateHzClientInstance(CamelContext context, Map<String, Object> parameters) throws Exception {
    HazelcastInstance hzInstance = null;
    ClientConfig config = null;
    // Query param named 'hazelcastInstance' (if exists) overrides the instance that was set
    hzInstance = resolveAndRemoveReferenceParameter(parameters, HAZELCAST_INSTANCE_PARAM, HazelcastInstance.class);
    // Check if an already created instance is given then just get instance by its name.
    if (hzInstance == null && parameters.get(HAZELCAST_INSTANCE_NAME_PARAM) != null) {
        hzInstance = Hazelcast.getHazelcastInstanceByName((String) parameters.get(HAZELCAST_INSTANCE_NAME_PARAM));
    }
    // as reference or as xml configuration file.
    if (hzInstance == null) {
        config = resolveAndRemoveReferenceParameter(parameters, HAZELCAST_CONFIGU_PARAM, ClientConfig.class);
        if (config == null) {
            String configUri = getAndRemoveParameter(parameters, HAZELCAST_CONFIGU_URI_PARAM, String.class);
            if (configUri != null) {
                configUri = getCamelContext().resolvePropertyPlaceholders(configUri);
            }
            if (configUri != null) {
                InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context, configUri);
                config = new XmlClientConfigBuilder(is).build();
            }
        }
        if (hazelcastInstance == null && config == null) {
            config = new XmlClientConfigBuilder().build();
            // Disable the version check
            config.getProperties().setProperty("hazelcast.version.check.enabled", "false");
            config.getProperties().setProperty("hazelcast.phone.home.enabled", "false");
            hzInstance = HazelcastClient.newHazelcastClient(config);
        } else if (config != null) {
            hzInstance = HazelcastClient.newHazelcastClient(config);
        }
        if (hzInstance != null) {
            if (this.customHazelcastInstances.add(hzInstance)) {
                LOGGER.debug("Add managed HZ instance {}", hzInstance.getName());
            }
        }
    }
    return hzInstance == null ? hazelcastInstance : hzInstance;
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) XmlClientConfigBuilder(com.hazelcast.client.config.XmlClientConfigBuilder) InputStream(java.io.InputStream) ClientConfig(com.hazelcast.client.config.ClientConfig)

Example 4 with HazelcastInstance

use of com.hazelcast.core.HazelcastInstance in project camel by apache.

the class HazelcastConfigurationTest method testNamedInstanceWithConfigurationUri.

@Test
public void testNamedInstanceWithConfigurationUri() throws Exception {
    DefaultCamelContext context = null;
    try {
        context = new DefaultCamelContext();
        context.start();
        HazelcastDefaultEndpoint endpoint1 = getHzEndpoint(context, "hazelcast:map:my-cache-1?hazelcastConfigUri=classpath:hazelcast-named.xml");
        HazelcastDefaultEndpoint endpoint2 = getHzEndpoint(context, "hazelcast:map:my-cache-2?hazelcastConfigUri=classpath:hazelcast-named.xml");
        Assert.assertNotNull(endpoint1.getHazelcastInstance());
        Assert.assertNotNull(endpoint2.getHazelcastInstance());
        Assert.assertTrue(endpoint1.getHazelcastInstance() == endpoint2.getHazelcastInstance());
        HazelcastComponent component = context.getComponent("hazelcast", HazelcastComponent.class);
        Assert.assertNull(component.getHazelcastInstance());
        HazelcastInstance hz = endpoint1.getHazelcastInstance();
        Assert.assertFalse(hz.getConfig().getNetworkConfig().getJoin().getAwsConfig().isEnabled());
        Assert.assertFalse(hz.getConfig().getNetworkConfig().getJoin().getMulticastConfig().isEnabled());
        Assert.assertFalse(hz.getConfig().getNetworkConfig().getJoin().getTcpIpConfig().isEnabled());
        Assert.assertEquals(9876, hz.getConfig().getNetworkConfig().getPort());
    } finally {
        if (context != null) {
            context.stop();
        }
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 5 with HazelcastInstance

use of com.hazelcast.core.HazelcastInstance in project camel by apache.

the class HazelcastConfigurationTest method testDefaultConfiguration.

@Test
public void testDefaultConfiguration() throws Exception {
    DefaultCamelContext context = null;
    try {
        context = new DefaultCamelContext();
        context.start();
        HazelcastDefaultEndpoint endpoint1 = getHzEndpoint(context, "hazelcast:map:my-cache-1");
        HazelcastDefaultEndpoint endpoint2 = getHzEndpoint(context, "hazelcast:map:my-cache-2");
        Assert.assertNotNull(endpoint1.getHazelcastInstance());
        Assert.assertNotNull(endpoint2.getHazelcastInstance());
        Assert.assertTrue(endpoint1.getHazelcastInstance() != endpoint2.getHazelcastInstance());
        HazelcastComponent component = context.getComponent("hazelcast", HazelcastComponent.class);
        Assert.assertNull(component.getHazelcastInstance());
        for (HazelcastDefaultEndpoint endpoint : Arrays.asList(endpoint1, endpoint2)) {
            HazelcastInstance hz = endpoint.getHazelcastInstance();
            Assert.assertFalse(hz.getConfig().getNetworkConfig().getJoin().getAwsConfig().isEnabled());
            Assert.assertFalse(hz.getConfig().getNetworkConfig().getJoin().getMulticastConfig().isEnabled());
            Assert.assertTrue(hz.getConfig().getNetworkConfig().getJoin().getTcpIpConfig().isEnabled());
            Assert.assertEquals(5701, hz.getConfig().getNetworkConfig().getPort());
        }
    } finally {
        if (context != null) {
            context.stop();
        }
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Aggregations

HazelcastInstance (com.hazelcast.core.HazelcastInstance)2077 Test (org.junit.Test)1684 QuickTest (com.hazelcast.test.annotation.QuickTest)1466 ParallelTest (com.hazelcast.test.annotation.ParallelTest)1389 Config (com.hazelcast.config.Config)815 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)665 AssertTask (com.hazelcast.test.AssertTask)263 MapConfig (com.hazelcast.config.MapConfig)254 CountDownLatch (java.util.concurrent.CountDownLatch)251 NightlyTest (com.hazelcast.test.annotation.NightlyTest)230 MapStoreConfig (com.hazelcast.config.MapStoreConfig)169 IMap (com.hazelcast.core.IMap)145 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)144 ClientConfig (com.hazelcast.client.config.ClientConfig)137 Before (org.junit.Before)111 NearCacheConfig (com.hazelcast.config.NearCacheConfig)106 Member (com.hazelcast.core.Member)96 Map (java.util.Map)96 SlowTest (com.hazelcast.test.annotation.SlowTest)94 SqlPredicate (com.hazelcast.query.SqlPredicate)83