com.hazelcast.client
Class HazelcastClient

java.lang.Object
  extended by com.hazelcast.client.HazelcastClient
All Implemented Interfaces:
HazelcastInstance

public class HazelcastClient
extends Object
implements HazelcastInstance

Hazelcast Client enables you to do all Hazelcast operations without being a member of the cluster. It connects to one of the cluster members and delegates all cluster wide operations to it. When the connected cluster member dies, client will automatically switch to another live member.


Method Summary
 void addInstanceListener(InstanceListener instanceListener)
          Add a instance listener which will be notified when a new instance such as map, queue, multimap, topic, lock is added or removed.
protected  void destroy(String proxyName)
           
static Collection<HazelcastClient> getAllHazelcastClients()
           
 AtomicNumber getAtomicNumber(String name)
          Creates cluster-wide atomic long.
 ClientConfig getClientConfig()
           
<K,V,E> Object
getClientProxy(Object o)
           
 ClientService getClientService()
          Returns the client service of this Hazelcast instance.
 Cluster getCluster()
          Returns the Cluster that this Hazelcast instance is part of.
 Config getConfig()
          Returns the configuration of this Hazelcast instance.
 ConnectionManager getConnectionManager()
           
 ICountDownLatch getCountDownLatch(String name)
          Creates cluster-wide CountDownLatch.
 ExecutorService getExecutorService()
          Returns the default distributed executor service.
 ExecutorService getExecutorService(String name)
          Returns the distributed executor service for the given name.
 IdGenerator getIdGenerator(String name)
          Creates cluster-wide unique IDs.
 InRunnable getInRunnable()
           
 Collection<Instance> getInstances()
          Returns all queue, map, set, list, topic, lock, multimap instances created by Hazelcast.
 LifecycleService getLifecycleService()
          Returns the lifecycle service for this instance.
<E> IList<E>
getList(String name)
          Returns the distributed list instance with the specified name.
 ILock getLock(Object obj)
          Returns the distributed lock instance for the specified key object.
 LoggingService getLoggingService()
          Returns the logging service of this Hazelcast instance.
<K,V> IMap<K,V>
getMap(String name)
          Returns the distributed map instance with the specified name.
<K,V> MultiMap<K,V>
getMultiMap(String name)
          Returns the distributed multimap instance with the specified name.
 String getName()
          Returns the name of this Hazelcast instance
 OutRunnable getOutRunnable()
           
 PartitionService getPartitionService()
          Returns the partition service of this Hazelcast instance.
<E> IQueue<E>
getQueue(String name)
          Returns the distributed queue instance with the specified name.
 ISemaphore getSemaphore(String name)
          Creates cluster-wide semaphore.
<E> ISet<E>
getSet(String name)
          Returns the distributed set instance with the specified name.
<E> ITopic<E>
getTopic(String name)
          Returns the distributed topic instance with the specified name.
 Transaction getTransaction()
          Returns the transaction instance associated with the current thread, creates a new one if it wasn't already.
 boolean isActive()
           
static HazelcastClient newHazelcastClient(ClientConfig config)
           
 void removeInstanceListener(InstanceListener instanceListener)
          Removes the specified instance listener.
 void restart()
          Detaches this member from the cluster first and then restarts it as a new member.
 void shutdown()
          Detaches this member from the cluster.
static void shutdownAll()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInRunnable

public InRunnable getInRunnable()

getOutRunnable

public OutRunnable getOutRunnable()

newHazelcastClient

public static HazelcastClient newHazelcastClient(ClientConfig config)
Parameters:
config -
Returns:

getConfig

public Config getConfig()
Description copied from interface: HazelcastInstance
Returns the configuration of this Hazelcast instance.

Specified by:
getConfig in interface HazelcastInstance
Returns:
configuration of this Hazelcast instance

getPartitionService

public PartitionService getPartitionService()
Description copied from interface: HazelcastInstance
Returns the partition service of this Hazelcast instance. PartitionService allows you to introspect current partitions in the cluster, partition owner members and listen for partition migration events.

Specified by:
getPartitionService in interface HazelcastInstance
Returns:
partition service

getClientService

public ClientService getClientService()
Description copied from interface: HazelcastInstance
Returns the client service of this Hazelcast instance. Client service allows you to get information about connected clients.

Specified by:
getClientService in interface HazelcastInstance
Returns:

getLoggingService

public LoggingService getLoggingService()
Description copied from interface: HazelcastInstance
Returns the logging service of this Hazelcast instance. LoggingService allows you to listen for LogEvents generated by Hazelcast runtime. You can log the events somewhere or take action base on the message.

Specified by:
getLoggingService in interface HazelcastInstance
Returns:
logging service

