public final class Hazelcast extends Object
HazelcastInstance
's, a node in a cluster.Modifier and Type | Method and Description |
---|---|
static HazelcastInstance |
bootstrappedInstance()
Returns either a local Hazelcast instance or a "bootstrapped" Hazelcast
client for a remote Hazelcast cluster, depending on the context.
|
static Set<HazelcastInstance> |
getAllHazelcastInstances()
Returns all active/running HazelcastInstances on this JVM.
|
static HazelcastInstance |
getHazelcastInstanceByName(String instanceName)
Returns an existing HazelcastInstance with instanceName.
|
static HazelcastInstance |
getOrCreateHazelcastInstance()
Gets or creates a HazelcastInstance with the default XML configuration looked up in:
System property: Hazelcast will first check if "hazelcast.config" system property is set to a file or a
classpath:... path. |
static HazelcastInstance |
getOrCreateHazelcastInstance(Config config)
Gets or creates the HazelcastInstance with a certain name.
|
static HazelcastInstance |
newHazelcastInstance()
Creates a new HazelcastInstance (a new node in a cluster).
|
static HazelcastInstance |
newHazelcastInstance(Config config)
Creates a new HazelcastInstance (a new node in a cluster).
|
static void |
setOutOfMemoryHandler(OutOfMemoryHandler outOfMemoryHandler)
Sets
OutOfMemoryHandler to be used when an OutOfMemoryError
is caught by Hazelcast threads. |
static void |
shutdownAll()
Shuts down all member
HazelcastInstance s running on this JVM. |
public static void shutdownAll()
HazelcastInstance
s running on this JVM.
It doesn't shutdown all members of the cluster but just the ones running on this JVM.newHazelcastInstance(Config)
public static HazelcastInstance newHazelcastInstance(Config config)
To shutdown all running HazelcastInstances (all members on this JVM)
call shutdownAll()
.
config
- Configuration for the new HazelcastInstance (member)shutdownAll()
,
getHazelcastInstanceByName(String)
public static HazelcastInstance newHazelcastInstance()
To shutdown all running HazelcastInstances (all members on this JVM)
call shutdownAll()
.
Hazelcast will look into two places for the configuration file:
classpath:...
path.
Examples: -Dhazelcast.config=C:/myhazelcast.xml , -Dhazelcast.config=classpath:the-hazelcast-config.xml ,
-Dhazelcast.config=classpath:com/mydomain/hazelcast.xml
shutdownAll()
,
getHazelcastInstanceByName(String)
public static HazelcastInstance getHazelcastInstanceByName(String instanceName)
To shutdown all running HazelcastInstances (all members on this JVM)
call shutdownAll()
.
instanceName
- Name of the HazelcastInstance (member)newHazelcastInstance(Config)
,
shutdownAll()
public static HazelcastInstance getOrCreateHazelcastInstance()
classpath:...
path.
Examples: -Dhazelcast.config=C:/myhazelcast.xml , -Dhazelcast.config=classpath:the-hazelcast-config.xml ,
-Dhazelcast.config=classpath:com/mydomain/hazelcast.xml
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.public static HazelcastInstance getOrCreateHazelcastInstance(Config config)
config
is null
, then an XML configuration file is looked up in the following order:
classpath:...
path.
Examples: -Dhazelcast.config=C:/myhazelcast.xml , -Dhazelcast.config=classpath:the-hazelcast-config.xml ,
-Dhazelcast.config=classpath:com/mydomain/hazelcast.xml
config
- the Config.IllegalArgumentException
- if the instance name of the config is null or empty or if no config file can be
located.public static Set<HazelcastInstance> getAllHazelcastInstances()
To shutdown all running HazelcastInstances (all members on this JVM)
call shutdownAll()
.
newHazelcastInstance(Config)
,
getHazelcastInstanceByName(String)
,
shutdownAll()
@Nonnull public static HazelcastInstance bootstrappedInstance()
When you submit a job to a Hazelcast instance that runs locally in your
JVM, it will have all the dependency classes available. However, when
you take the same job to a remote Hazelcast cluster, you'll often find
that it fails with a ClassNotFoundException
because the remote
cluster doesn't have all the classes you use in the job.
Normally you would have to explicitly add all the dependency classes to
the JobConfig
, either one by one or
packaged into a JAR. If you're submitting a job using the command-line
tool hazelcast submit
, the JAR to attach is the same JAR that
contains the code that submits the job.
This factory takes all of the above into account in order to provide a smoother experience:
hazelcast submit
, it returns a local HazelcastInstance
, either by creating a new one or looking up a cached one.
The instance won't join any cluster.
hazelcast submit
, it returns a "bootstrapped"
instance of Hazelcast client that automatically attaches the JAR to all jobs
you submit using it.
To use this feature, follow these steps:
main()
method and your code the usual way, making
sure you use this method (instead of HazelcastClient.newHazelcastClient()
)
to acquire a Hazelcast client instance.
jet-job.jar
) with your entry point
declared as the Main-Class
in MANIFEST.MF
. The JAR should
include all dependencies required to run it (except the Hazelcast classes
— these are already available on the cluster classpath).
hazelcast submit hz-job.jar
on
the command line. This assumes you have downloaded the Hazelcast
distribution package and its bin
directory is on your system
path. The Hazelcast client will use the configuration file <distro_root>/config/hazelcast-client.yaml
. Adjust that file as
needed.
public class CustomJetJob { public static void main(String[] args) { HazelcastInstance hz = Hazelcast.bootstrappedInstance(); hz.getJet().newJob(buildPipeline()).join(); } public static Pipeline buildPipeline() { // ... } }
public static void setOutOfMemoryHandler(OutOfMemoryHandler outOfMemoryHandler)
OutOfMemoryHandler
to be used when an OutOfMemoryError
is caught by Hazelcast threads.
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 Hazelcast threadsOutOfMemoryError
,
OutOfMemoryHandler
Copyright © 2024 Hazelcast, Inc.. All rights reserved.