use of com.hazelcast.client.impl.connection.AddressProvider in project hazelcast by hazelcast.
the class TcpClientConnectionManager method translate.
private Address translate(Address target) {
CandidateClusterContext currentContext = clusterDiscoveryService.current();
AddressProvider addressProvider = currentContext.getAddressProvider();
try {
Address translatedAddress = addressProvider.translate(target);
if (translatedAddress == null) {
throw new HazelcastException("Address Provider " + addressProvider.getClass() + " could not translate address " + target);
}
return translatedAddress;
} catch (Exception e) {
logger.warning("Failed to translate address " + target + " via address provider " + e.getMessage());
throw rethrow(e);
}
}
use of com.hazelcast.client.impl.connection.AddressProvider in project hazelcast by hazelcast.
the class ClusterDiscoveryServiceBuilder method build.
public ClusterDiscoveryService build() {
ArrayList<CandidateClusterContext> contexts = new ArrayList<>();
for (ClientConfig config : configs) {
ClientNetworkConfig networkConfig = config.getNetworkConfig();
SocketInterceptor interceptor = initSocketInterceptor(networkConfig.getSocketInterceptorConfig());
ICredentialsFactory credentialsFactory = initCredentialsFactory(config);
if (credentialsFactory == null) {
credentialsFactory = new StaticCredentialsFactory(new UsernamePasswordCredentials(null, null));
}
credentialsFactory.configure(new ClientCallbackHandler(config));
DiscoveryService discoveryService = initDiscoveryService(config);
AddressProvider provider;
if (externalAddressProvider != null) {
provider = externalAddressProvider;
} else {
provider = createAddressProvider(config, discoveryService);
}
final SSLConfig sslConfig = networkConfig.getSSLConfig();
final SocketOptions socketOptions = networkConfig.getSocketOptions();
contexts.add(new CandidateClusterContext(config.getClusterName(), provider, discoveryService, credentialsFactory, interceptor, clientExtension.createChannelInitializer(sslConfig, socketOptions)));
}
return new ClusterDiscoveryService(unmodifiableList(contexts), configsTryCount, lifecycleService);
}
use of com.hazelcast.client.impl.connection.AddressProvider in project hazelcast by hazelcast.
the class ClientRegressionWithRealNetworkTest method testOperationsContinueWhenClientDisconnected.
private void testOperationsContinueWhenClientDisconnected(ClientConnectionStrategyConfig.ReconnectMode reconnectMode) {
HazelcastInstance instance1 = Hazelcast.newHazelcastInstance();
ClientConfig clientConfig = new ClientConfig();
clientConfig.getConnectionStrategyConfig().setReconnectMode(reconnectMode);
AtomicBoolean waitFlag = new AtomicBoolean();
CountDownLatch testFinished = new CountDownLatch(1);
AddressProvider addressProvider = new AddressProvider() {
@Override
public Addresses loadAddresses() {
if (waitFlag.get()) {
try {
testFinished.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return AddressHelper.getSocketAddresses("127.0.0.1");
}
@Override
public Address translate(Address address) {
return address;
}
};
clientConfig.getConnectionStrategyConfig().getConnectionRetryConfig().setClusterConnectTimeoutMillis(Long.MAX_VALUE);
clientConfig.setProperty(ClientProperty.INVOCATION_TIMEOUT_SECONDS.getName(), "3");
HazelcastInstance client = HazelcastClientUtil.newHazelcastClient(addressProvider, clientConfig);
HazelcastInstance instance2 = Hazelcast.newHazelcastInstance();
warmUpPartitions(instance1, instance2);
String keyOwnedBy2 = generateKeyOwnedBy(instance2);
makeSureConnectedToServers(client, 2);
IMap<Object, Object> clientMap = client.getMap("test");
// we are closing a connection and making sure It is not established ever again
waitFlag.set(true);
UUID memberUUID = instance1.getLocalEndpoint().getUuid();
instance1.shutdown();
makeSureDisconnectedFromServer(client, memberUUID);
// we expect these operations to run without throwing exception, since they are done on live instance.
clientMap.put(keyOwnedBy2, 1);
assertEquals(1, clientMap.get(keyOwnedBy2));
testFinished.countDown();
}
use of com.hazelcast.client.impl.connection.AddressProvider in project hazelcast by hazelcast.
the class TestHazelcastFactory method createAddressProvider.
private AddressProvider createAddressProvider(ClientConfig config) {
boolean discoveryEnabled = new HazelcastProperties(config.getProperties()).getBoolean(ClientProperty.DISCOVERY_SPI_ENABLED);
List<DiscoveryStrategyConfig> aliasedDiscoveryConfigs = ClientAliasedDiscoveryConfigUtils.createDiscoveryStrategyConfigs(config);
List<String> userConfiguredAddresses = config.getNetworkConfig().getAddresses();
boolean isAtLeastAProviderConfigured = discoveryEnabled || !aliasedDiscoveryConfigs.isEmpty() || !userConfiguredAddresses.isEmpty();
if (isAtLeastAProviderConfigured) {
// address providers or addresses are configured explicitly, don't add more addresses
return null;
}
return new AddressProvider() {
@Override
public Addresses loadAddresses() {
Addresses possibleAddresses = new Addresses();
for (Address address : getKnownAddresses()) {
Addresses addresses = AddressHelper.getPossibleSocketAddresses(address.getPort(), address.getHost(), 1);
possibleAddresses.addAll(addresses);
}
return possibleAddresses;
}
@Override
public Address translate(Address address) {
return address;
}
};
}
Aggregations