5. Error Handling

In every distributed system, the robustness of your application depends on its ability to recover gracefully from unexpected events. The AMPS client provides the building blocks necessary to ensure your application can recover from the kinds of errors and special events that may occur when using AMPS.

Exceptions

Generally speaking, when an error occurs that prohibits an operation from succeeding, AMPS will throw an exception. AMPS exceptions universally derive from com.crankuptheamps.client.exception.AMPSException, so by catching AMPSException, you will be sure to catch anything AMPS throws. For example:

import com.crankuptheamps.client.Client;
import com.crankuptheamps.client.exception.AMPSException;

public void readAndEvaluate(Client client) {
    Scanner scanner = new Scanner(System.in);
    String payload = scanner.nextLine();

    // write a new message to AMPS
    if ( payload != null) {
        try {
            client.publish("UserMessage", " { \"data\" : \"" + payload + "\" }");
        }
        catch (AMPSException e) {
            System.err.println("An AMPS exception occurred: " + e.toString());
            e.printStackTrace();
        }
    }
}

Example 5.1: Catching an AMPSException

In this example, if an error occurs the program writes the error to stderr, and the publish() command fails. However, client is still usable for continued publishing and subscribing. When the error occurs, the exception is written stderr, which calls the exception’s toString() method. As with most Java exceptions, toString() will convert the Exception into a string that includes a message. The printStackTrace() method will write to stderr the stack trace and information on any ”inner” exceptions (exceptions from outside of AMPS that caused AMPS to throw an exception).

AMPS exception types vary based on the nature of the error that occurs. In your program, if you would like to handle certain kinds of errors differently than others, you can catch the appropriate subclass of AMPSException to detect those specific errors and do something different.

public CommandId CreateNewSubscription(Client client) {
    CommandId id = null;
    string topicName;
    while (id == null) {
        /* our program is an interactive program that attempts to retrieve a topic
         * name (or regular expression) from the user.
         */
        topicName = askUserForTopicName();
        try {
            Command command = new Command("subscribe").setTopic(topicName);
            MessagePrinter mp = new MessagePrinter();
            id = client.executeAsync(command, mp);
        }
        catch(BadRegexTopicException ex) {
            /* This line indicates that the program catches the BadRegexTopicException
             * exception and displays a specific error to the user indicating the topic
             * name or expression was invalid. By not returning from the function in
             * this catch block, the while loop runs again and the user is asked for
             * another topic name.
             */
            System.err.println("Error: bad topic name " +
                "or regular expression " + topicName +
                ". The error was: " + ex.toString());
            // we’ll ask the user for another topic
        }
        catch(AMPSException ex) {
        /* If an AMPS exception of a type other than BadRegexTopicException is thrown
         * by AMPS, it is caught here. In that case, the program emits a different
         * error message to the user.
         */
            System.err.println("Error: error setting " +
                "up subscription to topic " +
                topicName + ". The error was: " +
                ex.toString());

            return null; // give up
        }
    }
    return id;
}

Example 5.2: Catch AMPSException Subclasses

Exception Types

Each method in AMPS documents the kinds of exceptions that it can throw. For reference, Table 1.A contains a list of all of the exception types you may encounter while using AMPS, when they occur, and what they mean.

Exception Handling and Asynchronous Message Processing

When using asynchronous message processing, exceptions thrown from the message handler are silently absorbed by the AMPS Java client by default. The AMPS Java client allows you to register an exception listener to detect and respond to these exceptions. When an exception listener is registered, AMPS will call the exception listener with the exception. See Example 5.6 for details.

Controlling Blocking with Command Timeout

The named convenience methods and the Command class provide a timeout setting that specifies how long the command should wait to receive a processed acknowledgment from AMPS. This can be helpful in cases where it is important for the caller to limit the amount of time to block waiting for AMPS to acknowledge the command. If the AMPS client does not receive the processed acknowledgment within the specified time, the client sends an unsubscribe command to the server to cancel the command and throws an exception.

Acknowledgments from AMPS are processed by the client receive thread on the same socket as data from AMPS. This means that any other data previously returned (such as the results of a large query) must be consumed before the acknowledgment can be processed. An application that submits a set of SOW queries in rapid succession should set a timeout that takes into account the amount of time required to process the results of the previous query.

Disconnect Handling

Every distributed system will experience occasional disconnections between one or more nodes. The reliability of the overall system depends on an application’s ability to efficiently detect and recover from these disconnections. Using the AMPS Java client’s disconnect handling, you can build powerful applications that are resilient in the face of connection failures and spurious disconnects.