getMap

public <K,V> IMap<K,V> getMap(String name)
Description copied from interface: HazelcastInstance
Returns the distributed map instance with the specified name.

Specified by:
getMap in interface HazelcastInstance
Parameters:
name - name of the distributed map
Returns:
distributed map instance with the specified name

getClientProxy

public <K,V,E> Object getClientProxy(Object o)

getTransaction

public Transaction getTransaction()
Description copied from interface: HazelcastInstance
Returns the transaction instance associated with the current thread, creates a new one if it wasn't already.

Transaction doesn't start until you call transaction.begin() and if a transaction is started then all transactional Hazelcast operations are automatically transactional.

  Map map = Hazelcast.getMap("mymap");
  Transaction txn = Hazelcast.getTransaction();
  txn.begin();
  try {
    map.put ("key", "value");
    txn.commit();
  }catch (Exception e) {
    txn.rollback();
  }
 
Isolation is always READ_COMMITTED . If you are in a transaction, you can read the data in your transaction and the data that is already committed and if not in a transaction, you can only read the committed data. Implementation is different for queue and map/set. For queue operations (offer,poll), offered and/or polled objects are copied to the next member in order to safely commit/rollback. For map/set, Hazelcast first acquires the locks for the write operations (put, remove) and holds the differences (what is added/removed/updated) locally for each transaction. When transaction is set to commit, Hazelcast will release the locks and apply the differences. When rolling back, Hazelcast will simply releases the locks and discard the differences. Transaction instance is attached to the current thread and each Hazelcast operation checks if the current thread holds a transaction, if so, operation will be transaction aware. When transaction is committed, rolled back or timed out, it will be detached from the thread holding it.

Specified by:
getTransaction in interface HazelcastInstance
Returns:
transaction for the current thread

getConnectionManager

public ConnectionManager getConnectionManager()

addInstanceListener

public void addInstanceListener(InstanceListener instanceListener)
Description copied from interface: HazelcastInstance
Add a instance listener which will be notified when a new instance such as map, queue, multimap, topic, lock is added or removed.

Specified by:
addInstanceListener in interface HazelcastInstance
Parameters:
instanceListener - instance listener

getCluster

public Cluster getCluster()
Description copied from interface: HazelcastInstance
Returns the Cluster that this Hazelcast instance is part of. Cluster interface allows you to add listener for membership events and learn more about the cluster that this Hazelcast instance is part of.

Specified by:
getCluster in interface HazelcastInstance
Returns:
cluster that this Hazelcast instance is part of

getExecutorService

public ExecutorService getExecutorService()
Description copied from interface: HazelcastInstance
Returns the default distributed executor service. Executor service enables you to run your Runnables and Callables on the Hazelcast cluster. Note that it don't support invokeAll/Any and don't have standard shutdown behavior

Specified by:
getExecutorService in interface HazelcastInstance
Returns:
distributed executor service of this Hazelcast instance

getExecutorService

public ExecutorService getExecutorService(String name)
Description copied from interface: HazelcastInstance
Returns the distributed executor service for the given name.

Specified by:
getExecutorService in interface HazelcastInstance
Parameters:
name - name of the executor service
Returns:
executor service for the given name

getIdGenerator

public IdGenerator getIdGenerator(String name)
Description copied from interface: HazelcastInstance
Creates cluster-wide unique IDs. Generated IDs are long type primitive values between 0 and Long.MAX_VALUE . Id generation occurs almost at the speed of AtomicLong.incrementAndGet() . Generated IDs are unique during the life cycle of the cluster. If the entire cluster is restarted, IDs start from 0 again.

Specified by:
getIdGenerator in interface HazelcastInstance
Parameters:
name - name of the IdGenerator
Returns:
IdGenerator for the given name

getAtomicNumber

public AtomicNumber getAtomicNumber(String name)
Description copied from interface: HazelcastInstance
Creates cluster-wide atomic long. Hazelcast AtomicNumber is distributed implementation of java.util.concurrent.atomic.AtomicLong.

Specified by:
getAtomicNumber in interface HazelcastInstance
Parameters:
name - name of the AtomicNumber proxy
Returns:
AtomicNumber proxy for the given name

getCountDownLatch

public ICountDownLatch getCountDownLatch(String name)
Description copied from interface: HazelcastInstance
Creates cluster-wide CountDownLatch. Hazelcast ICountDownLatch is distributed implementation of java.util.concurrent.CountDownLatch.

Specified by:
getCountDownLatch in interface HazelcastInstance
Parameters:
name - name of the ICountDownLatch proxy
Returns:
ICountDownLatch proxy for the given name

getSemaphore

