public abstract class EPatternFeed extends EFeed implements ESubscriber, IESubscribeFeed
eBus notification events and an
eBus client, searching for a
client-specified event pattern in the
notification event stream. When a match is found, a
MatchEvent is posted to the client via the configured
notification callback, just like any other notification.
Follow these steps when using a pattern feed:
Step 1: Implement
the ESubscriber interface.
Step 2: Define an
EventPattern.
Step 3: Open a pattern feed for the subscriber and pattern defined above.
Step 4 (optional):
Do not override
ESubscriber interface methods. Instead, set callbacks
using statusCallback(FeedStatusCallback) and/or
notifyCallback(NotifyCallback) passing in Java lambda
expressions.
Step 5: Subscribe to the open pattern feed.
Step 6: Wait for an
up
feed status.
This callback will occur before any MatchEvents are
delivered. If the feed state is down,
then no matches will be delivered until the feed state comes
back up.
Step 7: When the
pattern feed is up, wait for MatchEvents to arrive.
Step 8: When the
subsceriber is shutting down, close the feed.
EPatternFeedimport net.sf.eBus.client.EFeedState;
import net.sf.eBus.client.EPatternFeed;
import net.sf.eBus.client.ESubscriber;
import net.sf.eBus.messages.EventPattern;
import net.sf.eBus.messages.MatchEvent;
Step 1: Implement the ESubscriber interface.
public final class TradeAlgo implements ESubscriber
{
// Monitor this stock symbol.
private final String mSymbol;
// Store the feed here so it can be used to unsubscribe.
private EPatternFeed mFeed;
public TradeAlgo(final String symbol) {
mSymbol = symbol;
}
@Override public void startup() {
Step 2: Define event pattern.
final EventPattern pattern = (see EventPattern to learn how to create a pattern.)
Step 3: Open the pattern feed.
mFeed = EPatternFeed.open(this, pattern);
Step 4: ESubscriber interface method overridden.
Step 5: Subscribe to the pattern feed.
mFeed.subscribe();
}
Step 6: Wait for EFeedState.UP feed status.
@Override public void feedStatus(final EFeedState feedState, final IESubscribeFeed feed) {
// What is the feed state?
if (feedState == EFeedState.DOWN) {
// Down. At least one pattern is down. Expect no matches until all
// subordinate pattern feeds are up.
} else {
// Up. Expect to receive match notifications.
}
}
Step 7: Wait for matches to arrive.
@Override public void notify(final ENotificationMessage msg, final IESubscribeFeed feed) {
final MatchEvent match = (MatchEvent) msg;
Match handling code here.
}
@Override public void shutdown() {
Step 8: When subscriber is shutting down, retract subscription feed.
// mFeed.unsubscribe() is not necessary since close() will unsubscribe.
if (mFeed != null) {
mFeed.close();
mFeed = null;
}
}
}| Modifier and Type | Class and Description |
|---|---|
protected static class |
EPatternFeed.AbstractMatchFrame
A match frame tracks an in-progress match.
|
EFeed.AbstractClientTask, EFeed.FeedScope, EFeed.NotifyTask, EFeed.StatusTask<T extends IEFeed>| Modifier and Type | Field and Description |
|---|---|
protected long |
mAllSubFeedsMask
Bit mask for all subordinate feeds.
|
protected Predicate<MatchEvent> |
mCondition
Match event is posted to subscriber if-and-only-if the
match event satisfies this condition.
|
protected Map<ENotificationMessage,List<EPatternFeed.AbstractMatchFrame>> |
mEventFrames
Maps an accepted notification event to the match frames
in which it appears.
|
protected boolean |
mIsExclusive
Set to
true if an event may be used for only one
match. |
protected NotifyCallback |
mNotifyCallback
Notification message callback.
|
protected String |
mPatternName
Pattern name used as the
MatchEvent subject. |
protected EMessageKey |
mPubKey
Publish match events on this message key.
|
protected FeedStatusCallback<IESubscribeFeed> |
mStatusCallback
Feed status callback.
|
protected long |
mSubFeedMask
Bit mask used to determine if all subordinate feeds are
up.
|
protected List<ESubscribeFeed> |
mSubFeeds
Maps the
feed identifier to the
subordinate notify feed. |
protected BiPredicate<List<ENotificationMessage>,ENotificationMessage> |
mUntil
Collected matching events must satisfy this until
predicate.
|
protected static Logger |
sLogger
Logging subsystem interface.
|
mEClient, mFeedId, mFeedState, mInPlace, mIsActive, mScope, NO_CONDITION, NOTIFY_METHOD| Modifier | Constructor and Description |
|---|---|
protected |
EPatternFeed(EClient client,
EventPattern pattern)
Creates a new eBus pattern feed for the given subscriber
client and event pattern.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
addMapping(ENotificationMessage event,
EPatternFeed.AbstractMatchFrame mf)
Adds a mapping from the given event to the match frame
which now contains the event.
|
protected static boolean |
componentTest(ENotificationMessage e,
EPatternFeed.AbstractMatchFrame mf,
MatchCondition mc) |
protected void |
inactivate()
Retracts all in place, subordinate feed subscriptions.
|
boolean |
isExclusive()
Returns
true if this is an exclusive pattern feed. |
EMessageKey |
key()
Returns a message key with
MatchEvent as the class
and the event pattern name as the subject. |
protected void |
markDefunct(List<ENotificationMessage> events)
Marks all of the match frames associated with the given
events as defunct.
|
protected abstract void |
matchEvent(ENotificationMessage event,
int eventId)
Applies
event to the pattern. |
void |
notifyCallback(NotifyCallback cb)
Puts the
MatchEvent notification message callback
in place. |
static EPatternFeed |
open(ESubscriber client,
EventPattern pattern)
|
void |
statusCallback(FeedStatusCallback<IESubscribeFeed> cb)
Puts the subscription feed status callback in place.
|
void |
subscribe()
Activates this event pattern feed by putting the
subordinate subscription feeds in place.
|
void |
unsubscribe() |
addAllKeys, addKey, checkScopes, clientId, close, defaultDispatcher, eClient, equals, feedId, feedState, findKeys, findKeys, findKeys, hashCode, inPlace, isActive, isFeedUp, isOverridden, loadKeys, location, register, register, register, scope, shutdown, shutdown, shutdownAll, startup, startup, startupAll, storeKeys, storeKeys, storeKeys, toStringclone, finalize, getClass, notify, notifyAll, wait, wait, waitfeedStatus, notifyprotected static final Logger sLogger
protected final String mPatternName
MatchEvent subject.protected final EMessageKey mPubKey
message key subject is
mPatternName.protected final BiPredicate<List<ENotificationMessage>,ENotificationMessage> mUntil
EMessage.timestamp.protected final boolean mIsExclusive
true if an event may be used for only one
match. If this is the case, then all extant match frames
are cleared when a single match completes.
Note: exclusivity applies only within a pattern and not between patterns. Even if two patterns are marked exclusive, a single event may still be used to satisfy those two patterns.
protected final Predicate<MatchEvent> mCondition
true and the pattern is exclusive, that all
related match frames are removed.protected final List<ESubscribeFeed> mSubFeeds
feed identifier to the
subordinate notify feed. These feeds are defined by the
EventPattern.parameters().protected final Map<ENotificationMessage,List<EPatternFeed.AbstractMatchFrame>> mEventFrames
Set to null for non-exclusive.
protected long mAllSubFeedsMask
protected long mSubFeedMask
mAllSubFeedsMask,
then this event feed is up.protected FeedStatusCallback<IESubscribeFeed> mStatusCallback
ESubscriber.feedStatus(EFeedState, IESubscribeFeed).protected NotifyCallback mNotifyCallback
ESubscriber.notify(ENotificationMessage, IESubscribeFeed).protected EPatternFeed(EClient client, EventPattern pattern)
client - post match events to this client.pattern - event pattern.protected abstract void matchEvent(ENotificationMessage event, int eventId)
event to the pattern. If this event
completes the pattern, then a MatchEvent is
created, tested with the match condition, and, if the match
event satisfies the condition, the match event is
forwarded to the subscriber.event - apply this event to the pattern.eventId - unique identifier associated with the
event type.protected final void inactivate()
inactivate in class EFeedpublic final EMessageKey key()
MatchEvent as the class
and the event pattern name as the subject.public final void statusCallback(FeedStatusCallback<IESubscribeFeed> cb)
cb is not null, feed status updates are
passed to cb rather than to
ESubscriber.feedStatus(EFeedState, IESubscribeFeed).
The reverse is true if cb is null. That
is, a null cb means feed status updates are
posted to the
ESubscriber.feedStatus(EFeedState, IESubscribeFeed)
override.statusCallback in interface IESubscribeFeedcb - feed status update callback. May be null.IllegalStateException - if this feed is either closed or subscribed.public final void notifyCallback(NotifyCallback cb)
MatchEvent notification message callback
in place. If cb is not null, then
notification messages will be passed to cb rather
than
ESubscriber.notify(ENotificationMessage, IESubscribeFeed).
A null cb means that notification messages will be
passed to the
ESubscriber.notify(ENotificationMessage, IESubscribeFeed)
override.notifyCallback in interface IESubscribeFeedcb - pass notification messages back to application
via this callback.IllegalStateException - if this feed is either closed or subscribed.public final void subscribe()
Nothing is done if already subscribed.
subscribe in interface IESubscribeFeedIllegalStateException - if this feed is closed or if the client did not override
ESubscriber methods nor put the required callbacks
in place.unsubscribe(),
EFeed.close()public final void unsubscribe()
unsubscribe in interface IESubscribeFeedpublic final boolean isExclusive()
true if this is an exclusive pattern feed.
Exclusive match events do not share events between
instances of the same pattern feed.
Events may be shared between different pattern feeds.
true if the match pattern is exclusive.protected final void addMapping(ENotificationMessage event, EPatternFeed.AbstractMatchFrame mf)
event - event added to the given match frame.mf - match frame containing the event.protected final void markDefunct(List<ENotificationMessage> events)
Marking a match frame as defunct allows frames to be lazily removed from the frames list when encountered during the match process.
events - the events comprising a completely matched,
exclusive pattern.public static EPatternFeed open(ESubscriber client, EventPattern pattern)
client - forward MatchEvents to this
subscriber.pattern - event pattern definition.protected static boolean componentTest(ENotificationMessage e, EPatternFeed.AbstractMatchFrame mf, MatchCondition mc)
Copyright © 2019. All rights reserved.