Search in sources :

Example 11 with ClientNetworkConfig

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

the class ClientDiscoverySpiTest method test_CustomDiscoveryService_whenDiscoveredNodes_isEmpty.

@Test
public void test_CustomDiscoveryService_whenDiscoveredNodes_isEmpty() {
    ClientConfig config = new ClientConfig();
    config.setProperty(GroupProperty.DISCOVERY_SPI_ENABLED.getName(), "true");
    final DiscoveryService discoveryService = mock(DiscoveryService.class);
    DiscoveryServiceProvider discoveryServiceProvider = new DiscoveryServiceProvider() {

        public DiscoveryService newDiscoveryService(DiscoveryServiceSettings arg0) {
            when(discoveryService.discoverNodes()).thenReturn(Collections.<DiscoveryNode>emptyList());
            return discoveryService;
        }
    };
    ClientNetworkConfig networkConfig = config.getNetworkConfig();
    networkConfig.setConnectionAttemptLimit(1);
    networkConfig.setConnectionAttemptPeriod(1);
    networkConfig.getDiscoveryConfig().setDiscoveryServiceProvider(discoveryServiceProvider);
    try {
        HazelcastClient.newHazelcastClient(config);
    } catch (IllegalStateException expected) {
    // no server available
    }
    verify(discoveryService).discoverNodes();
}
Also used : DiscoveryServiceProvider(com.hazelcast.spi.discovery.integration.DiscoveryServiceProvider) DefaultDiscoveryServiceProvider(com.hazelcast.spi.discovery.impl.DefaultDiscoveryServiceProvider) ClientConfig(com.hazelcast.client.config.ClientConfig) DiscoveryService(com.hazelcast.spi.discovery.integration.DiscoveryService) DefaultDiscoveryService(com.hazelcast.spi.discovery.impl.DefaultDiscoveryService) DiscoveryServiceSettings(com.hazelcast.spi.discovery.integration.DiscoveryServiceSettings) ClientNetworkConfig(com.hazelcast.client.config.ClientNetworkConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 12 with ClientNetworkConfig

use of com.hazelcast.client.config.ClientNetworkConfig 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 ClientNetworkConfig

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

the class DummyClientMapPartitionIteratorTest method getClientConfig.

private static ClientConfig getClientConfig(HazelcastInstance instance) {
    Address address = instance.getCluster().getLocalMember().getAddress();
    String addressString = address.getHost() + ":" + address.getPort();
    ClientConfig clientConfig = new ClientConfig();
    ClientNetworkConfig networkConfig = new ClientNetworkConfig();
    networkConfig.setSmartRouting(false);
    networkConfig.addAddress(addressString);
    clientConfig.setNetworkConfig(networkConfig);
    return clientConfig;
}
Also used : Address(com.hazelcast.nio.Address) ClientConfig(com.hazelcast.client.config.ClientConfig) ClientNetworkConfig(com.hazelcast.client.config.ClientNetworkConfig)

Example 14 with ClientNetworkConfig

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

the class ClusterListenerSupport method connectToCluster.

public void connectToCluster() throws Exception {
    ownerConnectionAddress = null;
    final ClientNetworkConfig networkConfig = client.getClientConfig().getNetworkConfig();
    final int connAttemptLimit = networkConfig.getConnectionAttemptLimit();
    final int connectionAttemptPeriod = networkConfig.getConnectionAttemptPeriod();
    final int connectionAttemptLimit = connAttemptLimit == 0 ? Integer.MAX_VALUE : connAttemptLimit;
    int attempt = 0;
    Set<InetSocketAddress> triedAddresses = new HashSet<InetSocketAddress>();
    while (attempt < connectionAttemptLimit) {
        if (!client.getLifecycleService().isRunning()) {
            if (logger.isFinestEnabled()) {
                logger.finest("Giving up on retrying to connect to cluster since client is shutdown");
            }
            break;
        }
        attempt++;
        final long nextTry = Clock.currentTimeMillis() + connectionAttemptPeriod;
        boolean isConnected = connect(triedAddresses);
        if (isConnected) {
            return;
        }
        final long remainingTime = nextTry - Clock.currentTimeMillis();
        logger.warning(String.format("Unable to get alive cluster connection, try in %d ms later, attempt %d of %d.", Math.max(0, remainingTime), attempt, connectionAttemptLimit));
        if (remainingTime > 0) {
            try {
                Thread.sleep(remainingTime);
            } catch (InterruptedException e) {
                break;
            }
        }
    }
    throw new IllegalStateException("Unable to connect to any address in the config!" + " The following addresses were tried: " + triedAddresses);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ClientNetworkConfig(com.hazelcast.client.config.ClientNetworkConfig) HashSet(java.util.HashSet)

Example 15 with ClientNetworkConfig

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

the class TestClientApplicationContext method testAwsClientConfig.

@Test
public void testAwsClientConfig() {
    assertNotNull(client4);
    ClientConfig config = client4.getClientConfig();
    ClientNetworkConfig networkConfig = config.getNetworkConfig();
    ClientAwsConfig awsConfig = networkConfig.getAwsConfig();
    assertFalse(awsConfig.isEnabled());
    assertTrue(awsConfig.isInsideAws());
    assertEquals("sample-access-key", awsConfig.getAccessKey());
    assertEquals("sample-secret-key", awsConfig.getSecretKey());
    assertEquals("sample-region", awsConfig.getRegion());
    assertEquals("sample-group", awsConfig.getSecurityGroupName());
    assertEquals("sample-tag-key", awsConfig.getTagKey());
    assertEquals("sample-tag-value", awsConfig.getTagValue());
}
Also used : ClientConfig(com.hazelcast.client.config.ClientConfig) ClientAwsConfig(com.hazelcast.client.config.ClientAwsConfig) ClientNetworkConfig(com.hazelcast.client.config.ClientNetworkConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

ClientNetworkConfig (com.hazelcast.client.config.ClientNetworkConfig)18 ClientConfig (com.hazelcast.client.config.ClientConfig)13 QuickTest (com.hazelcast.test.annotation.QuickTest)9 Test (org.junit.Test)9 DefaultDiscoveryServiceProvider (com.hazelcast.spi.discovery.impl.DefaultDiscoveryServiceProvider)6 DiscoveryService (com.hazelcast.spi.discovery.integration.DiscoveryService)6 DiscoveryServiceProvider (com.hazelcast.spi.discovery.integration.DiscoveryServiceProvider)6 XmlClientConfigBuilder (com.hazelcast.client.config.XmlClientConfigBuilder)5 DiscoveryConfig (com.hazelcast.config.DiscoveryConfig)5 Address (com.hazelcast.nio.Address)5 DefaultDiscoveryService (com.hazelcast.spi.discovery.impl.DefaultDiscoveryService)5 InputStream (java.io.InputStream)4 DiscoveryServiceSettings (com.hazelcast.spi.discovery.integration.DiscoveryServiceSettings)3 ClientAwsConfig (com.hazelcast.client.config.ClientAwsConfig)2 AddressTranslator (com.hazelcast.client.connection.AddressTranslator)2 ILogger (com.hazelcast.logging.ILogger)2 InetSocketAddress (java.net.InetSocketAddress)2 HazelcastClient (com.hazelcast.client.HazelcastClient)1 AddressProvider (com.hazelcast.client.connection.AddressProvider)1 AwsAddressProvider (com.hazelcast.client.spi.impl.AwsAddressProvider)1