Proxy Pattern _ remote Proxy Parsing
1. What is proxy mode? As the name suggests, a proxy is a third party. For example, if a celebrity broker is responsible for handling all matters of a star, the agent will naturally find a way to do so by telling the agent what to do, after that, we can tell the stars the result, which is a direct interaction between the caller and the called. Now, the caller and the called are separated, and the agent is responsible for transmitting information to complete the call. what is the use of proxy mode? The proxy mode is a large mode, so it is widely used. It can be seen from the agent type: remote proxy: One of the most classic proxy modes, the remote proxy is responsible for communicating with the remote JVM to achieve normal interaction between the local caller and the remote caller. The virtual proxy is used to replace a large object and ensure that it is created as needed. protection proxy: provide access control for the caller, and confirm the caller's permissions. In addition, there are firewall proxy, intelligent reference proxy, cache proxy, synchronization proxy, complex hidden proxy, and copy proxy upon writing, they all have their own special uses P.S. remote proxy is the most basic proxy mode. It is necessary to discuss it separately. Therefore, this article will explain it in detail. Other proxies will detail it in the supplementary blog. remote proxy can easily solve some tasks without proxy, but some tasks must be done by proxy, for example, to call a method on another machine, we may have to use the internal mechanism of the remote proxy as follows: to explain, Stub is a "pile" or someone calls it a "Stub ", it indicates that the Server object Skeleton is a "Skeleton" (I don't know why it is called "pile" or "Skeleton" ", Of course, there is no need to know). It indicates that Client Stub is clearly on the customer's side. Why is it not the customer's proxy but the service's proxy? Because the customer wants to interact with the Server, and the service cannot interact in the remote JVM, Stub is used to represent the Server. Calling Stub is equivalent to calling the Server (the internal communication mechanism is transparent to the Client, for the Client, there is no difference between calling Stub and directly calling the Server, which is one of the advantages of the proxy mode) the specific process is as follows: the Client sends a method call request to Stub (the Client thinks Stub is the Server). Stub receives the request and communicates with the Server's Skeleton through the Socket. The call request is passed to SkeletonSkeleton to receive the request, call the local Server (which sounds a little strange. Here the Server is equivalent to the Service) Server to send the result to the caller SkeletonSkeleton. After receiving the result, it sends it to StubStub through Socket and passes the result to ClientP. s. both steps 1 and 2 need to communicate through Socket, and all the items transmitted to each other must be serialized and received before being sent. Post-deserialization, which explains why the public method return values in Server must be serializable. remote proxy can be implemented in two ways: customizing Stub and Skeleton (implementing internal communication) using Java-supported RMI (Remote Method Invocation, it can save a lot of trouble, but it is not easy to understand the internal principles. First, let's give an example of a self-defined method. A blog post about this is very good, so I recorded the link without authorization, click "I jump"> the original text provides a complete example, so I will not go into details here. I will add a pseudo-class image to the original text for ease of understanding: (do not ask me why the class image is drawn like this, it is a "pseudo" class graph ..) Taking a closer look at the original text, it is not difficult to understand the remote proxy. Stub and Skeleton are responsible for communication, similar to the chat program written using Socket, in addition, there is nothing special ------- the following section describes how to use RMI supported by Java to implement the proxy mode. Obviously, many details are hidden. First, you must define a remote interface: package ProxyPattern; import java. rmi. remoteException;/*** defines the Service Interface (extended from java. rmi. remote interface) * @ author ayqy */public interface Service extends java. rmi. remote {/* 1. method return type must be Serializable * 2. every method must declare the exception throws RemoteException (because it is the RMI method) ** // *** @ return complete greeting statement * @ throws Rem OteException */public String greet (String name) throws RemoteException;} Note: The return type of the public method in the service interface must be serializable (in other words, the custom return type must implement the Serializable Interface), and the String type has implemented the Serializable interface. Why do we need to define such an extension from java. rmi. remote interface? The API documentation provides a clear explanation: The Remote interface serves to identify interfaces whose methods may be invoked from a non-local virtual machine. any object that is a remote object must directly or indirectly implement this interface. only those methods specified in a "remote interface", an interface that extends java. rmi. remote are available remotely. to put it bluntly, we want to tell the compiler that our Service object can be called remotely. That's all. ------- package ProxyPattern; import java. rmi. remoteException; import java. rmi. server. unicastRemoteObject;/*** remote service (extended from UnicastRemoteObject and implemented custom remote interface) * @ author ayqy */public class MyService extends UnicastRemoteObject implements Service {/*** is used to verify the program version (the acceptor will verify the UID in deserialization, causing an exception if it does not match) */private static final long serialVersionUID = 1L;/*** empty constructor, just to declare an exception (the default constructor does not declare an exception) * @ throws RemoteException */protected MyService () throws RemoteException {}@ Override public String greet (String name) throws RemoteException {return "Hey," + name ;}} package ProxyPattern; import java.net. malformedURLException; import java. rmi. naming; import java. rmi. remoteException; import java. rmi. registry. locateRegistry;/*** implements the Server class, which is responsible for enabling the Service and registering the service object * @ author ayqy */public class Server {public static void main (String [] args) {try {// start the RMI registration service. The specified port is 1099 (1099 is the default port) LocateRegistry. createRegistry (1099); // create the service object MyService service = new MyService (); // register the service on the RMI registration server and name it MyService Naming. rebind ("MyService", service);} catch (RemoteException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (MalformedURLException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} note that besides using LocateRegistry. in addition to enabling the Service in createRegistry () mode, you can also use the command line (rmiregistry command) to enable the Service. In the same effect, the server code is completed here, just like the Socket chat program, we need to write two parts of code: Server and Client -------