/*
* Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.core;
import com.hazelcast.config.
Config;
import com.hazelcast.instance.
HazelcastInstanceFactory;
import com.hazelcast.instance.
OutOfMemoryErrorDispatcher;
import java.util.
Set;
/**
* Factory for {@link HazelcastInstance}'s, a node in a cluster.
*/
public final class
Hazelcast {
private
Hazelcast() {
}
/**
* Shuts down all running Hazelcast Instances on this JVM.
* It doesn't shutdown all members of the
* cluster but just the ones running on this JVM.
*
* @see #newHazelcastInstance(Config)
*/
public static void
shutdownAll() {
HazelcastInstanceFactory.
shutdownAll();
}
/**
* Creates a new HazelcastInstance (a new node in a cluster).
* This method allows you to create and run multiple instances
* of Hazelcast cluster members on the same JVM.
* <p/>
* To shutdown all running HazelcastInstances (all members on this JVM)
* call {@link #shutdownAll()}.
*
* @param config Configuration for the new HazelcastInstance (member)
* @return new HazelcastInstance
* @see #shutdownAll()
* @see #getHazelcastInstanceByName(String)
*/
public static
HazelcastInstance newHazelcastInstance(
Config config) {
return
HazelcastInstanceFactory.
newHazelcastInstance(
config);
}
/**
* Creates a new HazelcastInstance (a new node in a cluster).
* This method allows you to create and run multiple instances
* of Hazelcast cluster members on the same JVM.
* <p/>
* To shutdown all running HazelcastInstances (all members on this JVM)
* call {@link #shutdownAll()}.
*
* Hazelcast will look into two places for the configuration file:
* <ol>
* <li>
* System property: Hazelcast will first check if "hazelcast.config" system property is set to a file path.
* Example: -Dhazelcast.config=C:/myhazelcast.xml.
* </li>
* <li>
* Classpath: If config file is not set as a system property, Hazelcast will check classpath for hazelcast.xml file.
* </li>
* </ol>
* If Hazelcast doesn't find any config file, it will happily start with default configuration (hazelcast-default.xml)
* located in hazelcast.jar.
*
* @return new HazelcastInstance
* @see #shutdownAll()
* @see #getHazelcastInstanceByName(String)
*/
public static
HazelcastInstance newHazelcastInstance() {
return
HazelcastInstanceFactory.
newHazelcastInstance(null);
}
/**
* Returns an existing HazelcastInstance with instanceName.
* <p/>
* To shutdown all running HazelcastInstances (all members on this JVM)
* call {@link #shutdownAll()}.
*
* @param instanceName Name of the HazelcastInstance (member)
* @return HazelcastInstance
* @see #newHazelcastInstance(Config)
* @see #shutdownAll()
*/
public static
HazelcastInstance getHazelcastInstanceByName(
String instanceName) {
return
HazelcastInstanceFactory.
getHazelcastInstance(
instanceName);
}
/**
* Gets or creates the HazelcastInstance with a certain name.
*
* If a Hazelcast with the same name as the configuration exists, then it is returned, otherwise it is created.
*
* @param config the Config.
* @return the HazelcastInstance
* @throws NullPointerException if config is null.
* @throws IllegalArgumentException if the instancename of the config is null or empty.
*/
public static
HazelcastInstance getOrCreateHazelcastInstance(
Config config) {
return
HazelcastInstanceFactory.
getOrCreateHazelcastInstance(
config);
}
/**
* Returns all active/running HazelcastInstances on this JVM.
* <p/>
* To shutdown all running HazelcastInstances (all members on this JVM)
* call {@link #shutdownAll()}.
*
* @return all HazelcastInstances
* @see #newHazelcastInstance(Config)
* @see #getHazelcastInstanceByName(String)
* @see #shutdownAll()
*/
public static
Set<
HazelcastInstance>
getAllHazelcastInstances() {
return
HazelcastInstanceFactory.
getAllHazelcastInstances();
}
/**
* Sets <tt>OutOfMemoryHandler</tt> to be used when an <tt>OutOfMemoryError</tt>
* is caught by Hazelcast threads.
*
* <p>
* <b>Warning: </b> <tt>OutOfMemoryHandler</tt> may not be called although JVM throws
* <tt>OutOfMemoryError</tt>.
* Because error may be thrown from an external (user thread) thread
* and Hazelcast may not be informed about <tt>OutOfMemoryError</tt>.
* </p>
*
* @param outOfMemoryHandler
*
* @see OutOfMemoryError
* @see OutOfMemoryHandler
*/
public static void
setOutOfMemoryHandler(
OutOfMemoryHandler outOfMemoryHandler) {
OutOfMemoryErrorDispatcher.
setHandler(
outOfMemoryHandler);
}
}