Learning notes: simple example of Java RMI remote method call

Source: Internet
Author: User

RMI Concept

Remote Method Invocation (RMI) remote method call is a communication mechanism in which computers use remote object calls to communicate with each other. Using this mechanism, objects on a computer can call objects on another computer to obtain remote data. RMI is the pillar of Enterprise JavaBeans and is used to build distributed Java applications.Program. In the past, TCP/IP socket communication was the main means of remote communication, but this development method did not use the object-oriented method for development, when developing such a communication mechanism, programmers often feel bored. RPC (Remote Procedure Call) emerged, making it easier for programmers to call remote programs, however, in the face of complex information messaging, RPC is still not well supported, and RPC is not the development mode of object-oriented calling. RMI is designed as an Object-Oriented communication method that allows programmers to use remote objects for communication and supports multi-threaded services, this is a revolution in remote communication and a new milestone for remote communication.

RMI development steps

    1. Create a remote interface and declare a remote method. Note that this is an interface for communication between the two parties.
    2. Develop a class to implement remote interfaces and remote methods. It is worth noting that the implementation class must inherit unicastremoteobject
    3. Use the javac command to compile the file, use the Java-server command to register the service, and start the remote object
    4. Finally, the client finds the remote object and calls the remote method.

Simple instance

First, create a model layer for the service. Note that because the object needs to be remotely transmitted, it must inherit the serializable

Code

Package RMI. model;

ImportJava. Io. serializable;

// note that the object must inherit serializable
Public class personentity implements serializable {
private int ID;
private string name;
private int age;

PublicVoidSetid (IntID ){
This. ID=ID;
}

PublicIntGETID (){
ReturnID;
}

PublicVoidSetname (string name ){
This. Name=Name;
}

PublicString getname (){
ReturnName;
}

PublicVoidSetage (IntAge ){
This. Age=Age;
}

PublicIntGetage (){
ReturnAge;
}
}

Create the remote interface personservice. Note that the remote interface must inherit the remote

Code

Package RMI. Service;

ImportJava. RMI. Remote;
ImportJava. RMI. RemoteException;
ImportJava. util. List;
ImportRMI. model.*;

//This is an interface called by a remote object and must inherit the remote class.
PublicInterfacePersonserviceExtendsRemote {
PublicList<Personentity>Getlist ()ThrowsRemoteException;
}

Create a personserviceimpl to implement a remote interface. Note that this is a remote object implementation class and must inherit unicastremoteobject

Code

Package RMI. serviceimpl;

ImportJava. RMI. RemoteException;
ImportJava. RMI. server. unicastremoteobject;
ImportJava. util. Collections list;
ImportJava. util. List;

ImportRMI. model. personentity;
ImportRMI. Service.*;

// This is an implementation class of a remote object. It must inherit unicastremoteobject
Public class personserviceimpl extends unicastremoteobject implements personservice {

PublicPersonserviceimpl ()ThrowsRemoteException {
Super();
//Todo auto-generated constructor stub
}

@ Override
Public List < Personentity > Getlist () Throws RemoteException {
// Todo auto-generated method stub
System. Out. println ( " Get person start! " );
List < Personentity > Personlist = New Shortlist < Personentity > ();

Personentity person1=NewPersonentity ();
Person1.setage (25);
Person1.setid (0);
Person1.setname ("Leslie");
Personlist. Add (person1 );

Personentity person2=NewPersonentity ();
Person2.setage (25);
Person2.setid (1);
Person2.setname ("Rose");
Personlist. Add (person2 );

ReturnPersonlist;
}
}

Create a server and register the RMI communication port and path on the server. Then, run the javac command to compile the file and run the Java-server command to register the service. BelowCodeFor example, if you create a project on the d :\\ RMI \ remotingservice folder, enter D :\\ RMI \ remotingservice \ SRC> javac RMI/remotingservice/program. java gets program. class (skip this step to directly call the generated program in the */bin folder. class), and then enter D :\\ RMI \ remotingservice \ SRC> java rmi/remotingservice/program to start the service.

Code

Package RMI. remotingservice;

ImportJava. RMI. Naming;
ImportJava. RMI. Registry. locateregistry;

ImportRMI. Service.*;
ImportRMI. serviceimpl.*;

PublicClassProgram {

Public Static Void Main (string [] ARGs ){
Try {
Personservice = New Personserviceimpl ();
// Register Communication Port
Locateregistry. createregistry ( 6600 );
// Register the communication path
Naming. rebind ( " RMI: // 127.0.0.1: 6600/personservice " , Personservice );
System. Out. println ( " Service start! " );
} Catch (Exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}

Finally, create a client for testing. Note that the RMI path called by the customer must be consistent with the server configuration.

Code

Package RMI. remotingclient;

ImportJava. RMI. Naming;
ImportJava. util. List;

ImportRMI. model. personentity;
ImportRMI. Service.*;

Public Class Program {
Public Static Void Main (string [] ARGs ){
Try {
// Call a remote object. Note that the RMI path and interface must be consistent with the server configuration.
Personservice = (Personservice) Naming. Lookup ( " RMI: // 127.0.0.1: 6600/personservice " );
List < Personentity > Personlist = Personservice. getlist ();
For (Personentity person: personlist ){
System. Out. println ( " ID: " + Person. GETID () + " Age: " + Person. getage () + " Name: " + Person. getname ());
}
} Catch (Exception ex ){
Ex. printstacktrace ();
}
}
}

Common Errors

  1. This command is not displayed when you call a Java command at a command prompt. This is because the JDK command of Java is not bound to the "environment variable, right-click your computer and choose Properties> advanced> environment variables ". Load JDK path in system variable path settings. D: \ Program Files \ genuitec \ common \ binary \ com. Sun. java. JDK. win32.x86 _ 1.6.0.013 \ bin. Then load the server's program. Class address in classpath.; D: \ RMI \ remotingservice \ bin
  2. when calling the javac command, the following error occurs: "javac cannot find the file ..... "This error may be caused by an error in the file path you entered. Do not set D :\\ RMI \ remotingservice \ SRC> javac RMI / remotingservice / program. java error: D :\\ RMI \ remotingservice \ SRC> javac RMI . remotingservice . program. java
  3. "Exception in thread 'main' Java occurs when calling the D: \ RMI \ remotingservice \ bin> java rmi/remotingservice/Program command. lang. noclassedfounderror "error. First, this may be becauseProgramThe error code is "program ".. ClassNote that the suffix is not required for Java commands. The second possibility is that you set "Java RMI/Remotingservice/The error code of program is "Java RMI ".\Remotingservice\Program ".

Source code(Download)

Remote Object callsArticle:

Review of. Net remoting distributed development

Learning notes: simple example of Java RMI remote method call

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.