Commands
Definition
Commands are object instances on the server that can be invoked by connected clients. A command object must be declared in the application.properties file where it is also assigned an external name. To invoke a command the client must first obtain a command proxy and cast it to one of the interfaces implemented by the command class. Because the command objects are created at server startup, are never garbage collected and don't encapsulate state we also call them static commands.

Command Interfaces
The sample application contains one command exported as 'synchronizer'. The command implements the following interfaces:
Name Description
IPublish Contains the method for updating the message on the server.
IRequest Contains the method for retrieving the server message.
ISubscribe Contains the methods for subscribing and unsubscribing the invoking client from message update notifications.

These are the definitions for the command interfaces:


public interface IPublish
{
    public void publish( String message ) throws IOException;
}

public interface IRequest
{
    public String request() throws IOException;
}

public interface ISubscribe
{
    public void subscribe() throws IOException;

    public void unsubscribe() throws IOException;
}
        
We define the functionality implemented by the command using more interfaces because we want to grant application roles the privilege to execute certain interfaces. It is not possible to assign execution privileges for methods.

Command Implementation
The synchronizer class implements all these interfaces:
public class Synchronizer implements IPublish, IRequest, ISubscribe
{
    public void publish( String message )
    {
        MessageManager.instance().setMessage( message );
    }

    public String request()
    {
        return MessageManager.instance().getMessage();
    }

    public void subscribe()
    {
        SubscriptionManager.instance().addSubscription(
                        ClientContext.getConnectionContext(),
                        (IUploadCallback)ClientContext.getClientProxy());
    }
}
        
The SubscriptionManager singleton is a storage for client subscriptions. The MessageManager is a singleton that stores and allows the clients to update and retrieve the server message. When the message is updated by a client all subscribed clients are automatically sent the changed message.

Command Declaration
The command is declared in the application.properties file with the following lines:
Application.command=synchronizer
Application.command.synchronizer=com.accendia.datasynch.server.Synchronizer
Application.command.synchronizer.enabled=true
        
The enabled flag is optional and defaults to true.

Client Invocation
The client connects to the server and obtains a reference to the command proxy:

...
// connect
serverContext_ = new ServerContext( host, port, user, password, false );
...
// get the command proxy and cast to one of the implemented interfaces
publishCommand_ = (IPublish)serverContext_.getCommandProxy(
                                applicationName, "synchronizer" );
...
// invoke the remote command
publishCommand_.publish( message );