Search in sources :

Example 1 with XmlClientConfigBuilder

use of com.hazelcast.client.config.XmlClientConfigBuilder 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 2 with XmlClientConfigBuilder

use of com.hazelcast.client.config.XmlClientConfigBuilder in project hazelcast by hazelcast.

the class ClientConsoleApp method main.

/**
     * Starts the test application. Loads the config from classpath hazelcast.xml,
     * if it fails to load, will use default config.
     *
     * @param args none
     * @throws Exception
     */
public static void main(String[] args) throws Exception {
    ClientConfig clientConfig;
    try {
        clientConfig = new XmlClientConfigBuilder().build();
    } catch (IllegalArgumentException e) {
        clientConfig = new ClientConfig();
    }
    final HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
    ClientConsoleApp clientConsoleApp = new ClientConsoleApp(client);
    clientConsoleApp.start(args);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) XmlClientConfigBuilder(com.hazelcast.client.config.XmlClientConfigBuilder) ClientConfig(com.hazelcast.client.config.ClientConfig)

Example 3 with XmlClientConfigBuilder

use of com.hazelcast.client.config.XmlClientConfigBuilder in project hazelcast by hazelcast.

the class ClientCachingProviderTest method setup.

@Before
public void setup() {
    // start a member
    Config config = new Config();
    config.getGroupConfig().setName("test-group1");
    config.getGroupConfig().setPassword("test-pass1");
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    instances.add(instance);
    // start two client instances
    instance1 = createHazelcastInstance(INSTANCE_1_NAME);
    instance2 = createHazelcastInstance(INSTANCE_2_NAME);
    try {
        instance3 = HazelcastClient.newHazelcastClient(new XmlClientConfigBuilder(CONFIG_CLASSPATH_LOCATION).build());
    } catch (IOException e) {
        throw new AssertionError("Could not construct named hazelcast client instance: " + e.getMessage());
    }
    instances.add(instance1);
    instances.add(instance2);
    instances.add(instance3);
    cachingProvider = createCachingProvider(instance1);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) XmlClientConfigBuilder(com.hazelcast.client.config.XmlClientConfigBuilder) Config(com.hazelcast.config.Config) ClientConfig(com.hazelcast.client.config.ClientConfig) IOException(java.io.IOException) Before(org.junit.Before)

Example 4 with XmlClientConfigBuilder

use of com.hazelcast.client.config.XmlClientConfigBuilder in project hazelcast by hazelcast.

the class ClientDiscoverySpiTest method testNodeFilter_from_xml.

