Proxy mode Article 1

Source: Internet
Author: User

In short, there is a remote service and several monitors, which are used to monitor data in the remote service. Monitoring Task

The obstacle is that the remote service and the monitor program are not on the same JVM. As a result, the monitor cannot directly call the server locally.

Method. We need a proxy. What the monitor does is like a remote method call, but it only calls

The method on the "proxy" object in the local heap, and then the agent processes all the low-layer details of network communication.

To create a remote service, follow these steps:

1. Create a remote interface that defines the remote method to be called by the customer.

Myremote. Java: interface myremote extends remote (interface inherited Interface)

2. Create remote implementation. This is the method that the customer wants to call.

Myremoteimpl. Java: Class myremoteimpl extends unicastremoteobject implements remote (

Unicastremostobject makes the superclass help subclass A remote object and has some "remote" functions)

// 3. Design a constructor without variables and declare RemoteException. If the hyperclass constructor throws an exception, you can only declare

The constructor of the subclass also throws an exception. The compiler will check the error and give a prompt.

Public myremoteimpl () throws RemoteException {}

3. stub and skeleton generated by rmic.

% Rmic myremoteimpl,

Two new classes will be generated as auxiliary objects: myremoteimpl_stub.class and myremoteimpl_skel.class.

4. Use RMI registry to register this service

% Rmiregistry

5. Start the remote service.

% Java myremoteimpl

The remote service implementation code is as follows:

Myremote interface:

Public interface myremote extends remote {
Public String sayhello () throws RemoteException;
}

Myremoteimple service class:

Public class myremoteimpl extends unicastremoteobject implements myremote {

Protected myremoteimpl () throws RemoteException {
Super ();
// Todo auto-generated constructor stub
}

@ Override
Public String sayhello () throws RemoteException {
// Todo auto-generated method stub
Return "server says, 'hs '";
}

Public static void main (string [] ARGs)
{
Try {
Myremote service = new myremoteimpl ();
Naming. rebind ("remotehello", Service );
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

}

}

Let's look at the working method of the customer:

1. The customer went to RMI registry to search for it.

Naming. Lookup ("RMI: // 127.0.0.1/remotehello ");

2. Rmi registry returns the stub object, and RMI will automatically deserialize stub. You must have stub class on the client,

Otherwise, Stub cannot be deserialized.

3. The customer calls the stub method, just as stub is a real service object.

Complete client code:

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 ex)
{
Ex. printstacktrace ();
}

}
}

Shows the compilation process:

Generate stub and skeleton, and start RMI registry. Rmiregistry is like a phone book, from which the customer can find the proxy location (that is, the customer's Stub helper object ).

Start the remote service:

Then copy the corresponding. class files generated by the server during compilation, including stub class files, to the client class folder. Then the client class

Compile and run. As shown in:

At this point, our tasks have been successfully completed.

We encountered some obstacles in implementing this process. Before this blog is over, we will summarize the implementation process:

1. During the use of rmic to generate stub and skeleton classes, the class cannot be found. Solution: Set classpath = compile and generate

The path of the remote service class file. Pay attention to the problem of the class package.

2. rmic is only used to create stub and skeleton for remote services. For non-remote services, the following error occurs:

3. Before compiling the client code, put the corresponding class files and stub classes in the remote service under the client class files.

4. When compiling the Customer Code class, you must first set the classpath variable: Set classpath = "Path of the Customer Code class"

5. The variable and return value types transmitted so far by the customer and remote service must be serializable. Because it can be passed only when it is serializable

The network packages and transmits the variables and returned values.

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.