The HAClient class, included with the AMPS Java client, contains a disconnect handler and other features for building highly-available applications. 60East recommends using the HAClient for automatic reconnection wherever possible, as the HAClient disconnect handler has been carefully crafted to handle a wide variety of edge cases and potential failures. This section covers the use of a custom disconnect handler in the event that the behavior of the HAClient does not suit the needs of your application.

AMPS disconnect handling gives you the ultimate in control and flexibility regarding how to respond to disconnects. Your application gets to specify exactly what happens when an exception occurs by supplying a function to Client.setDisconnectHandler(), which is invoked whenever a disconnect occurs.

Example 5.3 shows the basics:

public class TestDisconnect {
    private String _uri = null;

    public TestDisconnect(String uri) throws AMPSException {
        this._uri = uri;
        Client client = new Client("TestDiscon-Client");
        TestDisconnectHandler dh = new TestDisconnectHandler();

        client.setDisconnectHandler(dh);
        client.connect(this._uri);

        for (Message msg : client.subscribe("order") )
        {
           // ...
        }
    }

    class TestDisconnectHandler implements ClientDisconnectHandler{
        public void invoke(Client client) {
            try{
                client.connect(_uri);
                client.logon();
                Thread.sleep(5000);
            } catch(Exception e){;}
        }
    }

    class MsgPrinter implements MessageHandler
    {
        public void invoke(Message m) {
            System.out.println(m.getData());
        }
    }
}

Example 5.3: Supplying a Disconnect Handler

By creating a more advanced disconnect handler, you can implement logic to make your application even more robust. For example, imagine you have a group of AMPS servers configured for high availability—you could implement fail-over by simply trying the next server in the list until one is found. Example 5.4 shows a brief example.

public class TestDisconnect {
    private String[] _uris;
    private int _currentUri = 0;
    ...

    public TestDisconnect(String[] uris) throws AMPSException {

        /* Here our application is configured with an array of AMPS server URIs to choose
         * from, instead of a single URI. These will be used in the ConnectToNextUri()
         * method as explained below.
         */
        _uris = uris;
        Client client = new Client("TestDiscon-Client");
        TestDisconnectHandler dh = new TestDisconnectHandler();
        client.setDisconnectHandler(dh);

        /* ConnectToNextUri() is invoked by our disconnect handler TestDisconnectHandler.
         * Since our client is currently disconnected, we manually invoke our disconnect
         * handler to initiate the first connection.
         */
        ConnectToNextUri(client);
    }

    private void ConnectToNextUri(Client client) {
        /* In our disconnect handler, we invoke ConnectToNextUri(), which loops around
         * the array of URIs, attempting to connect to each one. In the invoke()
         * method, it attempts to connect to the current URI and, if it is successful,
         * returns immediately. If the connection attempt fails, the exception
         * handler for AMPSException is invoked. In the exception handler, we advance
         * to the next URI, display a warning message, and continue around the loop.
         * This simplistic handler never gives up, but in a typical implementation,
         * you would likely at some point stop attempting to reconnect.
         */
        while(true) {
            try {
                MsgPrinter mp = new MsgPrinter();
                client.connect(_uris[_currentUri]);
                client.logon();
                Command command = new Command("subscribe").setTopic("orders");

                /* At this point the registers a subscription to the server we have
                 * connected to. It is important to note that, once a new server is
                 * connected, it the responsibility of the application to re-establish
                 * any subscriptions placed previously. This behavior provides an
                 * important benefit to your application: one reason for disconnect is
                 * due to a client’s inability to keep up with the rate of message flow.
                 * In a more advanced disconnect handler, you could choose to not
                 * re-establish subscriptions that are the cause of your application’s
                 * demise.
                 */
                client.execute(command, mp);
                return;
            }
            catch (Exception e) {
                _currentUri = (_currentUri + 1) % _uris.length;
                System.err.println("Connection failed " +
                    e.toString() + ". Failing over to " +
                    _uris[_currentUri]);
            }
        }
    }

    class TestDisconnectHandler implements ClientDisconnectHandler {
        public void invoke(Client client) {
            try {
                ConnectToNextUri(client);
            }
            catch(Exception e) {;}
        }
    }

    class MsgPrinter implements MessageHandler {
        public void invoke(Message m) {
            System.out.println(m.getData());
        }
    }
}

Example 5.4: Simple Client Failover Implementation

Using a Heartbeat to Detect Disconnection

The AMPS client includes a heartbeat feature to help applications detect disconnection from the server within a predictable amount of time. Without using a heartbeat, an application must rely on the operating system to notify the application when a disconnect occurs. For applications that are simply receiving messages, it can be impossible to tell whether a socket is disconnected or whether there are simply no incoming messages for the client.

