SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    duplicating user information

    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

    Code:
    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) {}
    
                }
        }
    >Server.java - the server interface with abstract methods to be implemented
    Code:
    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;
      
    }
    >MyServerImpl.java

    Code:
    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);
        
        }
      }
    }
    > SecondServer.java
    Code:
    import java.rmi.*
    public interface SecondServer extends Remote
    {
    	//update user profile on Second server
    	public String upDate(String user) throws RemoteException;
    }
    > SecondServerImpl.java

    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);
          }
    
        }
    Of course, this can be implmented using a database, I just want to try it out by just coping data stored in memory...

    Any responses appreciated!

    Ps. user data is hardcoded just for testing

  2. #2
    SitePoint Addict ruby-lang's Avatar
    Join Date
    Aug 2007
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about using memcached? There's even a Java port of the daemon, if you want to keep things pure Java. Another option is creating a Glassfish cluster and switching to EJB instead of doing low-level RMI.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your suggestion but I cant quite get my head around memcached.

    Any more contributions appreciated!

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Shared file system?
    But it would be as good as using Databases.

    Either write our own custom code or use some thirdparty products (like memcache) which does the same thing.

  5. #5
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your request is kind of strange...

    Would you be able to provide some more information?


    Is this a business requirement or a school assignment?
    Why RMI?
    Are you storing your user information in a database?
    Do the two servers know about one another (like via a network connection)?
    What level of fault-tolerance are you trying to achieve?

    Using RMI to do fault-tolerance is a bit ridiculous. There are other solutions that will be much better, but more information is needed prior to helping you out.
    baby steps... baby steps...

  6. #6
    SitePoint Addict ruby-lang's Avatar
    Join Date
    Aug 2007
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to do it the hard way, I think you could probably marshall the entire data structure when a backup server comes online and then just send commands (as in the command design pattern) between the instances to keep them synchronized.

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hooknc View Post
    Your request is kind of strange...

    Would you be able to provide some more information?


    Is this a business requirement or a school assignment?
    Why RMI?
    Are you storing your user information in a database?
    Do the two servers know about one another (like via a network connection)?
    What level of fault-tolerance are you trying to achieve?

    Using RMI to do fault-tolerance is a bit ridiculous. There are other solutions that will be much better, but more information is needed prior to helping you out.
    It's neither a school asst nor business requirement. I'm just trying to improve my programing skills. I'm using Java RMI because I find it to be relatively "neat" and involves less code since the creation of sockets etc is done automatically for you.

    Back to my question.

    I guess I should have asked: "how can I replicate data across multiple servers?" So say data is sent via a Client servlet to the 'primary' server. How can I while storing the data in a variable on the server, also send it to another server.

    As shown in my code snippet, i'm tryna call a method of the 'backup' server and pass it data held in the 'primary' server but this does seem to be working. I believe there's a better and perhaps easier way of achieving this. I should add that I was able to implement this using UDP Datagram sockets but stuck doing same in RMI. I'm not using a database at the moment as this is only the early phase. That'll come in later...

    Put another way. What I have is a client and server communicating via RMI.
    Any help appreciated.

    Ps. I believe you guys can help me out.

  8. #8
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    It seems to me that you should be a to replicate data across an arbitrary number of databases easily enough with the following 'model'

    client - data entered and submitted
    server - receives data, performs business logic, passes on to permanence layer (not the 'right' name, can't think of it at the moment)
    permanence layer - receives cleaned data and writes to n databases
    - on initialization, the permanence layer receives n 'database objects' which will contain the information it needs to connect to each database and insert/update records as needed. The database objects will be stored in an ArrayList, traverse the list to write the data to each database.

  9. #9
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,399
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    As everyone said, use Database. Worse case you can put it as a BLOB. Or, if you're OS savy, create shared folders that can be accessible w/ multiple server.

  10. #10
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,399
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You know you can even save the Object itself to the database using serialze/deserialize. I think this is what you're looking.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •