SOW and Subscribe

Imagine an application that displays real-time information about the position and status of a fleet of delivery vans. When the application starts, it should display the current location of each of the vans, along with their current status. As vans move around the city and post other status updates, the application should keep its display up to date. Vans upload information to the system by posting messages to the van_location topic, configured with a key of van_id on the AMPS server.

In this application, it is important to not only stay up-to-date on the latest information about each van, but to ensure all of the active vans are displayed as soon as the application starts. Combining a SOW with a subscription to the topic is exactly what is needed, and that is accomplished by the AMPS sow_and_subscribe command. Now we will look at an example:

private void UpdateVanPosition(Message message)
{
    switch (message.Command) {
        case Message.Commands.SOW:
        case Message.Commands.Publish:

            // For each of these messages we call AddOrUpdateVan(), that presumably adds the
            // van to our application's display. As vans send updates to the AMPS server, those
            // are also received by the client because of the subscription placed by
            // sowAndSubscribe(). Our application does not need to distinguish between updates
            // and the original set of vans we found via the SOW query, so we use
            // addOrUpdateVan() to display the new position of vans as well.
            AddOrUpdateVan(message);
            break;
        case Message.Commands.OOF:
            RemoveVan(message);
        break;
    }
}

public void SubscribeToVanLocation(Client client) {
    Command command = new Command("sow_and_subscribe")
                            .setTopic("van_location")
                            .setFilter("/status = 'ACTIVE'")
                            .setBatchSize(100)
                            .setOptions("oof");

    // Notice here that we specified an option of "oof". Including this option causes us
    // to receive Out-of-Focus (OOF) messages for the topic.
    // OOF messages are sent when an entry that was sent to us in the past
    // no longer matches our query. This happens when an entry is removed from the SOW
    // cache via a sowDelete() operation, when the entry expires (as specified by the
    // expiration time on the message or by the configuration of that topic on the AMPS
    // server), or when the entry no longer matches the content filter specified. In
    // our case, if a van's status changes to something other than ACTIVE, it no longer
    // matches the content filter, and becomes out of focus. When this occurs, a
    // Message is sent with Command set to oof. We use OOF messages to remove vans from
    // the display as they become inactive, expire, or are deleted.


    foreach (Message msg in client.execute(command))
    {
        updateVanPosition(message);
    }
}

public void addOrUpdateVan(message) {
   // Use information in the message to add the van or update
   // the van position.
}

public void removeVan(message) {
   // Use information in the message to remove information on
   // the van position.
}

Now we will look at an example that uses the asynchronous form of execute to place a sow_and_subscribe command:

private void UpdateVanPosition(Message message)
{
    switch (message.Command) {
        case Message.Commands.SOW:
        case Message.Commands.Publish:
           AddOrUpdateVan(message);
            break;
        case Message.Commands.OOF:
            RemoveVan(message);
        break;
    }
}


public void SubscribeToVanLocation(Client client) {
    Command command = new Command("sow_and_subscribe")
                            .setTopic("van_location")
                            .setFilter("/status = 'ACTIVE'")
                            .setBatchSize(100)
                            .setOptions("oof");

    client.executeAsync(command, msg => UpdateVanPosition(msg));
}

Last updated