K
- The data type of the Key in the state Map
V
- The data type of the Value in the state Map
public interface StatefulTask<K,V>
Runnable
and Callable
tasks scheduled with an IScheduledExecutorService
. When task implements this interface, the Scheduled Executor
will be able to handle state of the task among the replicas in an event of Migration or Node failure.
Example:
public class CleanUpTask implements Runnable, StatefulTask, HazelcastInstanceAware { private transient HazelcastInstance instance; private transient int recordsDeletedSoFar; public CleanUpTask(HazelcastInstance instance) { this.instance = instance; } public void run() { recordsDeletedSoFar += cleanUpInvalidRecordsAndReturnCount(); } private int cleanUpInvalidRecordsAndReturnCount() { } public void save(Map snapshot) { snapshot.put("recordsDeletedSoFar", recordsDeletedSoFar); } public void load(Map snapshot) { if (state.containsKey("recordsDeletedSoFar")) { recordsDeletedSoFar = snapshot.get("recordsDeletedSoFar"); } } }
Modifier and Type | Method and Description |
---|---|
void |
load(Map<K,V> snapshot)
Task callback to initialize its inner state, after a replica promotion, from the given map.
|
void |
save(Map<K,V> snapshot)
Task callback to capture its state on the provided map.
|
void save(Map<K,V> snapshot)
Runnable.run()
or Callable.call()
to capture a snapshot of the state and publish to replicas. If two or more replicas of the
same task run at the same time and publish their states, then only the one running on the owner
member will be allowed to update the replicas.
Note: The state of the cluster is not known or guaranteed during task's execution, thus,
publication of the task's state to replicas is done on best-effort basis.
Called immediately after run() or call() of the Runnable
or Callable
respectively.snapshot
- The Map
responsible for holding a snapshot of the current state.void load(Map<K,V> snapshot)
Runnable.run()
or Callable.call()
to setup task's state as published from
the previous owner of the task in the cluster.
load will not be called if the snapshot is empty.snapshot
- The Map
responsible for providing a snapshot of the task's state.Copyright © 2018 Hazelcast, Inc.. All Rights Reserved.