We'll use EventWeb's Publish/Subscribe feature to publish notifications when the Bulletin Board changes.
We'll publish a simple BBChange message, indicating a post has been added or removed. When it gets the message, the client reloads the bulletin board.
Copy BBoardProvFtry.java to BBoardProvFtryV010.java
Change the copy's class name to BBoardProvFtryV010
We'll only change the name in a couple of critical places. That way it'll be easier to compare the new version with the old one.We will add one outgoing BBChange message.
New Outgoing Message | |
---|---|
Tag | Value |
BBChange | (empty) |
Add a tag for the BBChange message
In the // Tag-value Tags
section:
TAG_BB_CHANGE = "BBChange"
(We don't need to change any of the SelfDescribing fields or methods. The tag and value stuff is for incoming messages and the other things haven't changed.)
Create a global field to hold a reference to a PublishProvFtry.PublishProvImp
private PublishProvFtry.PublishProvImp pubProv = null;
line to the main class private variables sectionUpdate the init method to instantiate the pubProv field
We'll get a reference to the publish provider factory from the service registry. (We need to have a PublishProvFtry in the registry in order to use publish/subscribe. It's already set up in the demos.) If there's no PublishProvFtry in the registry we'll log a warning that change notifications won't be published.
Add a local variable to hold a reference to the PublishProvFtry:
PublishProvFtry factory;
Make sure we have a registry and it contains a PublishProvFtry:
if ((registry != null) && registry.containsClass(PublishProvFtry.class))
Get the reference:
factory = (PublishProvFtry)registry.getObject(PublishProvFtry.class);
Get a PublishProvImp to use:
pubProv = factory.newClient();
If we couldn't get a PublishProvFtry, log a warning:
log.println("BBoardProvFtry WARNING: PublishProvFtry not registered");
log.println("Change notifications will not be published");
Add a synchronized notifyChange method to the BBoardProvFtry main class;
private synchronized void notifyChange()
This method will use pubProv to publish the BBChange message.
if (pubProv != null)
pubProv.publish(new TvList(TAG_BB_CHANGE, ""), null);
Add a call to notifyChange to the end of the addPost
method.
notifyChange();
Add the same call to the end of the removePost
method.
All the client has to do is subscribe to BBChange messages and then request an update when it gets one. We add a couple lines to BBoard.js to do this.
Copy BBoard.js to BBoardV010.js
Add a line to the Revision History of the new version:
Subscribe to BBChange messages
The updateStatus method is called with a status Id of TVB_STS_OPEN when the channel channel first opens. Add a line to the TVB_STS_OPEN clause in updateStatus:
tvbSendMsg("RqstSub", "BBChange");
Request an update when we get a BBChange messages
Add a BBChange clause to the getNewData method:
else if (pair[0] == "BBChange") tvbSendMsg("RqstRsrc", "BBPosts");
(We already have the code that handles the update.)
We don't have to do anything new for deployment. Just replace the BBoardProvFtry and BBoard.js code.