|
| |
Here's the BankAccountServer class, which is the remote server for all
BankAccount remote objects:
package rmiServer;
import java.rmi.RemoteException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import rmi.BankAccount;
/**
* The BankAccount RMI server.
*
* @author Bryan Higgs
* @version 1.0
*/
public class BankAccountServer
{
/**
* Main entry point for server
* @param args the command line arguments
* args[0] contains the port number for the registry
*/
public static void main(String[] args)
{
// Determine the port number
int port = 1099; // the default port number for the registry
if (args.length > 0)
{
try
{
port = Integer.parseInt(args[0]);
}
catch (NumberFormatException nfe)
{
System.err.println("Port number " + args[1] + " invalid");
return;
}
}
try
{
System.err.println("Starting BankAccount Server...");
System.err.println("Creating BankAccount objects...");
// Create an object that provides the BankAccount service
BankAccountImpl account1 = new BankAccountImpl(1, "Fred Bloggs", 10000);
// Create a second object that provides the BankAccount service
BankAccountImpl account2 = new BankAccountImpl(2, "Mary Clark", 30000);
System.err.println("Binding remote objects to registry...");
Context namingContext = new InitialContext();
namingContext.bind("rmi://localhost:" + port + "/Fred", account1);
namingContext.bind("rmi://localhost:" + port + "/Mary", account2);
System.err.println("BankAccount Server ready...");
}
catch (NamingException ex)
{
ex.printStackTrace();
}
catch (RemoteException re)
{
re.printStackTrace();
}
}
}
|
Note the following:
|