Interface EntryProcessor<K,V,R>
-
- Type Parameters:
K
- map entry key typeV
- map entry value typeR
- return type
- All Superinterfaces:
java.io.Serializable
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface EntryProcessor<K,V,R> extends java.io.Serializable
An EntryProcessor processes aMap.Entry
. TheEntryProcessor
'sprocess(Map.Entry)
method is executed atomically. This obviates the need to explicitly lock as would be required with aExecutorService
.Performance can be very high as the data is not moved off the Member partition. This avoids network cost and, if the storage format is
InMemoryFormat.OBJECT
, then there is no de-serialization or serialization cost.EntryProcessors execute on the partition thread in a member. Multiple operations on the same partition are queued and executed sequentially.
While executing partition migrations are not allowed. Any migrations are queued on the partition thread.
An EntryProcessor may not be re-entrant i.e. it may not access the same
Map
. Limitation: you can only access data on the same partition.Note that to modify an entry by using EntryProcessors you should explicitly call the
Map.Entry.setValue(V)
method ofMap.Entry
such as:Override public Object process(Map.Entry entry) { Value value = entry.getValue(); // process and modify value // ... entry.setValue(value); return result; }
EntryProcessor instances can be shared between threads. If an EntryProcessor instance contains mutable state, proper concurrency control needs to be provided to coordinate access to mutable state. Another option is to rely on
ThreadLocal
s.Since Hazelcast 4.1, an instance of
ExtendedMapEntry
is provided as argument inprocess(Map.Entry)
method:{@code class IncrementWithOptionalTtl implements EntryProcessor
{ private final long ttlSeconds; public IncrementWithOptionalTtl(long ttlSeconds) { this.ttlSeconds = ttlSeconds; } - See Also:
ExtendedMapEntry
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default EntryProcessor<K,V,R>
getBackupProcessor()
Get the entry processor to be applied to backup entries.R
process(java.util.Map.Entry<K,V> entry)
Process the entry without worrying about concurrency.
-
-
-
Method Detail
-
process
R process(java.util.Map.Entry<K,V> entry)
Process the entry without worrying about concurrency.Note that to modify an entry by using an EntryProcessor you should explicitly call
setValue()
method ofMap.Entry
, for example:@Override public Object process(Map.Entry entry) { Value value = entry.getValue(); // process and modify value // ... entry.setValue(value); return result; }
otherwise theEntryProcessor
does not guarantee to modify the entry.The entry's value will be
null
, if the entry for the key doesn't exist. You can create new mapping by setting a non-null value or remove existing mapping entry by setting the value tonull
.- Parameters:
entry
- entry to be processed- Returns:
- a result that will be returned from the method taking the
EntryProcessor
, such asIMap.executeOnKey()
-
getBackupProcessor
@Nullable default EntryProcessor<K,V,R> getBackupProcessor()
Get the entry processor to be applied to backup entries.In case of a readonly execution,
null
can be returned to indicate that no backups should be made.Note that there is a possibility which an
EntryProcessor
can see that a key exists but its backup processor may not find it at run time due to an unsent backup of a previous operation (e.g. a previous put). In those situations, Hazelcast internally/eventually will sync those owner and backup partitions so you will not lose any data. When coding an backup entry processor, you should take that case into account, otherwiseNullPointerException
s can be seen sinceMap.Entry.getValue()
may return null.- Returns:
- the backup entry processor
-
-