@Test
public void testNodeFilter_from_xml() throws Exception {
    String xmlFileName = "hazelcast-client-discovery-spi-test.xml";
    InputStream xmlResource = ClientDiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName);
    ClientConfig clientConfig = new XmlClientConfigBuilder(xmlResource).build();
    ClientNetworkConfig networkConfig = clientConfig.getNetworkConfig();
    DiscoveryConfig discoveryConfig = networkConfig.getDiscoveryConfig();
    DiscoveryServiceProvider provider = new DefaultDiscoveryServiceProvider();
    DiscoveryService discoveryService = provider.newDiscoveryService(buildDiscoveryServiceSettings(discoveryConfig));
    discoveryService.start();
    discoveryService.discoverNodes();
    discoveryService.destroy();
    Field nodeFilterField = DefaultDiscoveryService.class.getDeclaredField("nodeFilter");
    nodeFilterField.setAccessible(true);
    TestNodeFilter nodeFilter = (TestNodeFilter) nodeFilterField.get(discoveryService);
    assertEquals(4, nodeFilter.getNodes().size());
}
Also used : DiscoveryServiceProvider(com.hazelcast.spi.discovery.integration.DiscoveryServiceProvider) DefaultDiscoveryServiceProvider(com.hazelcast.spi.discovery.impl.DefaultDiscoveryServiceProvider) Field(java.lang.reflect.Field) DefaultDiscoveryServiceProvider(com.hazelcast.spi.discovery.impl.DefaultDiscoveryServiceProvider) XmlClientConfigBuilder(com.hazelcast.client.config.XmlClientConfigBuilder) InputStream(java.io.InputStream) ClientConfig(com.hazelcast.client.config.ClientConfig) DiscoveryConfig(com.hazelcast.config.DiscoveryConfig) DiscoveryService(com.hazelcast.spi.discovery.integration.DiscoveryService) DefaultDiscoveryService(com.hazelcast.spi.discovery.impl.DefaultDiscoveryService) ClientNetworkConfig(com.hazelcast.client.config.ClientNetworkConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with XmlClientConfigBuilder

use of com.hazelcast.client.config.XmlClientConfigBuilder in project hazelcast by hazelcast.

the class ClientDiscoverySpiTest method test_discovery_address_translator_with_public_ip.

@Test
public void test_discovery_address_translator_with_public_ip() throws Exception {
    String xmlFileName = "hazelcast-client-discovery-spi-test.xml";
    InputStream xmlResource = ClientDiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName);
    ClientConfig clientConfig = new XmlClientConfigBuilder(xmlResource).build();
    ClientNetworkConfig networkConfig = clientConfig.getNetworkConfig();
    DiscoveryConfig discoveryConfig = networkConfig.getDiscoveryConfig();
    DiscoveryServiceProvider provider = new DefaultDiscoveryServiceProvider();
    DiscoveryService discoveryService = provider.newDiscoveryService(buildDiscoveryServiceSettings(discoveryConfig));
    AddressTranslator translator = new DiscoveryAddressTranslator(discoveryService, true);
    Address publicAddress = new Address("127.0.0.1", 50001);
    Address privateAddress = new Address("127.0.0.1", 1);
    // Enforce refresh of the internal mapping
    assertEquals(publicAddress, translator.translate(privateAddress));
}
Also used : Address(com.hazelcast.nio.Address) InputStream(java.io.InputStream) DiscoveryServiceProvider(com.hazelcast.spi.discovery.integration.DiscoveryServiceProvider) DefaultDiscoveryServiceProvider(com.hazelcast.spi.discovery.impl.DefaultDiscoveryServiceProvider) AddressTranslator(com.hazelcast.client.connection.AddressTranslator) DefaultDiscoveryServiceProvider(com.hazelcast.spi.discovery.impl.DefaultDiscoveryServiceProvider) XmlClientConfigBuilder(com.hazelcast.client.config.XmlClientConfigBuilder) ClientConfig(com.hazelcast.client.config.ClientConfig) DiscoveryConfig(com.hazelcast.config.DiscoveryConfig) DiscoveryService(com.hazelcast.spi.discovery.integration.DiscoveryService) DefaultDiscoveryService(com.hazelcast.spi.discovery.impl.DefaultDiscoveryService) ClientNetworkConfig(com.hazelcast.client.config.ClientNetworkConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

XmlClientConfigBuilder (com.hazelcast.client.config.XmlClientConfigBuilder)13 ClientConfig (com.hazelcast.client.config.ClientConfig)10 InputStream (java.io.InputStream)6 ClientNetworkConfig (com.hazelcast.client.config.ClientNetworkConfig)5 DiscoveryConfig (com.hazelcast.config.DiscoveryConfig)4 HazelcastInstance (com.hazelcast.core.HazelcastInstance)4 QuickTest (com.hazelcast.test.annotation.QuickTest)4 Test (org.junit.Test)4 Config (com.hazelcast.config.Config)3 DefaultDiscoveryService (com.hazelcast.spi.discovery.impl.DefaultDiscoveryService)3 DefaultDiscoveryServiceProvider (com.hazelcast.spi.discovery.impl.DefaultDiscoveryServiceProvider)3 DiscoveryService (com.hazelcast.spi.discovery.integration.DiscoveryService)3 DiscoveryServiceProvider (com.hazelcast.spi.discovery.integration.DiscoveryServiceProvider)3 IOException (java.io.IOException)3 AddressTranslator (com.hazelcast.client.connection.AddressTranslator)2 ClientConnectionManagerFactory (com.hazelcast.client.impl.ClientConnectionManagerFactory)2 HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)2 HazelcastClientProxy (com.hazelcast.client.impl.HazelcastClientProxy)2 Address (com.hazelcast.nio.Address)2 Before (org.junit.Before)2