public ISemaphore getSemaphore(String name)
Description copied from interface: HazelcastInstance
Creates cluster-wide semaphore. Hazelcast ISemaphore is distributed implementation of java.util.concurrent.Semaphore.

Specified by:
getSemaphore in interface HazelcastInstance
Parameters:
name - name of the ISemaphore proxy
Returns:
ISemaphore proxy for the given name

getInstances

public Collection<Instance> getInstances()
Description copied from interface: HazelcastInstance
Returns all queue, map, set, list, topic, lock, multimap instances created by Hazelcast.

Specified by:
getInstances in interface HazelcastInstance
Returns:
the collection of instances created by Hazelcast.

getList

public <E> IList<E> getList(String name)
Description copied from interface: HazelcastInstance
Returns the distributed list instance with the specified name. Index based operations on the list are not supported.

Specified by:
getList in interface HazelcastInstance
Parameters:
name - name of the distributed list
Returns:
distributed list instance with the specified name

getLock

public ILock getLock(Object obj)
Description copied from interface: HazelcastInstance
Returns the distributed lock instance for the specified key object. The specified object is considered to be the key for this lock. So keys are considered equals cluster-wide as long as they are serialized to the same byte array such as String, long, Integer.

Locks are fail-safe. If a member holds a lock and some of the members go down, cluster will keep your locks safe and available. Moreover, when a member leaves the cluster, all the locks acquired by this dead member will be removed so that these locks can be available for live members immediately.

 Lock lock = Hazelcast.getLock("PROCESS_LOCK");
 lock.lock();
 try {
   // process
 } finally {
   lock.unlock();
 }
 

Specified by:
getLock in interface HazelcastInstance
Parameters:
obj - key of the lock instance
Returns:
distributed lock instance for the specified key.

getMultiMap

public <K,V> MultiMap<K,V> getMultiMap(String name)
Description copied from interface: HazelcastInstance
Returns the distributed multimap instance with the specified name.

Specified by:
getMultiMap in interface HazelcastInstance
Parameters:
name - name of the distributed multimap
Returns:
distributed multimap instance with the specified name

getName

public String getName()
Description copied from interface: HazelcastInstance
Returns the name of this Hazelcast instance

Specified by:
getName in interface HazelcastInstance
Returns:
name of this Hazelcast instance

getQueue

public <E> IQueue<E> getQueue(String name)
Description copied from interface: HazelcastInstance
Returns the distributed queue instance with the specified name.

Specified by:
getQueue in interface HazelcastInstance
Parameters:
name - name of the distributed queue
Returns:
distributed queue instance with the specified name

getSet

public <E> ISet<E> getSet(String name)
Description copied from interface: HazelcastInstance
Returns the distributed set instance with the specified name.

Specified by:
getSet in interface HazelcastInstance
Parameters:
name - name of the distributed set
Returns:
distributed set instance with the specified name

getTopic

public <E> ITopic<E> getTopic(String name)
Description copied from interface: HazelcastInstance
Returns the distributed topic instance with the specified name.

Specified by:
getTopic in interface HazelcastInstance
Parameters:
name - name of the distributed topic
Returns:
distributed topic instance with the specified name

removeInstanceListener

public void removeInstanceListener(InstanceListener instanceListener)
Description copied from interface: HazelcastInstance
Removes the specified instance listener. Returns silently if specified instance listener doesn't exist.

Specified by:
removeInstanceListener in interface HazelcastInstance
Parameters:
instanceListener - instance listener to remove

shutdownAll

public static void shutdownAll()

getAllHazelcastClients

public static Collection<HazelcastClient> getAllHazelcastClients()

shutdown

public void shutdown()
Description copied from interface: HazelcastInstance
Detaches this member from the cluster. It doesn't shutdown the entire cluster, it shuts down this local member only.

Specified by:
shutdown in interface HazelcastInstance
See Also:
HazelcastInstance.getLifecycleService()

isActive

public boolean isActive()

destroy

protected void destroy(String proxyName)

restart

public void restart()
Description copied from interface: HazelcastInstance
Detaches this member from the cluster first and then restarts it as a new member.

Specified by:
restart in interface HazelcastInstance
See Also:
HazelcastInstance.getLifecycleService()

getLifecycleService

public LifecycleService getLifecycleService()
Description copied from interface: HazelcastInstance
Returns the lifecycle service for this instance. LifecycleService allows you to shutdown, restart, pause and resume this HazelcastInstance and listen for the lifecycle events.

Specified by:
getLifecycleService in interface HazelcastInstance
Returns:
lifecycle service

getClientConfig

public ClientConfig getClientConfig()


Copyright © 2008-2012 Hazel Ltd. All Rights Reserved.