public final class EPublishFeed extends ENotifyFeed implements IEPublishFeed
EPublishFeed is the application entry point for
publishing notification messages
to subscribers. Follow these steps to use this feed:
Step 1: Implement the EPublisher
interface.
Step 2:
Open
a publish feed for a given EPublisher instance and
type+topic message key.
Step 3 (optional): Do not override
EPublisher interface method. Instead, set callback
using statusCallback(FeedStatusCallback) passing in a
Java lambda expression.
Step 4: Advertise this
publisher to eBus. This allows eBus to match publishers to
subscribers. An optional step is to
set the feed state
to up if the publisher is always able to
publish the message.
Step 5: Wait for the
EPublisher.publishStatus(EFeedState, IEPublishFeed)
callback where the feed state is up.
Attempting to publish before this will result in
publish(ENotificationMessage) throwing an
IllegalStateException.
Step 6: Start publishing notifications. Note
that
updateFeedState(EFeedState) with an up feed state
must be done prior to publishing notifications.
Step 7: When the publisher is shutting down,
retract the notification advertisement
and close the feed.
EPublishFeedimport net.sf.eBus.client.EFeed.FeedScope;
import net.sf.eBus.client.EFeedState;
import net.sf.eBus.client.EPublisher;
import net.sf.eBus.client.EPublishFeed;
import net.sf.eBus.messages.EMessageKey;
import net.sf.eBus.messages.ENotificationMessage;
// Step 1: Implement EPublisher interface.
public class CatalogPublisher implements EPublisher {
public CatalogPublisher(final String subject, final FeedScope scope) {
mKey = new EMessageKey(CatalogUpdate.class, subject); mScope = scope;
mFeed = null;
}
@Override
public void startup() {
try {
// Step 2: Open publish feed.
mFeed = EPublishFeed.open(this, mKey, mScope);
// Step 3: Overriding EPublisher interface methods.
// Step 4: Advertise this publisher to eBus.
mFeed.advertise();
// Inform the world that this publisher's feed state is up.
mFeed.updateFeedState(EFeedState.UP);
} catch (IllegalArgumentException argex) {
// Advertisement failed. Place recovery code here.
}
}
// Step 5: Handle publish status update.
@Override
public void publishStatus(final EFeedState feedState, final EPublishFeed feed) {
EFeedState publishState;
// Are we starting a feed?
if (feedState == EFeedState.UP) {
// Yes. Start publishing notifications on the feed.
publishState = startPublishing();
} else {
// We are stopping the feed. stopPublishing();
}
}
public void updateProduct(final String productName, final Money price, final int stockQty) {
if (mFeed != null && mFeed.isFeedUp()) {
// Step 6: Start publishing notifications.
mFeed.publish(new CatalogUpdate(productName, price, stockQty));
}
}
// Retract the notification feed.
@Override
public void shutdown() {
Step 7: On shutdown either unadvertise or close publish feed.
if (mFeed != null) {
// unadvertise() unnecessary since close() retracts an in-place advertisement.
mFeed.close();
mFeed = null;
}
}
// Starts the notification feed when the feed state is up.
// Return EFeedState.UP if the notification is successfully started;
// EFeedState.DOWN if the feed fails to start.
private EFeedState startPublishing() {
Application-specific code not shown.
}
// Stops the notification feed if up.
private void stopPublishing() {
Application-specific code not shown.
}
// Publishes this notification message class/subject key.
private final EMessageKey mKey;
// Published messages remain within this scope.
private final FeedScope mScope;
// Advertise and publish on this feed.
private EPublishFeed mFeed;
}
The first might be an external device connected to a serial
port, providing intermittent data updates. The eBus publisher
converts that data into notification messages. If the serial
interface is unreliable and goes down, the eBus publisher
calls updateFeedState(EFeedState.DOWN) to inform
subscribers about the fact.
The second is a publisher which also subscribes to one or more other feeds, publishing a value-added notification based on the inbound notifications. If one of the subscribed feeds goes down, then the publisher sets its feed state to down. When the subscribed feed is back up, then the publisher is also back up.
The above scenarios could also be accomplished by having the
publisher undadvertise() and advertise() when
the feed is down and up, respectively. But eBus expends much
effort doing this. It is less effort to leave the
advertisement in place and update the feed state.
| Modifier and Type | Class and Description |
|---|---|
protected static class |
ESingleFeed.FeedType
Enumerates the supported feed types.
|
EFeed.AbstractClientTask, EFeed.FeedScope, EFeed.NotifyTask, EFeed.StatusTask<T extends IEFeed>| Modifier and Type | Field and Description |
|---|---|
protected int |
mActivationCount
Tracks the number of contra-feeds matched to this feed.
|
protected static List<EFeed> |
mAdvertisers
Track the advertised feeds in order to generate
advertise messages. |
protected ESingleFeed.FeedType |
mFeedType
Specifies whether this is a publish, subscribe, request,
or reply feed.
|
protected net.sf.eBus.client.ESubject |
mSubject
The feed interfaces with this eBus subject.
|
mEClient, mFeedId, mFeedState, mInPlace, mIsActive, mScope, NO_CONDITION, NOTIFY_METHOD| Modifier and Type | Method and Description |
|---|---|
int |
activationCount()
Returns the feed activation count.
|
void |
advertise()
Advertises this publisher feed to the associated
notification subject.
|
protected void |
inactivate()
If the feed state is up, then informs the notify subject
that it is now down.
|
boolean |
isAdvertised()
Returns
true if this publish feed is both open and
advertised; otherwise, returns false. |
boolean |
isFeedUp()
Returns
true if the publisher is clear to publish
a notification and false if not clear. |
boolean |
isFeedUp(String subject)
|
EMessageKey |
key()
Returns the feed message key.
|
String |
messageSubject()
Returns the feed
message key subect. |
static EPublishFeed |
open(EPublisher client,
EMessageKey key,
EFeed.FeedScope scope)
Returns a notification publish feed for the specified
notification message class and subject.
|
static EPublishFeed |
open(EPublisher cl,
EMessageKey key,
EFeed.FeedScope scope,
EClient.ClientLocation l,
boolean isMulti)
Returns a notification publish feed for the specified
notification message class and subject.
|
void |
publish(ENotificationMessage msg)
Posts this notification message to all interested
subscribers.
|
EFeedState |
publishState()
Returns the publish state.
|
void |
statusCallback(FeedStatusCallback<IEPublishFeed> cb)
Puts the publish status callback in place.
|
String |
toString()
Returns a containing the feed message key and data member
values.
|
void |
unadvertise()
Retracts this publisher feed from the associated
notification subject.
|
void |
updateFeedState(EFeedState update)
Updates the publish feed state to the given value.
|
addAllKeys, addKey, checkScopes, clientId, close, defaultDispatcher, eClient, equals, feedId, feedState, findKeys, findKeys, findKeys, hashCode, inPlace, isActive, isOverridden, loadKeys, location, register, register, register, scope, shutdown, shutdown, shutdownAll, startup, startup, startupAll, storeKeys, storeKeys, storeKeysprotected static final List<EFeed> mAdvertisers
advertise messages.protected final ESingleFeed.FeedType mFeedType
protected final net.sf.eBus.client.ESubject mSubject
feed type.protected int mActivationCount
protected void inactivate()
inactivate in class EFeedpublic boolean isAdvertised()
true if this publish feed is both open and
advertised; otherwise, returns false.
Note: if true is returned, that does not
mean that the publisher is clear to publish notification
messages. The publisher should only post messages when
isFeedUp() returns true.
isAdvertised in interface IEPublishFeedtrue if this publish feed is open and
advertised.EFeed.isActive(),
isFeedUp(),
publishState()public EFeedState publishState()
EFeed.feedState() which returns
EFeedState.UP if there is any subscriber to this
feed. The publish state specifies whether the publisher is
capable of publishing messages for this feed.publishState in interface IEPublishFeedpublic boolean isFeedUp()
true if the publisher is clear to publish
a notification and false if not clear. When
true is returned, that means that this feed is
1) open, 2) advertised, 3) the publish state is up, and
4) there are subscribers listening to this notification
feed.isFeedUp in interface IEFeedisFeedUp in class EFeedtrue if the publisher feed is up and the
publisher is free to publish notification messages.EFeed.isActive(),
isAdvertised()public boolean isFeedUp(String subject)
true if subject equals this feed's
subject and isFeedUp() is true. Otherwise,
returns false.isFeedUp in interface IEPublishFeedsubject - check if the feed for this subject is up.true if the publisher feed is up for the
specified subject and the publisher is free to publish
notification messages.public static EPublishFeed open(EPublisher client, EMessageKey key, EFeed.FeedScope scope)
client - the application object publishing the
notification. May not be null.key - the notification message class and subject.
May not be null and must reference a notification
message class.scope - whether the feed supports local feeds, remote
feeds, or both.NullPointerException - if any of the arguments are null.IllegalArgumentException - if any of the arguments is invalid.statusCallback(FeedStatusCallback),
advertise()public void statusCallback(FeedStatusCallback<IEPublishFeed> cb)
cb
is not null, publish status updates will be passed
to cb rather than
EPublisher.publishStatus(EFeedState, IEPublishFeed).
The reverse is true if cb is null. That
is, a null cb means publish status updates are
posted to the
EPublisher.publishStatus(EFeedState, IEPublishFeed)
override.statusCallback in interface IEPublishFeedcb - the publish status update callback. May be
null.IllegalStateException - if this feed is either closed or advertised.advertise()public void advertise()
EPublisher.publishStatus(EFeedState, IEPublishFeed)
with an up feed state.
updateFeedState(EFeedState) with an
EFeedState.UP up publish state.
publish(ENotificationMessage) may
be called.advertise in interface IEPublishFeedIllegalStateException - if this feed is closed or the client did not override
EPublisher methods nor put the required callback
in place.unadvertise(),
updateFeedState(EFeedState),
EFeed.close()public void unadvertise()
unadvertise in interface IEPublishFeedIllegalStateException - if this feed was closed and is inactive.advertise(),
EFeed.close()public void updateFeedState(EFeedState update)
update equals the currently stored publish feed
state, nothing is done. Otherwise, the updated value is
stored. If this feed is advertised to the server and
the subscription feed is up, then this update is forwarded
to the subject.updateFeedState in interface IEPublishFeedupdate - the new publish feed state.NullPointerException - if update is null.IllegalStateException - if this feed was closed and is inactive or is not
advertised.public void publish(ENotificationMessage msg)
publish in interface IEPublishFeedmsg - post this message to subscribers.NullPointerException - if msg is null.IllegalArgumentException - if msg message key does not match the feed message
key.IllegalStateException - if this feed is inactive, not advertised or the publisher
has not declared the feed to be up.public static EPublishFeed open(EPublisher cl, EMessageKey key, EFeed.FeedScope scope, EClient.ClientLocation l, boolean isMulti)
This method does not parameter validation since this is
a package private method.
cl - the application object publishing the
notification. May not be null.key - the notification message class and subject.
May not be null and must reference a notification
message class.scope - whether the feed supports local feeds, remote
feeds, or both.l - client location.isMulti - true if this is part of a multiple
key feed. If true, this feed is not added to the
client feed list.public String toString()
public final EMessageKey key()
EPublishFeed, then a
notification
message key is returned.public final String messageSubject()
message key subect.public final int activationCount()
Copyright © 2019. All rights reserved.