Server Implementation
Listing 9.3 CustomServerImpl.java
(Complete server code)
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
//server implementation
public class CustomServerImpl extends
UnicastRemoteObject
implements CustomInterface{
//constructor
public CustomServerImpl()
throws RemoteException{
System.out.println("Creating Server Object");
}
//remote method used to get a list of products
public Vector getList()
throws RemoteException{
String [] list = {"Computer","Bike",
"Television","Music system"};
Vector v = new Vector();
for(int i=0;i<list.length;i++){
v.add(list[i]);
}
return v;
}
//remote method used to submit information
public String submit(Customer cm)
throws RemoteException{
String custName = "Name: ";
custName += cm.getName()+"\n";
String custAge = "Age: ";
custAge += cm.getAge()+"\n";
String item = "Item bought: ";
item += cm.getItem()+"\n";
String custInfo = custName + custAge + item;
custInfo+="Thanks for buying.";
return custInfo;
}
//main method
public static void main(String args[]){
try{
//Remote object
CustomInterface obj = new CustomServerImpl();
//bind the specified name to a new remote object.
Naming.rebind("Server", obj);
System.out.println("Server ready");
}catch(Exception ex){
System.out.println("Error: "+ex.getMessage());
}
}
}
Explanation
The getList() method stores all the
items sold by the company in an array, then it creates a vector and
adds all the items in the array to that vector. The method returns the
vector created. The Vector class implements the Serializable
interface, so it can be passed from one JVM to another JVM.
The submit() method extracts information
stored in the Customer object. It returns
a string containing information about the customer and adds a small
message to it.
|