HEALTH ADMINISTRATION
HEALTH ADMINISTRATION USING RMI
1.RMI Interface – HealthService.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface HealthService extends Remote {
String registerPatient(String name, int age, String condition) throws RemoteException;
String getPatientInfo(String name) throws RemoteException;
}
2.Implementation of the service – HealthServiceImpl.java
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.util.HashMap;
public class HealthServiceImpl extends UnicastRemoteObject implements HealthService {
private HashMap<String, String> patientRecords;
protected HealthServiceImpl() throws RemoteException {
super();
patientRecords = new HashMap<>();
}
public String registerPatient(String name, int age, String condition) throws RemoteException {
String record = "Name: " + name + ", Age: " + age + ", Condition: " + condition;
patientRecords.put(name, record);
return "Patient " + name + " registered successfully.";
}
public String getPatientInfo(String name) throws RemoteException {
return patientRecords.getOrDefault(name, "Patient not found.");
}
}
3.RMI server to bind the service – Server.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Server {
public static void main(String[] args) {
try {
HealthService service = new HealthServiceImpl();
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("HealthService", service);
System.out.println("Health Service is running...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.Client to access the remote service – Client.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("localhost", 1099);
HealthService service = (HealthService) registry.lookup("HealthService");
System.out.println(service.registerPatient("John Doe", 30, "Flu"));
System.out.println(service.getPatientInfo("John Doe"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
5.Expected Output
Server
Client


Comments
Post a Comment