

Public Member Functions | |
| Server () | |
| void | onMessage (javax.jms.Message message) |
Static Public Member Functions | |
| static void | main (String[] args) |
Private Member Functions | |
| void | start () |
Private Attributes | |
| Publisher | p = null |
| Subscriber | s = null |
| long | openSubscriptionToken = 0 |
| long | closeSubscriptionToken = 0 |
| boolean | publicationEnabled = false |
| String | status = null |
Static Private Attributes | |
| static final String | TOPIC = "CMW.ALARM_SYSTEM.ALARM_CATEGORY_TREE.CERN.INSTANT" |
This example shows how to setup a server publishing on a topic and listening to CONSUMER_NOTIFICATION events. It enables the actual msg publishing as it gets a CONSUMER_OPEN_NOTIFICATION msg and stops publishing when receives a CONSUMER_CLOSE_NOTIFICATION msg. It also sends an initialization msg to the new subscriber. This is the code that initializes the Subscriber :
<blockquote>
// Build the selector on the notification messages, only interested in notifications related to the topic TOPIC
String selector = new String( NotificationHelper.TOPIC_PROPERTY+" = '"+TOPIC+"'");
try {
// Subscribe to CONSUMER_OPEN_NOTIFICATION and CONSUMER_CLOSE_NOTIFICATION events with the defined selector
openSubscriptionToken = s.subscribe(NotificationHelper.CarrierTopics[NotificationHelper.CONSUMER_OPEN_NOTIFICATION],
this,
selector);
closeSubscriptionToken = s.subscribe(NotificationHelper.CarrierTopics[NotificationHelper.CONSUMER_CLOSE_NOTIFICATION],
this,
selector);
} catch (javax.jms.JMSException je) {
System.out.println("JMSException raised while subscribing to notifications");
je.printStackTrace();
} catch (javax.naming.NamingException ne) {
System.out.println("NamingException raised while subscribing to notifications");
ne.printStackTrace();
}</blockquote>
And this is how notifications are handled :
<blockquote>
public void onMessage(javax.jms.Message message) {
System.out.println("Got a notification");
try {
// Convert the Message into a Notification
Notification n = NotificationHelper.messageToNotification(message);
int type = n.getType();
switch (type) {
case NotificationHelper.CONSUMER_OPEN_NOTIFICATION:
{
// A subscription has been opened
System.out.println("CONSUMER_OPEN_NOTIFICATION");
ConsumerOpenNotification on = (ConsumerOpenNotification)n;
// Get the subscription topic name
String topicName = on.getTopicName();
// Get the subscription identification
String subscriptionId = on.getSubscriptionId();
// Prepare a message to initialize the new subscription with the current 'satus'
javax.jms.TextMessage m = p.createTextMessage();
m.setText(status);
m.setStringProperty(NotificationHelper.SUBSCRIPTION_ID_PROPERTY, subscriptionId);
try {
p.publish(topicName,m);
} catch (javax.naming.NamingException ne) {
System.out.println("NamingException raised while publishing an initialization msg");
ne.printStackTrace();
}
// Enable msg publication, someone is listening
publicationEnabled = true;
break;
}
case NotificationHelper.CONSUMER_CLOSE_NOTIFICATION:
{
System.out.println("CONSUMER_CLOSE_NOTIFICATION");
// Disable msg publication, subscription has been closed
publicationEnabled = false;
break;
}
}
} catch(javax.jms.JMSException e) {
System.out.println("JMSException raised while handling a notification");
e.printStackTrace();
}
}
</blockquote>
| cern::cmw::mom::test::Server::Server | ( | ) | [inline] |
| static void cern::cmw::mom::test::Server::main | ( | String[] | args | ) | [inline, static] |
| void cern::cmw::mom::test::Server::onMessage | ( | javax.jms.Message | message | ) | [inline] |
Method onMessage
| message |
References cern::cmw::mom::pubsub::Publisher::createObjectMessage(), cern::cmw::mom::pubsub::ConsumerOpenNotification::getSelector(), cern::cmw::mom::pubsub::ConsumerOpenNotification::getSubscriptionId(), cern::cmw::mom::pubsub::ConsumerOpenNotification::getTopicName(), cern::cmw::mom::pubsub::Notification::getType(), p, publicationEnabled, cern::cmw::mom::pubsub::Publisher::publish(), and status.
| void cern::cmw::mom::test::Server::start | ( | ) | [inline, private] |
Method start
References cern::cmw::mom::pubsub::Publisher::close(), cern::cmw::mom::pubsub::Subscriber::close(), closeSubscriptionToken, cern::cmw::mom::pubsub::Publisher::createObjectMessage(), openSubscriptionToken, p, publicationEnabled, cern::cmw::mom::pubsub::Publisher::publish(), s, status, cern::cmw::mom::pubsub::Subscriber::subscribe(), TOPIC, and cern::cmw::mom::pubsub::Subscriber::unSubscribe().
Referenced by Server().
long cern::cmw::mom::test::Server::closeSubscriptionToken = 0 [private] |
Referenced by start().
long cern::cmw::mom::test::Server::openSubscriptionToken = 0 [private] |
Referenced by start().
Publisher cern::cmw::mom::test::Server::p = null [private] |
Referenced by onMessage(), Server(), and start().
boolean cern::cmw::mom::test::Server::publicationEnabled = false [private] |
Referenced by onMessage(), and start().
Subscriber cern::cmw::mom::test::Server::s = null [private] |
String cern::cmw::mom::test::Server::status = null [private] |
Referenced by onMessage(), Server(), and start().
final String cern::cmw::mom::test::Server::TOPIC = "CMW.ALARM_SYSTEM.ALARM_CATEGORY_TREE.CERN.INSTANT" [static, private] |
Referenced by start().
1.6.2