Eclipse Java RMI

Source: Internet
Author: User
Eclipse Java RMI

Keyword: RMI

I recently read some related content about RMI on the Internet, but it is always unsuccessful to write an example. after a period of improvement. the operation was successful. here, I will share my example with you.

Before writing an example, let's take a look at RMI knowledge.

What is RMI?

Remote method call is a mechanism in which objects in a computer call each other's functions and start processes of the other party,

Using this mechanism, when an object on a computer calls a method on another computer

The sequence syntax rules are the same as those for calling methods between objects on the local machine.

Advantages

This mechanism brings great convenience to distributed computing system design and programming.

As long as you design the program according to the RMI rules, you do not have to worry about the network details under RMI, such as TCP and socket.

The communication between any two computers is solely the responsibility of RMI. Calling objects on a remote computer is as convenient as a local object.

1. Object-oriented:

RMI can pass complete objects as parameters and return values, not just pre-defined data types.

That is to say, a complex type similar to a Java Hasse table can be passed as a parameter.

2. movable attributes:

RMI can move properties from the client to the server or from the server to the client.

3. Design Method:

The object transfer feature allows you to take full advantage of the powerful features of object-oriented technology in distributed computing, such as layer-2 and layer-3 structured systems.

If you can pass attributes, you can use the object-oriented design method in your own solutions.

All object-oriented design methods rely on different attributes to function. If the complete object cannot be passed-including implementation and type

-- The advantages provided by the design method will be lost.

4. Security:

RMI uses the built-in JAVA security mechanism to ensure the security of the user system when downloading and executing programs.

RMI uses security management programs designed specifically to protect the system from malicious applets.

5. Easy to write and use

RMI makes it easy and easy to write Java remote service programs and Java client programs that access these service programs.

The remote interface is actually a Java interface.

To implement the RMI function, you must create a remote object. Any objects that can be called remotely must implement remote interfaces. But remote

The interface itself does not contain any method. Therefore, you need to create a new interface to expand the remote interface.

The New Interface contains all methods that can be called remotely. The remote object must implement this new interface because the new interface is extended.

The remote interface implements a new interface to meet the requirements of the remote object for implementing the remote interface.

As a remote object reference.

Personal summary:

To put it bluntly, RMI provides a remote method call. This call is simple and convenient, and can pass complex Java objects. Currently, the underlying implementation technology of EJB in popular J2EE is RMI. EJB calls are encapsulated and more advanced RMI calls.

Next we will write a RMI program:

1. Six steps to create an Rmi program:

1. Define a remote interface

2. implement this remote interface

3. Development Server

4. Develop the client

5. Generate the stub and baseline, and start the RMI registry, server, and client (you can use rmic to generate the stub and baseline in DOS)

Ii. Detailed procedures

Eclipse RMI plug-in is http://www.genady.net/rmi/v16/

Download v1.6.5 for eclipse 3.1.x

Download and decompress features and plugins and copy them to the corresponding eclipse folder.

First, create a Java project "rmidemo1" (my items are I:/codedemo/elispe/rmidemo1/rmidemo1. Here, pay special attention to the storage directory)

1. Define a remote interface. Each method in this interface must declare that it will generate a RemoteException.

Import java. RMI. Remote;

Import java. RMI. RemoteException;

Public interface sayhello extends remote {// need to inherit from remote

Public String sayhello (string info) throws remote=tio // remote exception n needs to be thrown;

}

2. Define a class to implement this interface.

Import java. RMI. RemoteException;

Import java. RMI. server. unicastremoteobject;

Public class sayhelloimpl extends unicastremoteobject implements sayhello {

Protected sayhelloimpl () throws RemoteException {

Super ();

// Todo auto-generated constructor stub

}

Public String sayhello (string info) throws RemoteException {

Return "Hello =" + Info;

}

}

The class that implements the interface must inherit the unicastremoteobject class.

Java. RMI. server. unicastremoteobject Extension

As the name implies, unicastremoteobject is used to establish a one-to-one connection between the client and the server object instance.

3. Generate the root file of the program

Right-click rmidemo1 and choose RMI -- enable stubs generation. The sayhelloimpl_stub.class is automatically generated in the bin/folder.

4. Create a server class

Import java. RMI .*;

Import java.net .*;

/**

* <P> title: </P>

*

* <P> Description: </P>

*

* <P> copyright: Copyright (c) 2007 </P>

*

* <P> company: </P>

*

* @ Author not attributable

* @ Version 1.0

*/

Public class sayrmiserver {

Public sayrmiserver (){

}

Public static void main (string ARGs []) throws RemoteException, malformedurlexception {

Sayhelloimpl add = new sayhelloimpl ();

Naming. rebind ("addnumbers", add );

}

}

5. Create a client class

Import java. RMI .*;

Import java.net .*;

/**

* <P> title: </P>

*

* <P> Description: </P>

*

* <P> copyright: Copyright (c) 2007 </P>

*

* <P> company: </P>

*

* @ Author not attributable

* @ Version 1.0

*/

Public class sayrmiclient {

Public sayrmiclient (){

}

Public static void main (string ARGs []) throws RemoteException,

Malformedurlexception, notboundexception {

String url = "RMI: // 127.0.0.1/addnumbers ";

Sayhello add = (sayhello) Naming. Lookup (URL );

String result = Add. sayhello ("hubin ");

System. Out. println (result );

}

}

Iii. Run the debugging project

Before running the job, you need to set it. Otherwise, an error message is displayed, indicating that stub class cannot be found.

1. Start rmiregistry

Eclipse menu window-> show view-> others, select RMI views-> RMI registry inspector in the pop-up menu. An additional window is displayed, showing registered RMI applications.

Click the RMI plugin icon on the toolbar and select start local registry from the menu.

Run --> cmd ---> Cd
Enter the directory where the class file generated by your project is located, for example
I:/codedemo/elispe/rmidemo1/rmidemo1/bin, run the rmiregistry command in the current directory). If it is not stored, note
Book, your service will always be unable to connect. Currently I have not found a good way to set, if which has a good way to tell me. mail: live525@126.com

Note that this only needs to be started once unless you turn it off.

2. Configure and run the RMI Service

Right-click the rmi_server.java file in the left-side tree and choose debug as> RMI application. In the displayed dialog box, find the RMI Properties tab.

The first two items are displayed in red.


Select the value box of the Java. Security. Police item and click the select button to display the file selection dialog box. Here we set it to C:/Java
/Jre1.5.0 _ 05/lib/security/Java. Security. Is the JRE security policy configuration file, you need to select the side of the JDK used currently.
.

Select the second java. RMI. server. codebase
Value. Here, select the folder where the class package is compiled. Click "select"> "add"> "pick from ".
Workspace, select the bin folder of the current project rmitest. Here is file:/I:/codedemo/elispe/rmidemo1
/Rmidemo1/bin.

Click Apply.

Click debug.

4. Click rmiserver and select Run as -- RMI Application

5. Click rmiclient and select Run as -- Java application

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.