A) Write Fully Working Remote Procedure Call (RPC) Program Using Java. Example
A) Write Fully Working Remote Procedure Call (RPC) Program Using Java. Example
Example
First, we need to define the interfaces for the RPC calls (in two separate files):
UserService.java:
```
import java.rmi.Remote;
import java.rmi.RemoteException;
UserServiceImpl.java:
```
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
@Override
public String getName(String userId) throws RemoteException {
if (userId.equals("1")) {
return "John Doe";
} else if (userId.equals("2")) {
return "Jane Smith";
} else {
return "Unknown user";
}
}
}
```
UserServer.java:
```
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
System.out.println("UserServer ready.");
} catch (Exception e) {
System.err.println("UserServer exception: " + e.toString());
e.printStackTrace();
}
}
}
```
name = userService.getName("2");
System.out.println("Name for user 2: " + name);
name = userService.getName("3");
System.out.println("Name for user 3: " + name);
} catch (Exception e) {
System.err.println("UserClient exception: " + e.toString());
e.printStackTrace();
}
}
}
```
To run this program, we need to compile all the files, start the server (`java
UserServer`) and then start the client (`java UserClient`). The client should output:
```
Name for user 1: John Doe
Name for user 2: Jane Smith
Name for user 3: Unknown user
```
B) Write fully working remote method invocation (RMI) program using java.
Example
Here is an example of a fully working RMI program in Java:
```java
import java.rmi.*;
import java.rmi.server.*;
}
```
```java
import java.rmi.*;
}
```
Interface implementation:
```java
import java.rmi.*;
```java
import java.rmi.*;
}
```
```
java RMIServer
```
```
Result: 30
```