When you set a heartbeat, the AMPS client sends a heartbeat message to the AMPS server at a regular interval, and waits a specified amount of time for the response. If the operating system reports an error on send, or if the server does not respond within the specified amount of time, the AMPS client considers the server to be disconnected.

The AMPS client processes heartbeat messages on the client receive thread, which is the thread used for asynchronous message processing. If your application uses asynchronous message processing and occupies the thread for longer than the heartbeat interval, the client may fail to respond to heartbeat messages in a timely manner and may be disconnected by the server.

Unexpected Messages

The AMPS Java client handles most incoming messages and takes appropriate action. Some messages are unexpected or occur only in very rare circumstances. The AMPS Java client provides a way for clients to process these messages. Rather than providing handlers for all of these unusual events, AMPS provides a single handler function for messages that can’t be handled during normal processing.

Your application registers this handler by setting the lastChanceMessageHandler for the client. This handler is called when the client receives a message that can’t be processed by any other handler. This is a rare event, and typically indicates an unexpected condition.

For example, if a client publishes a message that AMPS cannot parse, AMPS returns a failure acknowledgment. This is an unexpected event, so AMPS does not include an explicit handler for this event, and failure acknowledgments are received in the method registered as the lastChanceMessageHandler.

Your application is responsible for taking any corrective action needed. For example, if a message publication fails, your application can decide to republish the message, publish a compensating message, log the error, stop publication altogether, or any other action that is appropriate.

Unhandled Exceptions

In the AMPS Java client, exceptions can occur that are not thrown to the user. For example, when an exception occurs in the process of reading subscription data from the AMPS server, the exception occurs on a thread inside of AMPS. Consider the following example:

public class MyApp {
    ...
    public static void waitToBePoked(Client client) {
        Command command = new Command(
            "subscribe"
        ).setTopic(
            "pokes"
        ).setFilter(
            "/Pokee LIKE'" + System.getProperty("user.name") + "-.*"
        ).setTimeout(5000);
        client.execute(command, new MsgPrinter());

        Console c = System.console();
        Reader r = c.reader();
        while(r.read() == null) {
            Thread.sleep(10);
        }
    }

    class MsgPrinter implements MessageHandler {
        public void invoke(Message m) {
            System.out.println(m.getData());
        }
    }
}

Example 5.5: Where do Exceptions go?

In this example, we set up a simple subscription to wait for messages on the “pokes” topic, whose “Pokee” tag begins with our user name. When messages arrive, we print a message out to the console, but otherwise our application waits for a key to be pressed.

Inside of the AMPS client, the client creates a new thread of execution that reads data from the server, and invokes message handlers and disconnect handlers when those events occur. When exceptions occur inside this thread, however, there is no caller for them to be thrown to, and by default they are ignored.

In applications where it is important to deal with every issue that occurs in using AMPS, you can set an ExceptionHandler via Client.setExceptionHandler() that receives these otherwise-unhandled exceptions. Making the modifications shown in Example 5.6 to our previous example will allow those exceptions to be caught and handled. In this case we are simply printing those caught exceptions out to the console.

public class MyApp {
    ...
    client.setExceptionListener(new CustomExceptionListener());
    ...
}

class CustomExceptionListener implements ExceptionListener
{
    public void exceptionThrown(Exception ex) {
        System.out.println(ex.toString());
    }
}

Example 5.6: Exception Listener

In this example we have added a call to setExceptionHandler(), registering a simple function that writes the text of the exception out to the console. Even though our application waits for a user to press a key, messages to the console will still be produced, both as incoming “poke” topics arrive, and as issues arise inside of AMPS.

If your application will attempt to recover from an exception thrown on the background processing thread, your application should set a flag and attempt recovery on a different thread than the thread that called the exception listener.

Tip

At the point that the AMPS client calls the exception listener, it has handled the exception. Your exception listener must not rethrow the exception (or wrap the exception and throw a different exception type).

Detecting Write Failures

The publish methods in the Java client deliver the message to be published to AMPS and then return immediately, without waiting for AMPS to return an acknowledgment. Likewise, the sowDelete methods request deletion of SOW messages, and return before AMPS processes the message and performs the deletion. This approach provides high performance for operations that are unlikely to fail in production. However, this means that the methods return before AMPS has processed the command, without the ability to return an error in the event that the command fails.

