javarmi

how to create rmi in java


hi i have searched the internet for a long time trying to find some thing that talk about how to start rmi registry in windows 7 using cmd so if any one know how to do that pleas let me to know how to do it or if any one can provide us a good link for that... thx in advance

ok thanx for all how answered my question when i asked the question i was not fully understand the RMI system or how it work but know i have good idea i will summarized this for provide all with an idea for the RMI system and if i have any mistake please correct me

so

Remote Interface:

  1. We need an interface that extends from the Remote class and defined the method that we would like to invoke remotely
    note: Remote is a "marker" interface that identifies interfaces whose methods may be invoked from a non-local virtual machine.

    import java.rmi.Remote; 
    import java.rmi.RemoteException; 
    import java.util.Calendar; 
    
    public interface CalendarTask extends Remote { 
    
      Calendar getDate() throws RemoteException; 
    
    } 
    

The Remote Object:

  1. We need class that create a Remote object's so we crate class object implement the Remote Interface to make the object's that created by this class object remote object's and we link this object's to the RMI System by extends from this class UnicastRemoteObjec so When a class extends from UnicastRemoteObject, it must provide a constructor declaring this constructor calls super(), it activates code in UnicastRemoteObject, which performs the RMI linking and remote object initialization.

    import java.rmi.RemoteException; 
    import java.rmi.server.UnicastRemoteObject; 
    import java.util.Calendar; 
    
     public class CalendarImpl extends UnicastRemoteObject implements CalendarTask { 
    
      private int counter = 1; 
    
       public CalendarImpl() throws RemoteException {} 
    
       public Calendar getDate() throws RemoteException{
    
      System.out.print("Method called on server:"); 
      System.out.println("counter = " + counter++); 
      return Calendar.getInstance(); 
      } 
    } 
    

Writing the Server:

3.1 The server's job is to accept requests from a client, perform some service, and then send the results back to the client.

3.2 The server must specify an interface that defines the methods available to clients as a service. we do that above in the first step (Remote Interface)

3.3 The server creates the remote object, registers it under some arbitrary name, then waits for remote requests

3.4 so for register The remote object we use java.rmi.registry.LocateRegistry class allows the RMI registry service (provided as part of the JVM) to be started within the code by calling its createRegistry() method.

3.5 The java.rmi.registry.Registry class provides two methods for binding objects to the registry.

• Naming.bind("ArbitraryName", remoteObj); throws an Exception if an object is already bound under the "ArbitrayName".

• Naming.rebind ("ArbitraryName", remoteObj); binds the object under the "ArbitraryName" if it does not exist or overwrites the object that is bound.

3.6 The example on the following acts as a server that creates a CalendarImpl object and makes it available to clients by binding it under a name of "TheCalendar"

  import java.rmi.Naming; 
  import java.rmi.registry.LocateRegistry; 

  public class CalendarServer { 

  public static void main(String args[]) { 
      System.out.println("Starting server..."); 
      // Start RMI registry service and bind 
      // object to the registry 
      try { 
          LocateRegistry.createRegistry(1099); 
          Naming.rebind("TheCalendar", 
                  new CalendarImpl()); 
      } catch (Exception e) { 
          e.printStackTrace(); 
          System.exit(1); 
      } 
      System.out.println("Server ready"); 
   } 
 }

Writing the Client:

4.1 An RMI client is a program that accesses the services provided by a remote object

4.2 The java.rmi.registry.LocateRegistry class allows the RMI registry service to be located by a client by its getRegistry() method

4.3 The java.rmi.registry.Registry class provides a lookup() method that takes the "ArbitraryName" the remote object was bound to by the server.

Once the client obtains a reference to a remote object, it invokes methods as if the object were local

import java.rmi.registry.*; 
import java.util.Calendar; 

public class CalendarClient { 

  public static void main(String args[]) { 
      Calendar c = null; 
      CalendarTask remoteObj; 
      String host = "localhost"; 
      if(args.length == 1) 
          host = args[0]; 
      try { 
          Registry r = 
            LocateRegistry.getRegistry(host, 1099);
            Object o = r.lookup("TheCalendar"); 
            remoteObj = (CalendarTask) o; 
            c = remoteObj.getDate(); 
      } catch (Exception e) { 
          e.printStackTrace(); 
      } 
      System.out.printf("%tc", c); 
    } 
  } 

Solution

  • The code you have written doesn't start a registry. LocateRegistry.getRegistry() doesn't do that. Check the Javadoc. It assumes the Registry is already running. LocateRegistry.getRegistry() just constructs a Registry stub according to the host and port you provide. It doesn't even do any network operations.

    To start a Registry from within your JVM, use LocateRegistry.createRegistry(), as its Javadoc states.

    EDIT: There's a lot of misinformation in your edit.

    Remote is a "marker" interface that identifies interfaces whose methods may be invoked from a non-local virtual machine.

    Only if implemented by an exported remote object whose stub has been transmitted to that VM. The remote interface itself doesn't have any such magical property. All methods defined in a remote interface must be declared to throw RemoteException, although the implementations of these methods generally don't need to be so declared (i.e. unless they perform remote operations themselves: the compiler will tell you).

    We need class that create a Remote object's so we crate class object implement the Remote Interface to make the object's that created by this class object

    Far too much confusion here. We need a class. The class must implement the remote interface. This is not an 'object' yet: it is a piece of code that must be compiled to a .class file. A class doesn't 'make objects'. An application does that, with the new operator.

    we link this object's to the RMI System by extends from this class UnicastRemoteObjec so When a class extends from UnicastRemoteObject, it must provide a constructor declaring this constructor calls super(), it activates code in UnicastRemoteObject, which performs the RMI linking and remote object initialization

    There is no 'link' step in RMI. There is an 'export' step. It is performed either by extending UnicastRemoteObject or by calling UnicastRemoteObject.exportObject(). If you don't extend UnicastRemoteObject you don't need the constructor you described.

    The server's job is to accept requests from a client, perform some service, and then send the results back to the client.

    The server's job is to implement the methods in the remote interface. RMI does all the rest for you.

    The server creates the remote object, registers it under some arbitrary name, then waits for remote requests

    The server is the remote object. It can register itself.

    for register The remote object we use java.rmi.registry.LocateRegistry class allows the RMI registry service (provided as part of the JVM) to be started within the code by calling its createRegistry() method.

    Or you can use an external Registry via the rmiregistry command. Or you can use an LDAP server via JNDI.

    LocateRegistry.createRegistry(1099); 
    Naming.rebind("TheCalendar", 
                  new CalendarImpl()); 
    

    This won't work unless you store the result of createRegistry() into a static variable. And having stored it, you may as well use it to do the bind, instead of using the Naming class. If you don't store it into a static variable it will be garbage-collected and so will the remote object.

    The java.rmi.registry.LocateRegistry class allows the RMI registry service to be located by a client by its getRegistry() method

    Or you can use the Naming class, see below.

    The java.rmi.registry.Registry class provides a lookup() method that takes the "ArbitraryName" the remote object was bound to by the server.

    So does the Naming class. It takes an rmi: URL which specifies the host and port and bind-name. You can omit the rmi:// part. If you omit the host it defaults to 'localhost', but this is only useful if the client is running in the same host as the server, which isn't itself very useful. If you omit the port it defaults to 1099.