LogoLogo
AMPS Java Client 5.3.4
AMPS Java Client 5.3.4
  • Welcome to the AMPS Java Client
    • Before You Start
    • Obtaining and Installing the AMPS Java Client
    • Your First AMPS Program
      • Client Identification
      • Connection Strings for AMPS
      • Connection Parameters for AMPS
      • Providing Credentials to AMPS
    • Subscriptions
      • Content Filtering
        • Changing the Filter on a Subscription
      • Understanding Message Objects
      • Synchronous Message Processing
      • Asynchronous Message Processing
        • Understanding Threading
      • Regular Expression Subscriptions
      • Ending Subscriptions
    • Error Handling
      • Exceptions
      • Exception Types
      • Exception Handling and Asynchronous Message Processing
      • Controlling Blocking with Command Timeout
      • Disconnect Handling
        • Using a Heartbeat to Detect Disconnection
        • Managing Disconnection
        • Replacing Disconnect Handling
      • Unexpected Messages
      • Unhandled Exceptions
      • Detecting Write Failures
      • Monitoring Connection State
    • State of the World
      • SOW and Subscribe
      • Setting Batch Size
      • Managing SOW Contents
      • Client Side Conflation
    • Using Queues
      • Backlog and Smart Pipelining
      • Acknowledging Messages
      • Acknowledgment Batching
      • Returning a Message to the Queue
      • Manual Acknowledgment
      • Samples of Using a Queue
    • Delta Publish and Subscribe
      • Delta Subscribe
      • Delta Pubilsh
    • High Availability
    • AMPS Programming: Working with Commands
    • Utility Classes
    • Advanced Topics
    • Exceptions Reference
    • AMPS Server Documentation
    • API Documentation
Powered by GitBook

Get Help

  • FAQ
  • Legacy Documentation
  • Support / Contact Us

Get AMPS

  • Evaluate
  • Develop

60East Resources

  • Website
  • Privacy Policy

Copyright 2013-2024 60East Technologies, Inc.

On this page
Export as PDF
  1. Welcome to the AMPS Java Client
  2. State of the World

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 also 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.getCommand()) {
        case Message.Command.SOW:
        case Message.Command.Publish:
            /* For each of these messages, addOrUpdateVan() 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.Command.OOF:
            removeVan(message);
        break;
    }
}

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


        /* We issue a sowAndSubscribe() to begin receiving information about all of
         * the active delivery vans in the system. All of the vans in the system
         * now are returned as Messages whose getCommand() returns SOW.
         */
        for (Message message : client.execute(command)) {
                updateVanPosition(message);
        }
    }
    catch (AMPSException aex) {
        System.err.println("TestListener caught exception.");
    }
}

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 sowAndSubscribe:

public class VanPositionUpdater {

    public void invoke(Message message) {
        updateVanPosition(message);
    }

    private void updateVanPosition(Message message) {
        switch (message.getCommand()) {
            case Message.Command.SOW:
            case Message.Command.Publish:
                addOrUpdateVan(message);
                break;
            case Message.Command.OOF:
                removeVan(message);
                break;
        }
    }

    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.
      ...
     }
}


public void subscribeToVanLocation(Client client) {
    try {

        VanPositionUpdater vp = new VanPositionUpdater();
        Command command = new Command("sow_and_subscribe")
                            .setTopic("van_location")
                            .setFilter("/status = 'ACTIVE'")
                            .setBatchSize(100)
                            .setOptions("oof_enabled");

        client.execute(command, vp);

    }
    catch (AMPSException aex) {
        System.err.println("TestListener caught exception.");
    }
}
PreviousState of the WorldNextSetting Batch Size

Last updated 3 months ago