The AMPS Java client provides a FailedWriteHandler that is called when the client receives an acknowledgment that indicates a failure to persist data within AMPS. To use this functionality, you implement the FailedWriteHandler interface, construct an instance of your new class, and register that instance with the setFailedWriteHandler() method on the client. When a persisted acknowledgment returns that indicates a failed write, AMPS calls the registered handler method with information from the acknowledgment message, supplemented with information from the client publish store (if one is available). Your client can log this information, present an error to the user, or take whatever action is appropriate for the failure.

If your application needs to know whether publishes succeeded and are durably persisted, the following approach is recommended:

  • Set a PublishStore on the client. This will ensure that messages are retransmitted if the client becomes disconnected before the message is acknowledged and request persisted acknowledgments for messages.
  • Install a FailedWriteHandler. In the event that AMPS reports an error for a given message, that event will be reported to the FailedWriteHandler.
  • Call publishFlush() and verify that all messages are persisted before the application exits.

When no FailedWriteHandler is registered, acknowledgments that indicate errors in persisting data are treated as unexpected messages and routed to the LastChanceMessageHandler. In this case, AMPS provides only the acknowledgment message and does not provide the additional information from the client publish store (even when one is available).

Monitoring Connection State

The AMPS client interface provides the ability to set one or more connection state listeners. A connection state listener is a callback that is invoked when the AMPS client detects a change to the connection state.

A connection state listener may be called from the client receive thread. An application should not submit commands to AMPS from a connection state listener, or the application risks creating a deadlock for commands that wait for acknowledgement from the server.

The AMPS client provides the following state values for a connection state listener:

State Indicates
Connected

The client has established a connection to AMPS. If you are using a Client, this is delivered when connect() is successful.

If you are using an HAClient, this state indicates that the connect part of the connect and logon process has completed. An HAClient using the default disconnect handler will attempt to log on immediately after delivering this state.

Most applications that use Client will attempt to log on immediately after the call to connect() returns.

An application should not submit commands to AMPS from the connection state listener while the client is in this state unless the application knows that the state has been delivered from a Client and that the Client does not call logon().

LoggedOn

The client has successfully logged on to AMPS. If you are using a Client, this is delivered when logon() is successful.

If you are using an HAClient, this state indicates that the logon part of the connect and logon process has completed.

This state is delivered after the client is logged on, but before recovery of client state is complete. Recovery will continue after delivering this state: the application should not submit commands to AMPS from the connection state listener while the client is in this state if further recovery will take place.

HeartbeatInitiated

The client has successfully started heartbeat monitoring with AMPS. This state is delivered if the application has enabled heartbeating on the client.

This state is delivered before recovery of the client state is complete. Recovery may continue after this state is delivered. The application should not submit commands to AMPS from the connection state listener until the client is completely recovered.

PublishReplayed

Delivered when a client has completed replay of the publish store when recovering after connecting to AMPS.

This state is delivered when the client has a PublishStore configured.

If the client has a subscription manager set, (which is the default for an HAClient), the application should not submit commands from from the connection state listener until the Resubscribed state is received.

Resubscribed

Delivered when a client has re-entered subscriptions when recovering after connecting to AMPS.

This state is delivered when the client has a subscription manager set (which is the default for an HAClient). This is the final recovery step. An application can submit commands to AMPS from the connection state listener after receiving this state.

Disconnected The client is not connected. For an HAClient, this means that the client will attempt to reconnect to AMPS. For a Client, this means that the client will invoke the disconnect handler, if one is specified.
Shutdown The client is shut down. For an HAClient, this means that the client will no longer attempt to reconnect to AMPS. This state is delivered when close() is called on the client or when a server chooser tells the HAClient to stop reconnecting to AMPS.

Table 5.1: ConnectionStateListener values

The enumeration provided for the connection state listener also includes a value of UNKNOWN, for use as a default or to represent additional states in a custom Client implementation. The 60East implementations of the client do not deliver this state.

The following table shows examples of the set of states that will be delivered during connection, in order, depending on what features of the client are set. Notice that, for an instance of the Client class, this table assumes that the application calls both connect() and logon(). For an HAClient, this table assumes that the HAClient is using the default DisconnectHandler for the HAClient.

Configuration States

subscription manager

publish store

Connected

LoggedOn

PublishReplayed

Resubscribed

subscription manager

publish store

heartbeat set

Connected

LoggedOn

HeartbeatInitiated

PublishReplayed

Resubscribed

subscription manager

Connected

LoggedOn

Resubscribed

subscription manager

heartbeat set

Connected

LoggedOn

HeartbeatInitiated

Resubscribed

(default Client
configuration)

Connected

LoggedOn

Table 5.2: Sequence of states for connection