org.avis.client
Class Elvin

java.lang.Object
  extended by org.avis.client.Elvin
All Implemented Interfaces:
Closeable

public final class Elvin
extends Object
implements Closeable

The core class in the Avis client library which manages a client's connection to an Elvin router. Typically a client creates a connection and then subscribes to notifications and/or sends them.

Example:

 Elvin elvin = new Elvin ("elvin://elvinhostname");
 
 // subscribe to some notifications
 elvin.subscribe ("Foo == 42 || begins-with (Bar, 'baz')");
 
 // add a handler for notifications
 elvin.addSubscriptionListener (new GeneralSubscriptionListener ()
 {
   public void notificationReceived (GeneralNotificationEvent e)
   {
     System.out.println ("Got a notification:\n" + e.notification);
   }
 });
 
 // send a notification that we will get back from the router
 Notification notification = new Notification ();
 notification.set ("Foo", 42);
 notification.set ("Bar", "bar");
 notification.set ("Data", new byte []{0x00, 0xff});
 
 elvin.send (notification);
 
 elvin.close ();
 

Threading And Synchronisation Notes

Author:
Matthew Phillips

Constructor Summary
Elvin(ElvinURI routerUri)
          Create a new connection to an Elvin router.
Elvin(ElvinURI routerUri, ConnectionOptions options)
          Create a new connection to an Elvin router.
Elvin(ElvinURI routerUri, ConnectionOptions options, Keys notificationKeys, Keys subscriptionKeys)
          Create a new connection to an Elvin router.
Elvin(ElvinURI routerUri, ElvinOptions options)
          Create a new connection to an Elvin router.
Elvin(ElvinURI routerUri, Keys notificationKeys, Keys subscriptionKeys)
          Create a new connection to an Elvin router.
Elvin(String elvinUri)
          Create a new connection to an Elvin router.
Elvin(String elvinUri, ConnectionOptions options)
          Create a new connection to an Elvin router.
Elvin(String routerUri, ElvinOptions options)
          Create a new connection to an Elvin router.
 
Method Summary
 void addCloseListener(CloseListener listener)
          Add listener to the close event sent when the client's connection to the router is disconnected.
 void addLogListener(ElvinLogListener listener)
          Add a listener for log events emitted by the client.
 void addNotificationListener(GeneralNotificationListener listener)
          Add a listener to all notifications received by all subscriptions of this connection.
 void close()
          Close the connection to the router.
 void closeOnExit()
          Signal that this connection should be automatically closed when the VM exits.
 ConnectionOptions connectionOptions()
          The connection options established with the router.
 boolean hasSubscription(Subscription subscription)
          Test if a given subscription is part of this connection.
 boolean isOpen()
          Test if this connection is open i.e.
 long livenessTimeout()
           
 Object mutex()
          Return the mutex used to synchronize access to this connection.
 Keys notificationKeys()
           
 ElvinOptions options()
          Return the current options for the connection.
 long receiveTimeout()
           
 void removeCloseListener(CloseListener listener)
          Remove a previously added close listener.
 void removeLogListener(ElvinLogListener listener)
          Remove a previously added log listener.
 void removeNotificationListener(GeneralNotificationListener listener)
          Remove a listener to all notifications received by all subscriptions of this connection.
 ElvinURI routerUri()
          The router's URI.
 void send(Notification notification)
          Send a notification.
 void send(Notification notification, Keys keys)
          Send a notification with a set of keys but with no requirement for secure delivery: use send (notification, REQUIRE_SECURE_DELIVERY, keys) if you want only subscriptions with matching keys to receive a notification.
 void send(Notification notification, Keys keys, SecureMode secureMode)
          Send a notification.
 void send(Notification notification, SecureMode secureMode)
          Send a notification with a specified security mode.
 void setKeys(Keys newNotificationKeys, Keys newSubscriptionKeys)
          Change the connection-wide keys used to secure the receipt and delivery of notifications.
 void setLivenessTimeout(long livenessTimeout)
          Set the liveness timeout period (default is 60 seconds).
 void setNotificationKeys(Keys newNotificationKeys)
          Set the connection-wide notification keys used to secure delivery of notifications.
 void setReceiveTimeout(long receiveTimeout)
          Set the amount of time that must pass before the router is assumed not to be responding to a request message (default is 10 seconds).
 void setSubscriptionKeys(Keys newSubscriptionKeys)
          Set the connection-wide subscription keys used to secure receipt of notifications.
 Subscription subscribe(String subscriptionExpr)
          Create a new subscription.
 Subscription subscribe(String subscriptionExpr, Keys keys)
          Create a new subscription with a given set of security keys to enable secure delivery, but also allowing insecure notifications.
 Subscription subscribe(String subscriptionExpr, Keys keys, SecureMode secureMode)
          Create a new subscription.
 Subscription subscribe(String subscriptionExpr, SecureMode secureMode)
          Create a new subscription with a given security mode but with an empty key set.
 Keys subscriptionKeys()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Elvin

