Writing command interfaces
The command package can be used by distributed applications to reduce the number of remote invocations that a client makes. The base interface for all commands is the Command interface.
Overview
Distributed applications are defined by the ability to utilize remote resources as if they were local, but this remote work affects the performance of distributed applications. Distributed applications can improve performance by using remote calls sparingly. For example, if a server does several tasks for a client, the application can run more quickly if the client bundles requests together, reducing the number of individual remote calls. The command package provides a mechanism for collecting sets of requests to be submitted as a unit.
In addition, the command package provides a generic way of making requests. A client instantiates the command, sets its input data, and tells it to run. The command infrastructure determines the target server and passes a copy of the command to it. The server runs the command, sets any output data, and copies it back to the client. The package provides a common way to issue a command, locally or remotely, and independently of the server’s implementation. Any server (an enterprise bean, a Java Database Connectivity (JDBC) server, a servlet, and so on) can be a target of a command if the server supports Java access to its resources and provides a way to copy the command between the client’s Java Virtual Machine (JVM) and its own JVM. The command facility is implemented in the com.ibm.websphere.command Java package. The classes and interfaces in the command package fall into four general categories:
- Interfaces for creating commands
- Classes and interfaces for implementing commands
- Classes and interfaces for determining where the command is run
- Classes defining package-specific exceptions
Procedure
- Write a command interface. Extend one or more of the three interfaces that are included in the command package. The command interface provides only the client-side interface for generic commands and declares three basic methods:
- isReadyToCallExecute - This method is called on the client side before the command is passed to the server for execution.
- execute - This method passes the command to the target and returns any data.
- reset - This method reverts any output properties to the values they had before the execute method was called so that the object can be reused.
- Implement the command interface. The implementation class for your interface must contain implementations for the isReadyToCallExecute and reset methods. The execute method is implemented for you elsewhere. Most commands do not extend the Command interface directly but use one of the provided extensions: the TargetableCommand interface and the CompensableCommand interface.
- Use the command.
TargetableCommand interface
CompensableCommand interface
Implementing command interfaces
Use a command
Interfaces for creating commands
Facilities for implementing commands
Exceptions in the command package
Targets and target policies
Writing a command target (server)
Related tasks
Setting and determining targets
Use application clients