Java.rmi.Naming Remote Access Object method

Source: Internet
Author: User
Tags stub

RMI: Remote method call (invocation). Enables an object on a Java virtual machine to invoke a method on an object in another Java virtual machine as if it were a local object. RMI Remote invocation steps:1, the client object invokes a method on the client helper object2, the client helper packages the invocation information (variable, method name), which is sent over the network to the server helper object3, the server helper object will unpack the information sent by the client helper to find out what method is actually being called and what object the method resides in.4, invokes the real method on the real service object, and returns the result to the server helper object5, the server helper object packages the results and sends them to the client helper6, the client helper object will return the value of the package and return it to the client object7, the Customer object gets the return value for the Customer object, step 2-6 is fully transparent the process of building an RMI service is divided into the following 7 steps;1to create a remote method interface, which must inherit from the Remoted interface remote interface is an identity interface that identifies the interface that the contained method can invoke from a non-local virtual machine, and the remote interface itself does not contain any methods [Java] View plaincopy  Packageserver; ImportJava.rmi.Remote; Importjava.rmi.RemoteException;  Public InterfaceHelloextendsRemote { PublicString SayHello (string name)throwsremoteexception; Because the nature of the remote method call is still network communication, but the underlying implementation is hidden, network communication is often abnormal, so all methods of the interface must be thrown remoteexception to show that the method is risky2, create a remote method interface implementation class: The constructor for the UnicastRemoteObject class throws RemoteException, so its inheriting class cannot use the default constructor. The constructor of the inheriting class must also be thrown remoteexception because the method parameter and the return value will eventually be transferred over the network, it must be serializable [Java] View plaincopy Packageserver; Importjava.rmi.RemoteException; ImportJava.rmi.server.UnicastRemoteObject;  Public classHelloimplextendsUnicastRemoteObjectImplementsHello {Private Static Final LongSerialversionuid = -271947229644133464l;  PublicHelloimpl ()throwsremoteexception{Super(); }         PublicString SayHello (string name)throwsRemoteException {return"Hello," +name; }  }  3, generate SUTB stub class with Java Rmic tool (jdk1.5.0_15/bin/rmic) jdk1.2 later RMI can be sent directly to the real class through the reflection API, so do not need to skeleton class Sutb stub for the remote method class in the local proxy, is generated on the basis of the server code, need Helloimpl.class file, because Helloimpl inherited Hel Lo interface, so the Hello.class file is also not a few test- -Server---Hello.class----Helloimpl.classmode one: [plain] view Plaincopy[[email protected] test]$ CD/home/name/test/[[email protected] test]$ rmic server. Helloimpl Mode II: [Plain] view Plaincopy[[email protected] test]$ rmic-classpath/home/name/Test Server. Helloimpl the Helloimpl_stub.class file will be generated after the successful run4, start the RMI registration service (jdk1.5.0_15/bin/rmiregistry) mode one: Background start rmiregistry Service [plain] view Plaincopy[[email protected] jdk]$ jdk1.5.0_15/bin/rmiregistry 12312 &  [1] 22720[[email protected] jdk]$ PS-ef|grep rmiregistry name22720 13763 0 16:43 pts/3 00:00:00 jdk1.5.0_15/bin/rmiregistry 12312name22737 13763 0 16:43 PTS/3 00:00:00grep rmiregistry If you do not have a specific port number, the default is 1099 way two: Manually creating the Rmiregistry service, you need to add it in your code: [Java] View Plaincopylocateregistry.createregistry (12312); 5, write the service-side code [Java] View plaincopy Packageserver; Importjava.rmi.Naming; ImportJava.rmi.registry.LocateRegistry;  Public classHelloServer { Public Static voidMain (string[] args) {Try{Hello h=NewHelloimpl (); //creates and exports a registry instance on the local host that accepts the specified port request. //locateregistry.createregistry (12312);                           /**The naming class provides methods for storing and obtaining remote reference to remote objects in the object registry * Each method of the naming class can use a name as one of its parameters, which is a URL format using the following form (no              Scheme component) java.lang.String: *//host:port/name * Host: Registry host (remote or local), omit default to localhost * Port: Is the port number that the registry accepts calls, omitting the default is the famous port used by the 1099,rmi Registry registry * Name: is a simple string that is not interpreted by the registry*/              //Naming.bind ("//host:port/name ", h); Naming.bind ("Rmi://192.168.58.164:12312/hello", h); System.out.println ("HelloServer started successfully"); }Catch(Exception e) {e.printstacktrace (); The registry must be created before remote object information can be stored in the registry6, run the service side (58.164): Test- -Server---Hello.class----Helloimpl.class----HelloServer.class[plain] view Plaincopy[[email protected]~]$ Java server. HelloServer HelloServer Startup success of course/home/name/test must be in the system classpath, otherwise you will not find the corresponding. class file7, writing client code [Java] View plaincopy Packageclient; Importjava.net.MalformedURLException; Importjava.rmi.Naming; Importjava.rmi.NotBoundException; Importjava.rmi.RemoteException; Importserver.    Hello;  Public classhelloclient { Public Static voidMain (string[] args) {Try{Hello h= (Hello) naming.lookup ("Rmi://192.168.58.164:12312/hello"); System.out.println (H.sayhello ("ZX")); } Catch(malformedurlexception e) {System.out.println ("URL Format exception"); } Catch(RemoteException e) {System.out.println ("Create Object Exception");          E.printstacktrace (); } Catch(notboundexception e) {System.out.println ("Object Not Bound"); }      }  }  8, run the client (58.163): Test- -Client----helloclient.class- -Server---Hello.class----helloimpl_stub.class//stub files generated by the server[plain] view Plaincopy[[email protected] client]$ java client. Helloclient hello,zx with server side,/home/name/test must be in the system classpath PS:1, the client is located in the service and server network must be through (waste a lot of time at first, finally found that the network is not through)2, all code in jdk1.5on the. 0_15,linux server on the debug pass3The classpath configuration issue [plain] view Plaincopy[[email protected] If the Java command runs to prompt for a class file is not found~]$ vi. bash_profile [plain] View Plaincopyjava_home=/home/name/jdk/jdk1.5. 0_15 Export Java_home PATH= $JAVA _home/Bin: $PATH export PATH CLASSPATH=.: $JAVA _home/lib/dt.jar: $JAVA _home/lib/tools.jar:/home/name/Test Export CLASSPATH java_home as the root directory of the JDK path for the Java tool classpath (java,javac,rmic, etc.) CLASSPATH the storage path for the Java. class file, When you run a. class file using the Java command, you will find the disadvantage of the corresponding file Java RMI under the path configured by this parameter:1, you can see from the code that the code is dependent on the IP and port2,rmi relies on the Java Remote Message Exchange protocol JRMP (Java Messaging Protocol), which is a Java customization that requires both the server and the client to be Java-written

Java.rmi.Naming Remote Access Object method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.