Class EPatternFeed
- java.lang.Object
-
- net.sf.eBus.client.EFeed
-
- net.sf.eBus.feed.pattern.EPatternFeed
-
- All Implemented Interfaces:
EObject,ESubscriber,IEFeed,IESubscribeFeed
public abstract class EPatternFeed extends EFeed implements ESubscriber, IESubscribeFeed
eBus pattern feeds sit betweeneBus notification eventsand aneBus client, searching for a client-specifiedevent patternin the notification event stream. When a match is found, aMatchEventis 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
ESubscriberinterface.Step 2: Define an
EventPattern.Step 3: Open a pattern feed for the subscriber and pattern defined above.
Step 4 (optional): Do not override
ESubscriberinterface methods. Instead, set callbacks usingEPatternFeed.PatternBuilder.statusCallback(FeedStatusCallback)and/orEPatternFeed.PatternBuilder.notifyCallback(NotifyCallback)expressions.Step 5: Subscribe to the open pattern feed.
Step 6: Wait for an
upfeed status. This callback will occur before anyMatchEvents are delivered. If the feed state isdown, 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 subscriber is shutting down,
closethe feed.Example use of
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 = (seeEventPatternto 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; } } }- Author:
- Charles W. Rapp
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classEPatternFeed.AbstractMatchFrameA match frame tracks an in-progress match.static classEPatternFeed.PatternBuilder<F extends EPatternFeed,B extends EPatternFeed.PatternBuilder<F,?>>Based class for pattern feed builders.-
Nested classes/interfaces inherited from class net.sf.eBus.client.EFeed
EFeed.AbstractClientTask, EFeed.Builder<F extends EFeed,T extends EObject,B extends EFeed.Builder<F,T,?>>, EFeed.FeedScope, EFeed.NotifyTask, EFeed.StatusTask<T extends IEFeed>
-
-
Field Summary
Fields Modifier and Type Field Description protected longmAllSubFeedsMaskBit mask for all subordinate feeds.protected Predicate<MatchEvent>mConditionMatch event is posted to subscriber if-and-only-if the match event satisfies this condition.protected Map<ENotificationMessage,List<EPatternFeed.AbstractMatchFrame>>mEventFramesMaps an accepted notification event to the match frames in which it appears.protected booleanmIsExclusiveSet totrueif an event may be used for only one match.protected NotifyCallbackmNotifyCallbackNotification message callback.protected StringmPatternNamePattern name used as theMatchEventsubject.protected EMessageKeymPubKeyPublish match events on this message key.protected FeedStatusCallback<IESubscribeFeed>mStatusCallbackFeed status callback.protected longmSubFeedMaskBit mask used to determine if all subordinate feeds are up.protected List<ESubscribeFeed>mSubFeedsMaps thefeed identifierto the subordinate notify feed.protected BiPredicate<List<ENotificationMessage>,ENotificationMessage>mUntilCollected matching events must satisfy this until predicate.protected static org.slf4j.LoggersLoggerLogging subsystem interface.-
Fields inherited from class net.sf.eBus.client.EFeed
FEED_IS_INACTIVE, FEED_NOT_ADVERTISED, mEClient, mFeedId, mFeedState, mInPlace, mIsActive, mScope, NO_CONDITION, NOTIFY_METHOD, NULL_CLIENT
-
Fields inherited from interface net.sf.eBus.client.EObject
NAME_NOT_SET
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedEPatternFeed(EPatternFeed.PatternBuilder builder)Creates a new eBus pattern feed based on builder settings.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidaddMapping(ENotificationMessage event, EPatternFeed.AbstractMatchFrame mf)Adds a mapping from the given event to the match frame which now contains the event.protected static booleancomponentTest(ENotificationMessage e, EPatternFeed.AbstractMatchFrame mf, MatchCondition mc)protected voidinactivate()Retracts all in place, subordinate feed subscriptions.booleanisExclusive()Returnstrueif this is an exclusive pattern feed.EMessageKeykey()Returns a message key withMatchEventas the class and the event pattern name as the subject.protected voidmarkDefunct(List<ENotificationMessage> events)Marks all of the match frames associated with the given events as defunct.protected abstract voidmatchEvent(ENotificationMessage event, int eventId)Applieseventto the pattern.protected voidonEvent(ENotificationMessage event, IESubscribeFeed feed)Runseventthrough the state machine if-and-only-if all the subordinate feeds are up.protected voidonFeedStateUpdate(EFeedState state, IESubscribeFeed feed)Updates the feed bit in the subordinate feed mask.voidsubscribe()Activates this event pattern feed by putting the subordinate subscription feeds in place.voidunsubscribe()-
Methods inherited from class net.sf.eBus.client.EFeed
addAllKeys, addKey, addListener, checkScopes, clientId, close, createDispatcher, defaultDispatcher, eClient, equals, feedId, feedState, findKeys, findKeys, findKeys, hashCode, inPlace, isActive, isFeedUp, isOverridden, loadKeys, location, register, register, register, removeListener, scope, setExhaust, shutdown, shutdown, shutdownAll, startup, startup, startupAll, storeKeys, storeKeys, storeKeys, subclassDistance, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface net.sf.eBus.client.ESubscriber
feedStatus, notify
-
-
-
-
Field Detail
-
sLogger
protected static final org.slf4j.Logger sLogger
Logging subsystem interface.
-
mPatternName
protected final String mPatternName
Pattern name used as theMatchEventsubject.
-
mPubKey
protected final EMessageKey mPubKey
Publish match events on this message key. Themessage key subjectismPatternName.
-
mUntil
protected final BiPredicate<List<ENotificationMessage>,ENotificationMessage> mUntil
Collected matching events must satisfy this until predicate. The collected events list and latest event are the predicate parameters. This condition may be based on any event field but will most likely useEMessage.timestamp.
-
mIsExclusive
protected final boolean mIsExclusive
Set totrueif 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.
-
mCondition
protected final Predicate<MatchEvent> mCondition
Match event is posted to subscriber if-and-only-if the match event satisfies this condition. While this condition may appear to play the same role as a feed condition, the difference is that if this condition returnstrueand the pattern is exclusive, that all related match frames are removed.
-
mSubFeeds
protected final List<ESubscribeFeed> mSubFeeds
Maps thefeed identifierto the subordinate notify feed. These feeds are defined by theEventPattern.parameters().
-
mEventFrames
protected final Map<ENotificationMessage,List<EPatternFeed.AbstractMatchFrame>> mEventFrames
Maps an accepted notification event to the match frames in which it appears. This map is used only for exclusive pattern feeds. When a match frame completely matches the pattern, then this map is used to find the match frames referencing the same events.Set to
nullfor non-exclusive.
-
mStatusCallback
protected final FeedStatusCallback<IESubscribeFeed> mStatusCallback
Feed status callback. If not explicitly set by client, then defaults toESubscriber.feedStatus(EFeedState, IESubscribeFeed).
-
mNotifyCallback
protected final NotifyCallback mNotifyCallback
Notification message callback. If not explicity set by client, then defaults toESubscriber.notify(ENotificationMessage, IESubscribeFeed).
-
mAllSubFeedsMask
protected long mAllSubFeedsMask
Bit mask for all subordinate feeds. Bits are mapped to feed identifiers.
-
mSubFeedMask
protected long mSubFeedMask
Bit mask used to determine if all subordinate feeds are up. When a feed is up, its matching bit is set in this bit mask. When this mask equalsmAllSubFeedsMask, then this event feed is up.
-
-
Constructor Detail
-
EPatternFeed
protected EPatternFeed(EPatternFeed.PatternBuilder builder)
Creates a new eBus pattern feed based on builder settings.- Parameters:
builder- contains pattern feed settings.
-
-
Method Detail
-
matchEvent
protected abstract void matchEvent(ENotificationMessage event, int eventId)
Applieseventto the pattern. If this event completes the pattern, then aMatchEventis created, tested with the match condition, and, if the match event satisfies the condition, the match event is forwarded to the subscriber.- Parameters:
event- apply this event to the pattern.eventId- unique identifier associated with the event type.
-
inactivate
protected final void inactivate()
Retracts all in place, subordinate feed subscriptions.- Specified by:
inactivatein classEFeed
-
key
public final EMessageKey key()
Returns a message key withMatchEventas the class and the event pattern name as the subject.
-
subscribe
public final void subscribe()
Activates this event pattern feed by putting the subordinate subscription feeds in place.Nothing is done if already subscribed.
- Specified by:
subscribein interfaceIESubscribeFeed- Throws:
IllegalStateException- if this feed is closed or if the client did not overrideESubscribermethods nor put the required callbacks in place.- See Also:
unsubscribe(),EFeed.close()
-
unsubscribe
public final void unsubscribe()
- Specified by:
unsubscribein interfaceIESubscribeFeed
-
onFeedStateUpdate
protected final void onFeedStateUpdate(EFeedState state, IESubscribeFeed feed)
Updates the feed bit in the subordinate feed mask. If the feed state is up, then the feed bit is set; otherwise the feed bit is cleared. If the composite subordinate feed mask changes state, then this feed posts an update to its publish state.- Parameters:
state- subordinate feed's new state.feed- subordinate subscription feed.
-
onEvent
protected final void onEvent(ENotificationMessage event, IESubscribeFeed feed)
Runseventthrough the state machine if-and-only-if all the subordinate feeds are up.- Parameters:
event- inbound notification event.feed- event is from this subordinate feed.
-
isExclusive
public final boolean isExclusive()
Returnstrueif 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.
- Returns:
trueif the match pattern is exclusive.
-
addMapping
protected final void addMapping(ENotificationMessage event, EPatternFeed.AbstractMatchFrame mf)
Adds a mapping from the given event to the match frame which now contains the event. Note: this method assumes that the caller previously determined this is an exclusive pattern feed.- Parameters:
event- event added to the given match frame.mf- match frame containing the event.
-
markDefunct
protected final void markDefunct(List<ENotificationMessage> events)
Marks all of the match frames associated with the given events as defunct. Note: this method assumes that the caller previously determined this is an exclusive pattern feed. Also removes each of the listed events from the event-to-match frame map.Marking a match frame as defunct allows frames to be lazily removed from the frames list when encountered during the match process.
- Parameters:
events- the events comprising a completely matched, exclusive pattern.
-
componentTest
protected static boolean componentTest(ENotificationMessage e, EPatternFeed.AbstractMatchFrame mf, MatchCondition mc)
-
-