public abstract class Activity
extends java.lang.Object
implements java.io.Serializable
Events
. Each Activity
represents an action that is to be performed by the application, i.e. run a task or process
some Events
.
This class is the base class for all application activities. Applications should extend this class and implement the
initialize
, process
and cleanup
methods.
After creating an Activity, it can be submitted to Constellation using Constellation.submit(ibis.constellation.Activity)
. During this submit, a
globally unique ActivityIdentifier
will be assigned to the Activity. Using setIdentifier
this identifier will
be stored in the Activity. Use the identifier
method to retrieve it.
The Activity will be scheduled to run on a Constellation that matches the Context
. When it starts running,
initialize
will be invoked once. This method may perform any processing that is needed, but should not block
indefinitely as this may result in deadlocks. When finished, the method should either return FINISH
of SUSPEND
.
By returning FINISH
the Activity indicates it no further processing is needed. By returning SUSPEND
the
Activity indicates it expects an Event
. The Activity is then suspend by Constellation, until the Event
arrives.
Upon arrival of the Event
, the process
method will be invoked. After processing the event, process
can
indicate if more events are expected (by returning SUSPEND
or if the Activity is done (by returning FINISH
.
After FINISH
is returned by either initialize
of process
, the cleanup
method is invoked. After
this method returns, the Activity is finished and will nore receive any more processingtime.Modifier and Type | Field and Description |
---|---|
static int |
FINISH
Value to be returned by
initialize(ibis.constellation.Constellation) or process(ibis.constellation.Constellation, ibis.constellation.Event) when no further processing is needed. |
static int |
SUSPEND
Value to be returned by
initialize(ibis.constellation.Constellation) or process(ibis.constellation.Constellation, ibis.constellation.Event) when (further) events are expected. |
Constructor and Description |
---|
Activity(AbstractContext context,
boolean expectsEvents)
Create an Activity with a specified context, and specify if it expects to receive Events.
|
Activity(AbstractContext context,
boolean mayBeStolen,
boolean expectsEvents)
Create an Activity with a specified context, and indicate if this Activity may be stolen by other Constellations, and if it
expects to receive Events.
|
Modifier and Type | Method and Description |
---|---|
abstract void |
cleanup(Constellation constellation)
The implementation of this method is called when the activity is finished.
|
boolean |
expectsEvents()
Returns if this Activity expects
Event s. |
AbstractContext |
getContext()
Returns the
AbstractContext of this Activity. |
ActivityIdentifier |
identifier()
Returns the globally unique
ActivityIdentifier assigned to this Activity, or null if this Activity has
not been submitted yet. |
abstract int |
initialize(Constellation constellation)
The implementation of this method should perform the initial processing when the activity is first activated.
|
boolean |
mayBeStolen()
Returns if this Activity may be stolen by another
Constellation . |
abstract int |
process(Constellation constellation,
Event event)
The implementation of this method is called when an event is received for this activity.
|
void |
setIdentifier(ActivityIdentifier identifier)
This is a callback method used by the Constellation to assign a globally unique
ActivityIdentifier to this
Activity. |
public static final int FINISH
initialize(ibis.constellation.Constellation)
or process(ibis.constellation.Constellation, ibis.constellation.Event)
when no further processing is needed.public static final int SUSPEND
initialize(ibis.constellation.Constellation)
or process(ibis.constellation.Constellation, ibis.constellation.Event)
when (further) events are expected.public Activity(AbstractContext context, boolean mayBeStolen, boolean expectsEvents)
context
- the context in which this activity should be run.mayBeStolen
- if this activity may be stolen by other ConstellationsexpectsEvents
- if this Activity expects eventspublic Activity(AbstractContext context, boolean expectsEvents)
context
- the context in which this activity should be run.expectsEvents
- if this Activity expects eventspublic void setIdentifier(ActivityIdentifier identifier)
ActivityIdentifier
to this
Activity. This method may only be invoked once, since the identifier may not change once set. Any subsequent invocation
will result in an IllegalStateException
.identifier
- the globally unique ActivityIdentifier
public ActivityIdentifier identifier()
ActivityIdentifier
assigned to this Activity, or null
if this Activity has
not been submitted yet.ActivityIdentifier
of this Activity or null
.public AbstractContext getContext()
AbstractContext
of this Activity.AbstractContext
of this Activity.public boolean mayBeStolen()
Constellation
.Constellation
.public boolean expectsEvents()
Event
s.Event
s.public abstract int initialize(Constellation constellation)
SUSPEND
or FINISH
, depending on what the activity is to do next:
SUSPEND
when it wants to wait for events, and FINISH
when it is done.
Note that this method does not throw checked exceptions. It can, however, throw runtime exceptions or errors, and the
Constellation
running this Activity will deal with that.constellation
- the Constellation
on which the Activity is runningFINISH
if this Activity is done or SUSPEND
if this Activity expects events.public abstract int process(Constellation constellation, Event event)
SUSPEND
or FINISH
, depending on what the activity is to do next: SUSPEND
when it
expects other events, and FINISH
when it is done.
This method is invoked once at a time, even if more events arrive more or less simultaneously.
Note that this method does not throw checked exceptions. It can, however, throw runtime exceptions or errors, and the
Constellation
running this Activity will deal with that.constellation
- the Constellation
on which the Activity is runningevent
- the Event
to process.FINISH
if this Activity is done or SUSPEND
if this Activity expects events.public abstract void cleanup(Constellation constellation)
Constellation
running this Activity will deal with that.constellation
- the Constellation
on which the Activity is running