public Elvin(String elvinUri)
      throws InvalidURIException,
             IllegalArgumentException,
             ConnectException,
             IOException
Create a new connection to an Elvin router.

Parameters:
elvinUri - A URI for the Elvin router.
Throws:
InvalidURIException - if elvinUri is invalid.
IllegalArgumentException - if one of the arguments is not valid.
ConnectException - if the socket to the router could not be opened, e.g. connection refused.
IOException - if a general network error occurs.
See Also:
Elvin(ElvinURI, ConnectionOptions, Keys, Keys)

Elvin

public Elvin(String elvinUri,
             ConnectionOptions options)
      throws InvalidURIException,
             IllegalArgumentException,
             ConnectException,
             IOException
Create a new connection to an Elvin router.

Parameters:
elvinUri - A URI for the Elvin router.
options - The connection options.
Throws:
InvalidURIException - if elvinUri is invalid.
IllegalArgumentException - if one of the arguments is not valid.
ConnectException - if the socket to the router could not be opened, e.g. connection refused.
IOException - if a general network error occurs.
See Also:
Elvin(ElvinURI, ConnectionOptions, Keys, Keys)

Elvin

public Elvin(ElvinURI routerUri)
      throws IllegalArgumentException,
             ConnectException,
             IOException
Create a new connection to an Elvin router.

Parameters:
routerUri - A URI for the Elvin router.
Throws:
IllegalArgumentException - if one of the arguments is not valid.
ConnectException - if the socket to the router could not be opened, e.g. connection refused.
IOException - if a general network error occurs.
See Also:
Elvin(ElvinURI, ConnectionOptions, Keys, Keys)

Elvin

public Elvin(ElvinURI routerUri,
             ConnectionOptions options)
      throws IllegalArgumentException,
             ConnectException,
             IOException,
             ConnectionOptionsException
Create a new connection to an Elvin router.

Parameters:
routerUri - A URI for the Elvin router.
options - The connection options.
Throws:
IllegalArgumentException - if one of the arguments is not valid.
ConnectException - if the socket to the router could not be opened, e.g. connection refused.
IOException - if a general network error occurs.
ConnectionOptionsException - if the router rejects a connection option.
See Also:
Elvin(ElvinURI, ConnectionOptions, Keys, Keys)

Elvin

public Elvin(ElvinURI routerUri,
             Keys notificationKeys,
             Keys subscriptionKeys)
      throws IllegalArgumentException,
             ConnectException,
             IOException,
             ConnectionOptionsException
Create a new connection to an Elvin router.

Parameters:
routerUri - The URI of the router to connect to.
notificationKeys - These keys automatically apply to all notifications, exactly as if they were added to the keys in the send call.
subscriptionKeys - These keys automatically apply to all subscriptions, exactly as if they were added to the keys in the subscription call.
Throws:
IllegalArgumentException - if one of the arguments is not valid.
ConnectException - if a socket to the router could not be opened, e.g. connection refused.
ConnectionOptionsException - if the router rejected the connection options. The client may elect to change the options and try to create a new connection.
IOException - if some other IO error occurs.
See Also:
Elvin(ElvinURI, ConnectionOptions, Keys, Keys)

Elvin

public Elvin(ElvinURI routerUri,
             ConnectionOptions options,
             Keys notificationKeys,
             Keys subscriptionKeys)
      throws IllegalArgumentException,
             ConnectException,
             IOException,
             ConnectionOptionsException
