Search in sources :

Example 11 with XmlClientConfigBuilder

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

the class ClientDiscoverySpiTest method test_discovery_address_translator.

@Test
public void test_discovery_address_translator() 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, false);
    Address address = new Address("127.0.0.1", 50001);
    assertNull(translator.translate(null));
    assertEquals(address, translator.translate(address));
    // Enforce refresh of the internal mapping
    assertEquals(address, translator.translate(address));
}
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)

Example 12 with XmlClientConfigBuilder

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

the class ClientDiscoverySpiTest method testParsing.

@Test
public void testParsing() 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();
    AwsConfig awsConfig = networkConfig.getAwsConfig();
    assertNull(awsConfig);
    DiscoveryConfig discoveryConfig = networkConfig.getDiscoveryConfig();
    assertTrue(discoveryConfig.isEnabled());
    assertEquals(1, discoveryConfig.getDiscoveryStrategyConfigs().size());
    DiscoveryStrategyConfig providerConfig = discoveryConfig.getDiscoveryStrategyConfigs().iterator().next();
    assertEquals(3, providerConfig.getProperties().size());
    assertEquals("foo", providerConfig.getProperties().get("key-string"));
    assertEquals("123", providerConfig.getProperties().get("key-int"));
    assertEquals("true", providerConfig.getProperties().get("key-boolean"));
}
Also used : AwsConfig(com.hazelcast.config.AwsConfig) XmlClientConfigBuilder(com.hazelcast.client.config.XmlClientConfigBuilder) DiscoveryStrategyConfig(com.hazelcast.config.DiscoveryStrategyConfig) InputStream(java.io.InputStream) ClientConfig(com.hazelcast.client.config.ClientConfig) DiscoveryConfig(com.hazelcast.config.DiscoveryConfig) ClientNetworkConfig(com.hazelcast.client.config.ClientNetworkConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 13 with XmlClientConfigBuilder

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

the class HazelcastClientManager method newHazelcastClient.

@SuppressWarnings("unchecked")
public static HazelcastInstance newHazelcastClient(ClientConfig config, HazelcastClientFactory hazelcastClientFactory) {
    if (config == null) {
        config = new XmlClientConfigBuilder().build();
    }
    final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    HazelcastClientProxy proxy;
    try {
        Thread.currentThread().setContextClassLoader(HazelcastClient.class.getClassLoader());
        ClientConnectionManagerFactory clientConnectionManagerFactory = new DefaultClientConnectionManagerFactory();
        final HazelcastClientInstanceImpl client = hazelcastClientFactory.createHazelcastInstanceClient(config, clientConnectionManagerFactory);
        client.start();
        OutOfMemoryErrorDispatcher.registerClient(client);
        proxy = hazelcastClientFactory.createProxy(client);
        if (INSTANCE.clients.putIfAbsent(client.getName(), proxy) != null) {
            throw new DuplicateInstanceNameException("HazelcastClientInstance with name '" + client.getName() + "' already exists!");
        }
    } finally {
        Thread.currentThread().setContextClassLoader(contextClassLoader);
    }
    return proxy;
}
Also used : DuplicateInstanceNameException(com.hazelcast.core.DuplicateInstanceNameException) XmlClientConfigBuilder(com.hazelcast.client.config.XmlClientConfigBuilder) ClientConnectionManagerFactory(com.hazelcast.client.impl.ClientConnectionManagerFactory) DefaultClientConnectionManagerFactory(com.hazelcast.client.impl.DefaultClientConnectionManagerFactory) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) DefaultClientConnectionManagerFactory(com.hazelcast.client.impl.DefaultClientConnectionManagerFactory) HazelcastClientProxy(com.hazelcast.client.impl.HazelcastClientProxy)

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