public abstract class AbstractProcessor extends Object implements Processor
Processor
with several levels of convenience:
Processor.init(Outbox, Context)
retains the supplied outbox and the
logger retrieved from the context.
process(n, inbox)
delegates to the
matching tryProcessN
with each item received in the inbox.
tryProcess(int, Object)
to which
the tryProcessN
methods delegate by default. It must be used
for ordinals greater than 4, but it may also be used whenever the
processor doesn't care which edge an item originates from.
emit(...)
methods avoid the need to deal with Outbox
directly.
emitCooperatively(...)
methods handle the boilerplate of
cooperative item emission. They are especially useful in the Processor.complete()
step when there is a collection of items to emit. The Traversers
class contains traversers tailored to simplify the implementation of
complete()
.
TryProcessor
class additionally simplifies the
usage of emitCooperatively()
inside tryProcess()
, in a
scenario where an input item results in a collection of output items.
TryProcessor
is obtained from its factory method
flatMapper(Function)
.
Modifier and Type | Class and Description |
---|---|
protected class |
AbstractProcessor.FlatMapper<T,R>
A helper that simplifies the implementation of
tryProcess(int, Object) for flatMap -like
behavior. |
Processor.Context
Constructor and Description |
---|
AbstractProcessor() |
Modifier and Type | Method and Description |
---|---|
protected void |
emit(int ordinal,
Object item)
Emits the item to the outbox bucket at the supplied ordinal.
|
protected void |
emit(Object item)
Emits the item to all the outbox buckets.
|
protected boolean |
emitCooperatively(int ordinal,
Traverser<?> traverser)
Emits the items obtained from the traverser to the outbox bucket with the
supplied ordinal, in a cooperative fashion: if the outbox reports a
high-water condition, backs off and returns
false . |
protected boolean |
emitCooperatively(Traverser<?> traverser)
Convenience for
emitCooperatively(int, Traverser) which emits to all ordinals. |
protected <T,R> AbstractProcessor.FlatMapper<T,R> |
flatMapper(java.util.function.Function<? super T,? extends Traverser<? extends R>> mapper)
Factory of
AbstractProcessor.FlatMapper . |
protected <T,R> AbstractProcessor.FlatMapper<T,R> |
flatMapper(int outputOrdinal,
java.util.function.Function<? super T,? extends Traverser<? extends R>> mapper)
Factory of
AbstractProcessor.FlatMapper . |
protected com.hazelcast.logging.ILogger |
getLogger()
Returns the logger associated with this processor instance.
|
protected Outbox |
getOutbox()
Returns the outbox received in the
init() method call. |
void |
init(Outbox outbox,
Processor.Context context)
Initializes this processor with the outbox that the processing methods
must use to deposit their output items.
|
protected void |
init(Processor.Context context)
Method that can be overridden to perform any necessary initialization for
the processor.
|
void |
process(int ordinal,
Inbox inbox)
Implements the boilerplate of dispatching against the ordinal,
taking items from the inbox one by one, and invoking the
processing logic on each.
|
protected boolean |
tryProcess(int ordinal,
Object item)
Tries to process the supplied input item, which was received over
the supplied ordinal.
|
protected boolean |
tryProcess0(Object item)
Tries to process the supplied input item, which was received from the
edge with ordinal 0.
|
protected boolean |
tryProcess1(Object item)
Tries to process the supplied input item, which was received from the
edge with ordinal 1.
|
protected boolean |
tryProcess2(Object item)
Tries to process the supplied input item, which was received from the
edge with ordinal 2.
|
protected boolean |
tryProcess3(Object item)
Tries to process the supplied input item, which was received from the
edge with ordinal 3.
|
protected boolean |
tryProcess4(Object item)
Tries to process the supplied input item, which was received from the
edge with ordinal 4.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
complete, completeEdge, isCooperative
public final void init(@Nonnull Outbox outbox, @Nonnull Processor.Context context)
Processor
Processor.process(int, Inbox)
, Processor.completeEdge(int)
, Processor.complete()
).
The default implementation does nothing.
protected void init(@Nonnull Processor.Context context) throws Exception
process()
,
completeEdge()
, complete()
),
but after the outbox
and logger
have been initialized.context
- the context
associated with this processorException
protected final com.hazelcast.logging.ILogger getLogger()
public final void process(int ordinal, @Nonnull Inbox inbox)
protected boolean tryProcess(int ordinal, @Nonnull Object item) throws Exception
false
, in which case it will be called again later with the
same (ordinal, item)
combination.
The default implementation throws an UnsupportedOperationException
.
NOTE: unless the processor doesn't differentiate
between its inbound edges, the first choice should be leaving this method
alone and instead overriding the specific tryProcessN()
methods
for each ordinal the processor expects.
ordinal
- ordinal of the edge that delivered the itemitem
- item to be processedtrue
if this item has now been processed,
false
otherwise.Exception
protected boolean tryProcess0(@Nonnull Object item) throws Exception
false
, in which case it will be called again later with the same
item.
The default implementation delegates to
tryProcess(0, item)
.
item
- item to be processedtrue
if this item has now been processed,
false
otherwise.Exception
protected boolean tryProcess1(@Nonnull Object item) throws Exception
false
, in which case it will be called again later with the same
item.
The default implementation delegates to
tryProcess(1, item)
.
item
- item to be processedtrue
if this item has now been processed,
false
otherwise.Exception
protected boolean tryProcess2(@Nonnull Object item) throws Exception
false
, in which case it will be called again later with the same
item.
The default implementation delegates to
tryProcess(2, item)
.
item
- item to be processedtrue
if this item has now been processed,
false
otherwise.Exception
protected boolean tryProcess3(@Nonnull Object item) throws Exception
false
, in which case it will be called again later with the same
item.
The default implementation delegates to
tryProcess(3, item)
.
item
- item to be processedtrue
if this item has now been processed,
false
otherwise.Exception
protected boolean tryProcess4(@Nonnull Object item) throws Exception
false
, in which case it will be called again later with the same
item.
The default implementation delegates to
tryProcess(4, item)
.
item
- item to be processedtrue
if this item has now been processed,
false
otherwise.Exception
protected final Outbox getOutbox()
init()
method call.protected void emit(int ordinal, @Nonnull Object item)
protected boolean emitCooperatively(int ordinal, @Nonnull Traverser<?> traverser)
false
.
If this method returns false
, then the same traverser must be
retained by the caller and passed again in the subsequent invocation of
this method, so as to resume emitting where it left off.
ordinal
- ordinal of the target buckettraverser
- traverser over items to emitprotected boolean emitCooperatively(@Nonnull Traverser<?> traverser)
emitCooperatively(int, Traverser)
which emits to all ordinals.@Nonnull protected <T,R> AbstractProcessor.FlatMapper<T,R> flatMapper(int outputOrdinal, @Nonnull java.util.function.Function<? super T,? extends Traverser<? extends R>> mapper)
AbstractProcessor.FlatMapper
. The FlatMapper
will emit items to
the given output ordinal.@Nonnull protected <T,R> AbstractProcessor.FlatMapper<T,R> flatMapper(@Nonnull java.util.function.Function<? super T,? extends Traverser<? extends R>> mapper)
AbstractProcessor.FlatMapper
. The FlatMapper
will emit items to
all defined output ordinals.Copyright © 2017 Hazelcast, Inc.. All Rights Reserved.