State of the World (SOW)
AMPS State of the World (SOW) allows you to automatically keep and query the latest information about a topic on the AMPS server, without building a separate database. Using SOW lets you build impressively high-performance applications that provide rich experiences to users. The AMPS Java client lets you query SOW topics and subscribe to changes with ease.
Performing SOW Queries
To begin, we will look at a simple example of issuing a SOW query.
...
public void executeSOWQuery(Client client) {
for (Message m : client.sow("orders", "/symbol = 'ROL'")) {
if (m.getCommand() == Message.Command.GroupBegin) {
System.out.println("--- Begin SOW Results ---");
}
if (m.getCommand() == Message.Command.GroupEnd) {
System.out.println("--- End SOW Results ---");
}
if (m.getCommand() == Message.Command.SOW) {
System.out.println(m.getData());
}
}
}
...
In the example above, the executeSOWQuery()
method invokes
Client.sow()
to initiate a SOW query on the orders
topic, for
all entries that have a symbol of 'ROL'
.
As the query executes, the body of the loop processes each matching
entry in the topic. Messages containing the data of matching entries
have a Command
of value sow
; so as those arrive, we write them
to the console. AMPS sends a group_begin
message at the beginning of
the results and an group_end
message at the end of the results. We
use those messages to delimit the results of the query.
As with subscribe, the SOW command also provides an asynchronous
version, as well as versions that accept a Command
. For example, the
listing below shows an asynchronous SOW command that specifies the batch
size, or the maximum number of records that AMPS will return at a time.
public void executeSOWQuery(Client client) {
Command command = new Command(Message.Command.SOW)
.setTopic("orders")
.setFilter("/symbol = 'ROL'")
.setBatchSize(100);
client.executeAsync(command, new MessagePrinter());
}
...
public class MessagePrinter implements MessageHandler {
public void invoke(Message m) {
if (m.getCommand() == Message.Command.SOW) {
System.out.println(m.getData());
}
}
}
Samples of Querying a Topic in the SOW
The Java client distribution includes the following samples that demonstrate how to query a topic in the SOW.
Sample Name | Demonstrates |
---|---|
SOWConsolePublisher.java | Publishing messages to a SOW topic. |
SOWConsoleSubscriber.java | Querying messages from a SOW topic. |