| java.lang.Object org.cougaar.core.blackboard.Subscription org.cougaar.core.blackboard.TodoSubscription
TodoSubscription | public class TodoSubscription extends Subscription (Code) | | A subscription that queues objects, such as Alarms and other non-blackboard
callbacks, for processing in the plugin's "execute()" thread.
TodoSubscriptions are typically used for Alarm callbacks, for example:
public class MyPlugin extends ComponentPlugin {
private TodoSubscription expiredAlarms;
protected void setupSubscriptions() {
expiredAlarms = (TodoSubscription)
blackboard.subscribe(new TodoSubscription("x"));
// wake up in 10 seconds
getAlarmService().addRealTimeAlarm(
new MyAlarm(System.currentTimeMillis() + 10000));
}
protected void execute() {
if (expiredAlarms.hasChanged()) {
System.out.println("Due alarms: "+expiredAlarms.getAddedCollection());
}
}
private class MyAlarm extends AlarmBase {
// optionally add fields here, e.g. data or a Runnable
public MyAlarm(long time) { super(time); }
// don't do work in the "onExpire()" method, since it's the
// AlarmService's callback thread. Instead, put this alarm on our
// "todo" queue, which will asynchronously call our "execute()".
public void onExpire() { expiredAlarms.add(this); }
}
}
This is analogous to Swing's "invokeLater" pattern, for example:
SwingUtilities.invokeLater(new Runnable() {
public void run() { ... }
});
where the Runnable is put on a queue and will be "run()" in the Swing
thread.
|
Constructor Summary | |
public | TodoSubscription(String name) Create a TodoSubscription with the given non-null, unique name.
The name can be an arbitrary, non-null name, so long as the set of
names is unique within each plugin instance.
We need the name to support agent mobility and persistence in cases where
a plugin has more than one TodoSubscription.
For example, say a plugin has two TodoSubscriptions:
protected void setupSubscriptions() {
alpha = (TodoSubscription) blackboard.subscribe(new TodoSubscription("x"));
beta = (TodoSubscription) blackboard.subscribe(new TodoSubscription("y"));
}
and we persist just after calling:
alpha.add(new Foo(1234));
but before we've had a chance to "execute()". |
TodoSubscription | public TodoSubscription(String name)(Code) | | Create a TodoSubscription with the given non-null, unique name.
The name can be an arbitrary, non-null name, so long as the set of
names is unique within each plugin instance.
We need the name to support agent mobility and persistence in cases where
a plugin has more than one TodoSubscription.
For example, say a plugin has two TodoSubscriptions:
protected void setupSubscriptions() {
alpha = (TodoSubscription) blackboard.subscribe(new TodoSubscription("x"));
beta = (TodoSubscription) blackboard.subscribe(new TodoSubscription("y"));
}
and we persist just after calling:
alpha.add(new Foo(1234));
but before we've had a chance to "execute()". This pending object
will be persisted as a pending "inbox envelope" and rehydrated when we
restart the agent. Our "setupSubscriptions()" will be called again, and we
want to make sure that the object is put on the "alpha"
TodoSubscription.getAddedCollection and not on "beta".
We need the name to match the rehydrated "inbox envelope" with the above
... new TodoSubscription("x") ...
since we persist the envelopes, not the subscription instances.
Besides, the "new TodoSubscription" is a brand new subscription instance,
so without the name, we'd have no way to figure out that these two
instances represent the same "todo".
|
add | public void add(Object o)(Code) | | Add an object to the queue of pending objects that will be visible in the
next blackboard transaction's
TodoSubscription.getAddedCollection , and request an
asynchronous plugin "execute()" cycle.
This is analogous to a blackboard "publishAdd", except that the object is
only visible to this subscription and will not be persisted.
Parameters: o - the object to put on the queue, which can be any non-null object(either data or a Runnable) |
apply | public boolean apply(Envelope envelope)(Code) | | For infrastructure use only
|
fill | public void fill(Envelope envelope)(Code) | | For infrastructure use only
|
getAddedCollection | public Collection getAddedCollection()(Code) | | a possibly empty collection of objects that have beenadded since the last transaction. Will not return null. |
getAddedList | public Enumeration getAddedList()(Code) | | an enumeration of the objects that have been addedsince the last transaction. |
privateAdd | protected void privateAdd(Object o, boolean isVisible)(Code) | | |
privateChange | protected void privateChange(Object o, List changes, boolean isVisible)(Code) | | |
privateRemove | protected void privateRemove(Object o, boolean isVisible)(Code) | | |
resetChanges | protected void resetChanges()(Code) | | |
|
|