Java Foundation 11 [rmi for remote deployment] (read head First Java record)

Source: Internet
Author: User
Tags naming convention stub

method calls occur between two objects on the same heap (the same Machine's Java virtual machine), and if you want to invoke an object on another machine, you can enter/output through the Socket. remote procedure calls need to create 4 things: servers, clients, server-assisted facilities, client-side facilities  RMIJava's JMI provides client and Server-side assistive facility objects (stubs and skeleton, which are now actually used only with stub files, one for the client and the service Side)the Auxiliary facility is the object that actually performs the communication, he will let the client feel in the call native, actually the auxiliary facility resembles the agent, sends the information that the client transmits through the socket connection to the service side auxiliary facility, then the service side auxiliary facility unpack calls the real service side the service, Service assistance facilities after the result, he is returned to the Client-side facility via a socket connection, which the Client-side facility unlocks to the client the Remote Service invocation is implemented through JAVA.RMI.REGISTRY. It provides a class and two interfaces for the RMI registry, a registry binding string name, and a remote object that can invoke an object from the remote service through the registry Server-side Service implements a true method of remote invocation Client assistance Facilities and service aids are the same file that is generated on the server side  Create a remote service the approximate steps are as follows:   Step 1: Create the remote interfacedefines the methods that the client invokes, and the stubs and services implement this interface. The remote interface defines a method that the client can invoke Remotely.  Import java.rmi.*;public interface MyRemote extends Remote{public String SayHello () throws remoteexception;} description:1. Interfaces must inherit from remote2. Remote invocation is risky and requires the client to notice this by throwing a RemoteException exception3. The parameters and return values of the remote method must be primitive or serializable  Step 2: Implement the remote interfaceThis is the real implementation of the class, the implementation of the interface on the method, but also the client will invoke the object, to perform the real workpublic class MyRemoteImp1 extends unicastremoteobject implements MyRemote{public MyRemoteImp1 () throws remoteexception{}//unicastremoteobject throws a remoteexception exception, the constructor throws the same exception public String SayHello () {//all Methods for implementing the remote interfacereturn "Server says, ' Hey '"     }} description:1. Implementing the Remote interface2. To have remote-related features, you can inherit UnicastRemoteObject (unicastremoteobject throws the RemoteException exception, and throws the same exception if the call requires a constructor)3. Registering the service with RMI Registryonly to the RMI registry registered to allow remote users access (must be RMI registry must be registered or run the program will fail), register the object when RMI will add the stub to registry, which is required by the client, Using Java.rmi.Naming's rebind () Registration Servicetry{MyRemote service=new MyRemoteIm1 ();naming.rebind ("remote Hello", service);//register The implemented interface class instance to RMI registry and help name the service (Remote hello)}catch (Exception Ex) {...}   step 3: generate stubs with rmicexecutes rmic on a class that is actually implemented, generating a helper class (stub)_stub is added to the name of the remote implementation following the naming Convention. rmic MYREMOTEIMP1the myremoteimp1_stub.class file is generated   Step 4: Start RMI Registry (rmiregistry)the user obtains the proxy from here (the client's sub/helper Object)you need to execute the command in the implemented service Class: RmiregistryIf you want to specify a port number, you can use the "rmiregistry port number" to start  Step 5: start the remote service"java really implements class" starts the service, and instances of the class that implement the service are registered with RMI Registry (after registration to service the User)Java MYREMOTEIMP1  Specify the service port numberTwo Methods: One is specified in the program, and the second is the specified when starting Rmiregistryspecified in the program: start rmiregistry specify: "rmiregistry Port number"  Client Implementation  client obtains stub objectCopy the stub files generated on the server directly to the client . the client calls the remote methodthe static method of Naming.lookup () is used to find a remote method registered on an IP (through naming.rebind () registration), returning a stub object MyRemote service= (MyRemote) naming.lookup ("rmi://127.0.0.1: service port number/remote Hello"); If Naming.lookup does not specify a port number for the service, use the Java default 1099 port number  description:1. The client invocation service must use the same interface type as the server2.naming.lookup () Addressing the format "rmi://server address/registered remote method name", Returns a stub object3. Client local must have a stub class generated by the server rmic, otherwise the stub object returned by RMI registry will not be deserialized  considerations for RMI1. Before starting the remote service (the Java Implementation Service class), you must first start the Rmiregistry2. The parameter and return type must be serializable3. Handing the stub class to the client4. The client and the server must be able to ping through, otherwise you cannot call   problems encountered questions:client prompt address illegal at startup: java.net.MalformedURLException:invalid URL string:rmi://127.0.0.1:2000/remote Hello workaround:Convert the remote service address to the encoding formatString url= "rmi://127.0.0.1:2000/remote Hello";
Url= Urlencoder.encode (url, "UTF-8");MyRemote service= (MyRemote) naming.lookup (url);     

Java Foundation 11 [rmi for remote deployment] (read head First Java record)

Related Article

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.