Design Pattern (11)-RMI of proxy pattern

Source: Internet
Author: User

(1) Determine whether the variables and returned values belong to the primitive type or serializable Seriailizable type. All primitive types implement the Seriailizable interface.

(2) The transient keyword tells JVM not to serialize this field.

Dynamic class downloading)

 

Proxy mode: Controls Object Access

 

There are many proxy modes. For example, remote proxy or virtual proxy. Dynamic proxy (protection proxy)

The customer calls the local method and the local method calls the remote method. The local method is "proxy ". The "proxy" processes the Low-layer details of all network communications.

Java built-in remote call function: RMI

Benefits of RMI:

No need to write any network or I/O Code.

The client program calls the remote method (that is, the real service), just like the normal method call of the object running the local JVM.

The lookup service is used to search for and access remote objects.

Note: I/O and the network are subject to network risks and are prone to failures. Therefore, exceptions must be thrown at any time.

------------------- Remote Call implementation steps ---------------------

RMI will generate the class name_stub in the format of the client auxiliary object (called stub (pile. class) and the server auxiliary object (called skeleton (skeleton) naming format class name_skel. class ).

1. Create a remote service (remote end)

(1) create a remote interface. The interface defines a method for the customer to remotely call.

1. Extended java. rmi. Remote is a "mark" interface and does not have a method. Remote is of special significance for RMI.

Public interface MyRemote extends Remote {}// indicates that this interface is used to support Remote calls.

2. declare that all methods throw RemoteException.

 

import java.rmi.*;public interface MyRemote extends Remote{   public String sayHello() throws RemoteException;}
3. Determine whether the variables and returned values belong to the primitive type or Serializable type.

 

Variables of the remote method must be packaged and shipped over the network, which depends on serialization.

(2) create a remote interface implementation.

1. public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {

// SayHello implementation

}

2. Extended UnicastRemoteOject. In order to become a remote service object, Your object requires some "remote" functions, which are implemented in the simplest way.

3. Design a constructor without variables and declare RemoteException

 

/** The new super class UnicassRemoteObject brings about a small problem: its constructor throws RemoteException. The only way to solve this problem is that this exception is also declared. If the constructor of the superclass throws an exception, the constructor of the subclass must also throw an exception. Because the construction of the parent class is called before the subclass construction is called, the exception thrown by the parent class must be less than or equal to the exception thrown by the subclass */public MyRemoteImpl () throws RemoteException
4. Use RMI Registry to register this service. In the mian () method. It is equivalent to step 1.

 

 

Let it be called by remote customers, instantiate the service, and put it into RMI registry (remember to confirm that rmi registry is running, otherwise the registration will fail ). When registering this object, the RMI system actually registers stub, because this is what the customer really needs. The Registration Service uses the static rebind () method of the java. rmi. Naming class. Try {MyRemote service = new MyRemoteImpl (); Naming. rebind ("RemoteHello", service); // name the service so that the customer can search for it in the registry .} Catch (Exception ex ){...}


 

(3) Use rmic to generate stub and skeleton. This tool is available in JDK % rmic MyRemoteIMpl

Execute rmic on remote implementation class

(4) Start RMI regisgry (rmiregistry). rmiregisty is like a phone book, from which the customer can find the proxy location. % Rmiregistry -- start a terminal and start rmiregistry to make sure that the startup directory must be accessible to your class. The "classes" directory is started.

(5) enable remote service % java MyServiceImpl

2. How the client obtains the Stub object.

The customer must obtain the stub object (Our proxy) to call the method. Therefore, we need the help of RMI Registry. The customer looks for (lookup) proxy from the Registry.

MyRemote service = (MyRemote) Naming. lookup ("rmi: // 127.0.0.1/RemoteHello"); // The name used for remote registration.

Iii. Working methods.

(1) The customer goes to the registry to search. Naming. lookup ("rmi: // 127.0.0.1/RemoteHello ");

(2) RMI registry returns the stub object. (As the return value of the lookup method) Then RMI will automatically deserialize stub. You must have a stub class (rmic) on the client to generate it for you. Otherwise, stub cannot be deserialized.

(3) Call the stub method, just as stub is a real service object.

Complete client code

 

import java.rmi.Naming;public class MyRemoteClient {public static void main(String[] args) {new MyRemoteClient().go();}public void go(){try{MyRemote service = (MyRemote)Naming.lookup("rmi://127.0.0.1/RemoteHello");    String s = service.sayHello();System.out.println(s);}catch(Exception e){e.getMessage();}}}

Before registering a service, you must open a command window to execute the rmiregstry command. This command can be executed in a visible path of the remote class.

 

Complete remote interface code

 

import java.rmi.Remote;import java.rmi.RemoteException;public interface MyRemote extends Remote{public String sayHello() throws RemoteException;}
Complete remote implementation code

 

 

import java.rmi.Naming;import java.rmi.RemoteException;import java.rmi.server.UnicastRemoteObject;public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {@Overridepublic String sayHello() throws RemoteException {return "Server says ";}public MyRemoteImpl() throws RemoteException{}public static void main(String[] args) {try {MyRemote service = new MyRemoteImpl();Naming.rebind("RemoteHello", service);} catch (Exception e) {e.printStackTrace();System.out.println("111");}}}


 

 

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.