public final class HazelcastClient extends Object
Hazelcast
class and provides the ability
the create and manage Hazelcast clients. Hazelcast clients are HazelcastInstance
implementations, so
in most cases most of the code is unaware of talking to a cluster member or a client.
ClientNetworkConfig.setSmartRouting(boolean)
.
Modifier and Type | Method and Description |
---|---|
static Collection<HazelcastInstance> |
getAllHazelcastClients()
Gets an immutable collection of all client HazelcastInstances created in this JVM.
|
static HazelcastInstance |
getHazelcastClientByName(String instanceName)
Returns an existing HazelcastClient with instanceName.
|
static HazelcastInstance |
getOrCreateHazelcastClient()
Gets or creates a new HazelcastInstance (a new client in a cluster) with the default XML configuration looked up in:
System property: Hazelcast will first check if "hazelcast.client.config" system property is set to a file or a
classpath:... path. |
static HazelcastInstance |
getOrCreateHazelcastClient(ClientConfig config)
Gets or creates a new HazelcastInstance (a new client in a cluster) with a certain name.
|
static HazelcastInstance |
newHazelcastClient()
Creates a new HazelcastInstance (a new client in a cluster).
|
static HazelcastInstance |
newHazelcastClient(ClientConfig config)
Creates a new HazelcastInstance (a new client in a cluster).
|
static HazelcastInstance |
newHazelcastFailoverClient()
Creates a client with cluster switch capability.
|
static HazelcastInstance |
newHazelcastFailoverClient(ClientFailoverConfig clientFailoverConfig)
Creates a client with cluster switch capability.
|
static void |
setOutOfMemoryHandler(OutOfMemoryHandler outOfMemoryHandler)
Sets OutOfMemoryHandler to be used when an OutOfMemoryError
is caught by Hazelcast Client threads.
|
static void |
shutdown(HazelcastInstance instance)
Shutdown the provided client and remove it from the managed list
|
static void |
shutdown(String instanceName)
Shutdown the provided client and remove it from the managed list
|
static void |
shutdownAll()
Shuts down all the client HazelcastInstance created in this JVM.
|
public static HazelcastInstance newHazelcastClient()
shutdownAll()
.
Hazelcast will look into two places for the configuration file:
classpath:...
path. The configuration can either be an XML or a YAML configuration, distinguished by the
suffix ('.xml' or '.yaml') of the provided configuration file's name
Examples: -Dhazelcast.client.config=C:/myhazelcastclient.xml ,
-Dhazelcast.client.config=classpath:the-hazelcast-config.yaml ,
-Dhazelcast.client.config=classpath:com/mydomain/hazelcast.xml
shutdownAll()
,
(String)
public static HazelcastInstance newHazelcastClient(ClientConfig config)
shutdownAll()
.config
- Configuration for the new HazelcastInstance (member)shutdownAll()
,
(String)
public static HazelcastInstance newHazelcastFailoverClient()
ClientFailoverConfig
when it disconnects from a cluster.
The configuration is loaded using using the following resolution mechanism:
HazelcastException
is thrownHazelcastException
- if no failover configuration is foundInvalidConfigurationException
- if the loaded failover configuration is not validpublic static HazelcastInstance newHazelcastFailoverClient(ClientFailoverConfig clientFailoverConfig)
ClientFailoverConfig
when it disconnects from a cluster.
If provided clientFailoverConfig is null
, the configuration is loaded using using the following resolution
mechanism:
HazelcastException
is thrownclientFailoverConfig
- config describing the failover configs and try countHazelcastException
- if no failover configuration is foundInvalidConfigurationException
- if the provided or the loaded failover configuration is not validpublic static HazelcastInstance getHazelcastClientByName(String instanceName)
instanceName
- Name of the HazelcastInstance (client) which can be retrieved by HazelcastInstance.getName()
public static HazelcastInstance getOrCreateHazelcastClient()
classpath:...
path. The configuration can either be an XML or a YAML configuration, distinguished by the
suffix ('.xml' or '.yaml') of the provided configuration file's name
Examples: -Dhazelcast.client.config=C:/myhazelcastclient.xml ,
-Dhazelcast.client.config=classpath:the-hazelcast-config.yaml ,
-Dhazelcast.client.config=classpath:com/mydomain/hazelcast.xml
If a configuration file is not located, an IllegalArgumentException
will be thrown.
If a Hazelcast instance with the same name as the configuration exists, then it is returned, otherwise it is created.
IllegalArgumentException
- if the instance name of the config is null or empty or if no config file can be
located.(String)
public static HazelcastInstance getOrCreateHazelcastClient(ClientConfig config)
If a Hazelcast instance with the same name as the configuration exists, then it is returned, otherwise it is created.
If config
is null
, Hazelcast will look into two places for the configuration file:
classpath:...
path. The configuration can either be an XML or a YAML configuration, distinguished by the
suffix ('.xml' or '.yaml') of the provided configuration file's name
Examples: -Dhazelcast.client.config=C:/myhazelcastclient.xml ,
-Dhazelcast.client.config=classpath:the-hazelcast-config.yaml ,
-Dhazelcast.client.config=classpath:com/mydomain/hazelcast.xml
If a configuration file is not located, an IllegalArgumentException
will be thrown.
config
- Client configurationIllegalArgumentException
- if the instance name of the config is null or empty or if no config file can be
located.(String)
public static Collection<HazelcastInstance> getAllHazelcastClients()
public static void shutdownAll()
getAllHazelcastClients()
public static void shutdown(HazelcastInstance instance)
instance
- the hazelcast client instancepublic static void shutdown(String instanceName)
instanceName
- the hazelcast client instance namepublic static void setOutOfMemoryHandler(OutOfMemoryHandler outOfMemoryHandler)
Warning: OutOfMemoryHandler may not be called although JVM throws OutOfMemoryError. Because error may be thrown from an external (user thread) thread and Hazelcast may not be informed about OutOfMemoryError.
outOfMemoryHandler
- set when an OutOfMemoryError is caught by HazelcastClient threadsOutOfMemoryError
,
OutOfMemoryHandler
Copyright © 2022 Hazelcast, Inc.. All Rights Reserved.