To use a command, the client creates an instance of the command
and calls the execute method for the command. Depending on the command, calling
other methods can be necessary. The specifics will vary with the application.
About this task
In the example application, the server is the CheckingAccountBean,
an entity enterprise bean. In order to use this enterprise bean, the client
gets a reference to the bean’s home interface. The client then uses the reference
to the home interface and one of the finder methods for the bean to obtain
a reference to the remote interface for the bean. If there is no appropriate
bean, the client can create one using a create method on the home interface.
The
following code example illustrates the use of the
ModifyCheckingAccountCmd command.
This work takes place after an appropriate CheckingAccount bean has been found
or created. The code instantiates a command, setting the input values by using
one of the constructors defined for the command. The null argument indicates
that the command should look up the server using the default target policy,
and 1000 is the amount the command attempts to add to the balance of the checking
account. After the command is instantiated, the code calls the setCheckingAccount
method to identify the account to be modified. Finally, the execute method
on the command is called.
{
...
CheckingAccount checkingAccount
...
try {
ModifyCheckingAccountCmd cmd =
new ModifyCheckingAccountCmdImpl(null, 1000);
cmd.setCheckingAccount(checkingAccount);
cmd.execute();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
...
}