Create a new connection to an Elvin router.

Parameters:
routerUri - The URI of the router to connect to.
options - The connection options.
notificationKeys - These keys automatically apply to all notifications, exactly as if they were added to the keys in the send call.
subscriptionKeys - These keys automatically apply to all subscriptions, exactly as if they were added to the keys in the subscription call.
Throws:
IllegalArgumentException - if one of the arguments is not valid.
ConnectException - if a socket to the router could not be opened, e.g. connection refused.
ConnectionOptionsException - if the router rejected the connection options. The client may elect to change the options and try to create a new connection.
IOException - if some other IO error occurs.
See Also:
subscribe(String, Keys, SecureMode), send(Notification, Keys, SecureMode), setKeys(Keys, Keys)

Elvin

public Elvin(String routerUri,
             ElvinOptions options)
      throws IllegalArgumentException,
             ConnectException,
             IOException,
             ConnectionOptionsException
Create a new connection to an Elvin router.

Parameters:
routerUri - The URI of the router to connect to.
options - The Elvin client options. Modifying these after they have been passed into this constructor has no effect.
Throws:
IllegalArgumentException - if one of the arguments is not valid.
ConnectException - if a socket to the router could not be opened, e.g. connection refused.
ConnectionOptionsException - if the router rejected the connection options. The client may elect to change the options and try to create a new connection.
IOException - if some other IO error occurs.
See Also:
subscribe(String, Keys, SecureMode), send(Notification, Keys, SecureMode), setKeys(Keys, Keys)

Elvin

public Elvin(ElvinURI routerUri,
             ElvinOptions options)
      throws IllegalArgumentException,
             ConnectException,
             IOException,
             ConnectionOptionsException
Create a new connection to an Elvin router.

Parameters:
routerUri - The URI of the router to connect to.
options - The Elvin client options. Modifying these after they have been passed into this constructor has no effect.
Throws:
IllegalArgumentException - if one of the arguments is not valid.
ConnectException - if a socket to the router could not be opened, e.g. connection refused.
ConnectionOptionsException - if the router rejected the connection options. The client may elect to change the options and try to create a new connection.
IOException - if some other IO error occurs.
See Also:
subscribe(String, Keys, SecureMode), send(Notification, Keys, SecureMode), setKeys(Keys, Keys)
Method Detail

close

public void close()
Close the connection to the router. May be executed more than once with no effect.

Specified by:
close in interface Closeable
See Also:
addCloseListener(CloseListener)

closeOnExit

public void closeOnExit()
Signal that this connection should be automatically closed when the VM exits. This should be used with care since it creates a shutdown thread on each call, and stops the connection from being GC'd if it is closed manually.

This method is most suitable for small applications that want to create a connection, do something with it, and then cleanly shut down without having to worry about whether the VM exits normally, on Ctrl+C, System.exit (), etc.


isOpen

public boolean isOpen()
Test if this connection is open i.e. has not been closed locally or disconnected by the remote router.

See Also:
close()

routerUri

public ElvinURI routerUri()
The router's URI.


options

public ElvinOptions options()
Return the current options for the connection. These options reflect any changes made after initialisation, e.g. by using setReceiveTimeout(long), setNotificationKeys(Keys), etc.


connectionOptions

public ConnectionOptions connectionOptions()
The connection options established with the router. These cannot be changed after connection.

See Also:
options(), Elvin(ElvinURI, ConnectionOptions, Keys, Keys)

receiveTimeout

public long receiveTimeout()
See Also:
setReceiveTimeout(long), options()

setReceiveTimeout

public void setReceiveTimeout(long receiveTimeout)
Set the amount of time that must pass before the router is assumed not to be responding to a request message (default is 10 seconds). This method can be used on a live connection, unlike ElvinOptions.receiveTimeout.

Parameters:
receiveTimeout - The new receive timeout in milliseconds.
See Also:
setLivenessTimeout(long)

livenessTimeout

public long livenessTimeout()
See Also:
setLivenessTimeout(long), options()

setLivenessTimeout

public void setLivenessTimeout(long livenessTimeout)
Set the liveness timeout period (default is 60 seconds). If no messages are seen from the router in this period a connection test message is sent and if no reply is seen within the receive timeout period the connection is deemed to be closed. This method can be used on a live connection, unlike ElvinOptions.livenessTimeout.

