Nirvana Python : Asynchronous Exception Listener

Certain methods within the Nirvana Python Client API require synchronous calls to the server. For example the NirvanaSession.getLastEID method will request the most recent event ID that was published onto a Nirvana Channel. This method is required to be synchronous i.e. must block until a response is received. Other methods such as NirvanaSession.publish do not require a response so to make these methods as fast as possible, they are asynchronous.

With synchronous calls, if an exception is thrown on the server e.g. the user does not have permission to get the last event ID then the exception is passed back in the response and thrown on the client.

With asynchronous calls, the client does not wait for a response so if an exception is thrown on the server e.g. the user does not have permission to publish, the client will not know that the event was not successfully published. This is where it is useful to have an Asynchronous Exception Listener.

The Asynchronous Exception Listener will receive notification of exceptions that occurred on the server for asyncrhonous calls. So if the user was not allowed to publish, the listener will be notified with a message indicating this.

Creating a Asynchronous Exception Listener

Asynchronously receiving exceptions requires an object which implements the NirvanaPython.AsyncExceptionListener interface. The interface has one method, onException which is passed a string describing the exception.

class AsyncExceptionListener(NirvanaPython.AsyncExceptionListener):
    def onException(self,message):
        print "Received an exception -> "+message

exceptionListener = AsyncExceptionListener()

Registering the listener for events

In order to register the NirvanaPython.AsyncExceptionListener to receive notification of exceptions, you can call the addAsyncExceptionListener method of NirvanaSession (see Creating a Session).

mySession = NirvanaSession()
mySession.connect("nsp://localhost:9000")
mySession.addAsyncExceptionListener(exceptionListener)