Hi all,
I wonder if someone could help me with replicating data in a java program. Basically, im trying to replicate user information stored in a data structure on a server program to another data structure on another server program for the prupose of fault-tolerance. This being done using java RMI.
Put another way, when a user enters data say "username" in the frontend, I want the data to not only get stored on the main server but to be copied to another server. I'm just wondering what the easiest way to do this will be.
My attempt:
Files:
>MyClient.java - servlet to display user info - in this case, a hardcoded username
>Server.java - the server interface with abstract methods to be implementedCode:import java.io.*; import javax.servlet.http.*; import javax.servlet.*; import java.rmi.*; public class MyClient extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { PrintWriter out = res.getWriter(); res.setContentType("text/html"); String nm = "rmi://localhost/MyServer"; Server server = null; try { server = (Server)Naming.lookup(nm); } catch (Exception e) { out.println("Caught an exception doing name lookup on "+nm+": "+e); } try { String user = "testUser"; String username = server.userInfo(user); out.println("Username is : "+user); } catch (Exception e) {} } }
>MyServerImpl.javaCode:import java.rmi.*; import java.rmi.*; import java.io.*; import java.util.*; import java.rmi.server.UnicastRemoteObject; public interface Server extends Remote { //abstract method ... public String userInfo(String user) throws RemoteException; }
> SecondServer.javaCode:import java.rmi.*; import java.io.*; import java.util.*; import java.rmi.server.UnicastRemoteObject; public class MyServerImpl extends UnicastRemoteObject implements Server { MyServerImpl() throws RemoteException {}; public String userInfo(String user) throws RemoteException{ System.out.println("Username is " +user); //here i'm trying to call a method on the second server in order to store the username on it at the same time //create an instance of the second server SecondServerImpl second = new SecondServerImpl(); //call method on second server for update second.upDate(user); return user; //to client servlet } public static void main(String [] args) { try { // name with which we can find it = user name String nm = "MainServer"; //create new instance MyServerImpl server = new MyServerImpl(); // register with nameserver Naming.rebind(name, server); System.out.println("Server started as " + nm); } catch(Exception e) { System.out.println("Caught exception while registering: " + e); } } }
> SecondServerImpl.javaCode:import java.rmi.* public interface SecondServer extends Remote { //update user profile on Second server public String upDate(String user) throws RemoteException; }
Of course, this can be implmented using a database, I just want to try it out by just coping data stored in memory...Code:import java.rmi.*; import java.rmi.server.UnicastRemoteObject; public class SecondServerImpl extends UnicastRemoteObject implements SecondServer { // method to accept user details public String upDate(String username) throws RemoteException { String update = username; System.out.println("Received update: " +userdetails); return update; } public static void main(String[] args) throws Exception { //server RMI name String name = "SLAVEServer"; try { //register with nameserver Naming.rebind(nm, server); System.out.println("Started Social Network Server 2, registered as: " +nm); } catch(Exception e) { System.out.println("Caught exception while registering: " + e); System.exit(-1); } }
Any responses appreciated!
Ps. user data is hardcoded just for testing







Bookmarks