Parameters:
livenessTimeout - The new liveness timeout in milliseconds. Cannot be less than 1000.
See Also:
setReceiveTimeout(long)

addCloseListener

public void addCloseListener(CloseListener listener)
Add listener to the close event sent when the client's connection to the router is disconnected. This includes normal disconnection via close(), disconnections caused by the remote router shutting down, and abnormal disconnections caused by communications errors.


removeCloseListener

public void removeCloseListener(CloseListener listener)
Remove a previously added close listener.


addLogListener

public void addLogListener(ElvinLogListener listener)
Add a listener for log events emitted by the client. This includes non-fatal errors, warnings and diagnostics of potential problems. If no listeners are registered, log events will be echoed to the standard error output.


removeLogListener

public void removeLogListener(ElvinLogListener listener)
Remove a previously added log listener.


mutex

public Object mutex()
Return the mutex used to synchronize access to this connection. All methods on the connection that modify state or otherwise need to be thread safe acquire this before operation. Clients may choose to pre-acquire this mutex to execute several operations atomically -- see example in subscribe(String, Keys, SecureMode).

NB: while this mutex is held, all callbacks (e.g. notification delivery) will be blocked.


subscribe

public Subscription subscribe(String subscriptionExpr)
                       throws IOException,
                              InvalidSubscriptionException
Create a new subscription. See subscribe(String, Keys, SecureMode) for details.

Parameters:
subscriptionExpr - The subscription expression.
Returns:
The subscription instance.
Throws:
IOException - if an IO error occurs.
InvalidSubscriptionException - if the subscription expression is invalid.

subscribe

public Subscription subscribe(String subscriptionExpr,
                              Keys keys)
                       throws IOException,
                              InvalidSubscriptionException
Create a new subscription with a given set of security keys to enable secure delivery, but also allowing insecure notifications. See subscribe(String, Keys, SecureMode) for details.

Parameters:
subscriptionExpr - The subscription expression.
keys - The keys to add to the subscription.
Returns:
The subscription instance.
Throws:
IOException - if an IO error occurs.
InvalidSubscriptionException - if the subscription expression is invalid.

subscribe

public Subscription subscribe(String subscriptionExpr,
                              SecureMode secureMode)
                       throws IOException,
                              InvalidSubscriptionException
Create a new subscription with a given security mode but with an empty key set. Be careful when using REQUIRE_SECURE_DELIVERY with this subscription option: if you don't specify keys for the subscription elsewhere, either via setKeys(Keys, Keys) or Subscription.setKeys(Keys), the subscription will never be able to receive notifications.

See subscribe(String, Keys, SecureMode) for more details.

Parameters:
subscriptionExpr - The subscription expression.
secureMode - The security mode: specifying REQUIRE_SECURE_DELIVERY means the subscription will only receive notifications that are sent by clients with keys matching the set supplied here or the global subscription key set.
Returns:
The subscription instance.
Throws:
IOException - if an IO error occurs.
InvalidSubscriptionException - if the subscription expression is invalid.

subscribe

public Subscription subscribe(String subscriptionExpr,
                              Keys keys,
                              SecureMode secureMode)
                       throws IOException,
                              InvalidSubscriptionException
Create a new subscription. The returned subscription instance can be used to listen for notifications, modify subscription settings and unsubscribe.

See the Elvin Subscription Language page for information on how subscription expressions work.

There exists the possibility that, between creating a subscription and adding a listener to it, the client could miss a notification. If needed, the client may ensure this will not happen by acquiring the connection's mutex before subscribing. e.g.

   Elvin elvin = ...;
   
   synchronized (elvin.mutex ())
   {
     Subscription sub = elvin.subscribe (...);
     
     sub.addNotificationListener (...);
   }
 

Parameters:
subscriptionExpr - The subscription expression to match notifications.
keys - The keys that must match notificiation keys for secure delivery.
secureMode - The security mode: specifying REQUIRE_SECURE_DELIVERY means the subscription will only receive notifications that are sent by clients with keys matching the set supplied here or the global subscription key set.
Returns:
The subscription instance.
Throws:
IOException - if a network error occurs.
InvalidSubscriptionException - if the subscription expression is invalid.
See Also:
send(Notification, Keys, SecureMode), addNotificationListener(GeneralNotificationListener), Subscription, Subscription.escapeField(String), Subscription.escapeString(String), SecureMode, Keys

