Chapter 31 Using the Message Service
Using the message service APIs, you can publish, send, and receive messages.
When you publish a message, a copy is sent to all message queues that have a message selector registered with the specified topic.
A published message includes:
This example illustrates how to publish a message that notifies clients of changes in a stock value. Set the message topic, define the message text, set the message key, define and set message properties, and call publish:
public void notifyStockValue(MessageService cms,
String stock,
double value)
{
String topic = "StockValue." + stock;
String time = new java.util.Date().toString();
String text = time + ": The stock " + stock +
" has value " + value;
Message msg = new Message();
msg.key = cms.getMessageKey();
msg.props = new Property[2];
msg.props[0] = new Property("stock",
new PropertyValue());
msg.props[0].value.stringValue(stock);
msg.props[1] = new Property("value",
new PropertyValue());
msg.props[1].value.doubleValue(value);
msg.replyTo = "";
msg.text = text;
cms.publish(topic, msg, 0);
}
To send a message, you specify the destination message queue. The message service notifies listeners that are registered for the queue and the message remains in the queue until it is received and acknowledged. The send method requires:
When you send a message, the message is replicated when you set the REPLICATED option, or if the recipient cannot be found on the server to which the message is sent.
In this example, we notify a client of a completed order by creating a new message, constructing the message text, and sending the message to the client's queue:
public void notifyOrder(MessageService cms,
String queue,
int orderNo,
String product)
{
String time = new java.util.Date().toString();
String text = "Order " + orderNo + " for product "
+ product + " was completed at " + time;
Message msg = new Message();
msg.key = cms.getMessageKey();
msg.props = new Property[2];
msg.props[0] = new Property("orderNo",
new PropertyValue());
msg.props[0].value.longValue(orderNo);
msg.props[1] = new Property("product",
new PropertyValue());
msg.props[1].value.stringValue(product);
msg.replyTo = "";
msg.text = text;
cms.send(queue, msg, PERSISTENT.value);
}
This code sample gets all the messages from the queue, then for each message, it prints a message receipt and acknowledges the message:
Message[] seq = mq.receive(0, DEFAULT_TIMEOUT.value);
for (int m = 0; m < seq.lengeth; m++;)
{
Message msg = seq[m];
System.out.println("Received message: " + msg.text);
mq.acknowledge(msg.key);
}
The EAServer message service provides a pull-style mechanism for client notification. A client application can check its message queue for new messages. When new messages exist, the client uses the MessageQueue interface to get a list of messages in a queue, receive messages from a queue, and acknowledge the receipt of messages.
The following example is a portion of a client application that checks its message queue for new messages. The application uses the default timeout period. If a new message is available in the queue, the application processes the message with onMessage, and acknowledges its receipt:
public void run();
{
MessageQueue mq = null;
int ok = 2;
while (ok != 0)
{
if (mq == null)
{
mq = _cms.getMessageQueue(_queue,
"[MyThreadPool]",
REQUIRES_ACKNOWLEDGE.value);
}
try
{
Message[] seq = mq.receive(0,
DEFAULT_TIMEOUT.value);
for (int m = 0; m < seq.length; m++)
{
Message msg = seq[m];
_listener.onMessage(msg);
mq.acknowledge(msg.key);
}
ok = 2;
}
}
}
You can find a sample client application in $JAGUAR/html/ir/CtsComponents__MessageQueue.html.
A server component can publish messages, send messages to a queue, receive messages from a queue, and register for message topic notification.
The message service can generate and send regularly scheduled messages to message queues. To receive scheduled messages, add a listener to the message queue and subscribe to topics that define the date or times you want to be notified. In this example, we add a listener to "MyQueue" and subscribe to the topic "second:30", which causes the message service to send a message to MyQueue at 30 seconds past each minute:
cms.addListener("MyQueue", "MyPackage/MyComp");
cms.addSelector("MyQueue", "topic = '<second:30>'");
To request a message be sent to MyQueue at 15 and 45 minutes past each hour, subscribe to these topics:
cms.addSelector("MyQueue", "topic = '<minute:15>'");
cms.addSelector("MyQueue", "topic = '<minute:45>'");
Scheduled message topic names can be either '<minute:NN>' or '<second:NN>'. Additional constraints must include a variable name and value. For example, to request a message every hour between 9am and 9pm, Monday, Wednesday, and Friday, use this syntax:
cms.addSelector("MyQueue", "topic = '<minute:00>'
and day_of_week in (Monday, Wednesday, Friday)
and hour_of_day between 9 and 21");
You can use these variables to define the message topic subscriptions:
The variable names are not case sensitive; minute and MINUTE are equivalent. You can find documentation for the variables, whose names correspond to the constants in the Java class java.util.Calendar, in the Java API Specification .
Scheduled messages are delivered to queues with the appropriate selectors within milliseconds of the specified time. The time at which a component receives a message from the queue, however, depends on the number of unprocessed messages in the queue.
A scheduled message includes two properties that indicate the message creation time, which can be accessed by the component:
Property name |
Datatype |
Format |
---|---|---|
@t |
double |
Number of milliseconds since 1 January 1970 |
ts |
string |
YYYY-MM-DD HH:MM:SS |
Scheduled messages are not saved to persistent storage and they are not replicated. See "Publishing messages" for information about these message options.
You can find the message structure definition in $JAGUAR/html/ir/CtsComponents.html.
Copyright © 2000 Sybase, Inc. All rights reserved. |