hasSubscription

public boolean hasSubscription(Subscription subscription)
Test if a given subscription is part of this connection.

See Also:
Subscription.isActive()

addNotificationListener

public void addNotificationListener(GeneralNotificationListener listener)
Add a listener to all notifications received by all subscriptions of this connection.

See Also:
removeNotificationListener(GeneralNotificationListener), Subscription.addListener(NotificationListener)

removeNotificationListener

public void removeNotificationListener(GeneralNotificationListener listener)
Remove a listener to all notifications received by all subscriptions of this connection.

See Also:
addNotificationListener(GeneralNotificationListener)

send

public void send(Notification notification)
          throws IOException
Send a notification. See send(Notification, Keys, SecureMode) for details.

Throws:
IOException

send

public void send(Notification notification,
                 Keys keys)
          throws IOException
Send a notification with a set of keys but with no requirement for secure delivery: use send (notification, REQUIRE_SECURE_DELIVERY, keys) if you want only subscriptions with matching keys to receive a notification.

See send(Notification, Keys, SecureMode) for more details.

Parameters:
notification - The notification to send.
keys - The keys to attach to the notification.
Throws:
IOException - if an IO error occurs during sending.

send

public void send(Notification notification,
                 SecureMode secureMode)
          throws IOException
Send a notification with a specified security mode. Be careful when using REQUIRE_SECURE_DELIVERY with this method: if you haven't specified any global notification keys via setKeys(Keys, Keys) or setNotificationKeys(Keys), the notification will never be able to to be delivered.

See send(Notification, Keys, SecureMode) for more details.

Parameters:
notification - The notification to send.
secureMode - The security requirement.
Throws:
IOException - if an IO error occurs during sending.

send

public void send(Notification notification,
                 Keys keys,
                 SecureMode secureMode)
          throws IOException
Send a notification.

Parameters:
notification - The notification to send.
keys - The keys that must match for secure delivery.
secureMode - The security requirement. REQUIRE_SECURE_DELIVERY means the notification can only be received by subscriptions with keys matching the set supplied here (or the connections' global notification keys).
Throws:
IOException - if an IO error occurs.
See Also:
subscribe(String, Keys, SecureMode), Notification, SecureMode, Keys

notificationKeys

public Keys notificationKeys()
See Also:
setNotificationKeys(Keys)

setNotificationKeys

public void setNotificationKeys(Keys newNotificationKeys)
                         throws IOException
Set the connection-wide notification keys used to secure delivery of notifications.

Parameters:
newNotificationKeys - The new notification keys. These automatically apply to all notifications, exactly as if they were added to the keys in the send call.
Throws:
IOException - if an IO error occurs.
See Also:
setSubscriptionKeys(Keys), setKeys(Keys, Keys)

subscriptionKeys

public Keys subscriptionKeys()
See Also:
setSubscriptionKeys(Keys)

setSubscriptionKeys

public void setSubscriptionKeys(Keys newSubscriptionKeys)
                         throws IOException
Set the connection-wide subscription keys used to secure receipt of notifications.

Parameters:
newSubscriptionKeys - The new subscription keys. These automatically apply to all subscriptions, exactly as if they were added to the keys in the subscription, call. This includes currently existing subscriptions.
Throws:
IOException - if an IO error occurs.
See Also:
setNotificationKeys(Keys), setKeys(Keys, Keys)

setKeys

public void setKeys(Keys newNotificationKeys,
                    Keys newSubscriptionKeys)
             throws IOException
Change the connection-wide keys used to secure the receipt and delivery of notifications.

Parameters:
newNotificationKeys - The new notification keys. These automatically apply to all notifications, exactly as if they were added to the keys in the send call.
newSubscriptionKeys - The new subscription keys. These automatically apply to all subscriptions, exactly as if they were added to the keys in the subscription, call. This applies to all existing and future subscriptions.
Throws:
IOException - if an IO error occurs.


Copyright © 2008 Matthew Phillips. All Rights Reserved.