RMI starts

zhaozj2021-02-11  167

At the beginning of the article, I will first explain the origins of this article and translate it. At the beginning of the learning program, I like to learn from the specific program, from the original HelloWorld, then learn the specific syntax according to the program, learn the specific syntax, and learning programming is a constant loop, but the most fundamental is writer, and Not a strong book, go to experience the grammar from writing urging procedures, to experience a success, when you start contacting RMI, many reference books are talking a lot of principles, foundations, but there is no simple The implementation system is provided to us, and I still don't know how to do it. This is also a common problem of many programming books. Later, I found a RMI tutorial on the Sun website. It offers a detailed method to build a simple RMI system. I am very happy to see this. I hope to share this with everyone, so that I just started to contact RMI and think The people who learn RMI can get a little gain from it, so I translated this, there is no translation here, just translating the detailed steps of implementing this system.

RMI, Remote Method Invocation is the pillar of Enterprise JavaBeans, which is a convenient way to establish a distributed Java application. RMI is very easy to use, but it is very powerful.

The basis of RMI is the interface, and the RMI architecture is based on an important principle: the specific implementation of the interface and definition interface is separated. Below we use specific examples, establish a simple remote computing service and using its client programs.

A normally working RMI system consists of several parts:

● Interface definition of remote service

● Specific implementation of remote service interface

● STUB and Framework (SKELETON)

● A server running remote service

● A RMI naming service that allows the client to discover this remote service

● Provider for class files (an HTTP or FTP server)

● A client program that requires this remote service

Let's take a simple RMI system step by step. First create a new folder in your machine to place the files we created. For the sake of simplicity, we only use a folder to store the client and server code, and run the server and client in the same directory. .

If all the RMI files have been designed, then you need the following steps to generate your system:

1, write and compile the Java code

2, write and compile the Java code implemented

3. Implement the class generated pile (Skeleton) file from the interface.

4. Write the main program of remote service

5, write RMI client programs

6, install and run the RMI system

1, interface

The first step is to build and build Java code for service interface. This interface defines all the features that provide remote services. The following is the source program:

//Calculator.java

// define the interface

Import java.rmi.remote;

Public Interface Calculator Extends Remote

{

Public long add (long a, long b)

Throws java.rmi.remoteexception;

Public Long Sub (long A, long b)

Throws java.rmi.remoteexception;

Public Long Mul (Long A, Long B)

Throws java.rmi.remoteexception;

Public Long Div (Long A, Long B)

Throws java.rmi.remoteexception;

}

Note that this interface inherits from Remote, each defined method must throw a RemoteException exception object.

Establish this file and store it in the current directory and compile.

> Javac Calculator.java

2, the specific implementation of the interface

Next, we have to write the specific implementation of remote services, this is a CalculatorImpl class file:

//CalculatorImpl.java

// Implementation

Import java.rmi.server.UnicastRemoteObject

Public Class CalculatorImpl Extends UnicastRemoteObject Implements Calculator

{

// This implementation must have an explicit constructor, and to throw a RemoteException exception

Public CalculatorImpl ()

Throws java.rmi.remoteexception {

Super ();

}

Public long add (long a, long b)

Throws java.rmi.remoteexception {

RETURN A B;

}

Public Long Sub (long A, long b)

Throws java.rmi.remoteexception {

Return A - B;

}

Public Long Mul (Long A, Long B)

Throws java.rmi.remoteexception {

Return a * b;

}

Public Long Div (Long A, Long B)

Throws java.rmi.remoteexception {

Return A / B;

}

}

Similarly, save this file in your directory and compile him.

This implementation class uses UnicastRemoteObject to connect the RMI system. In our example, we are inherited from UnicastRemoteObject this class, in fact, do not have to do this, if a class is not inherited from UnicastrMeoteObject, it must be used to join the RMI.

If a class inherits from UnicastRemoteObject, it must provide a constructor and declare an RemoteException object. When this constructor calls Super (), it activates the code in UnicastRemoteObject to complete the connection of the RMI and the initialization of the remote object.

3, Stubs and Frames (Skeletons)

The next step is to use the RMI compiler RMIC to generate piles and frame files, this compile runs on the remote service implementation class file.

> RMIC CalculatorImpl

Run the above command in your directory, successfully executed the above command you can find a Calculator_stub.class file, if you are using Java2SDK, then you can also find the Calculator_skel.class file.

4, host server

The remote RMI service must be running in a server. The CalculatorServer class is a very simple server.

//Calculatorserver.java

Import java.rmi.naming;

Public class colculatorserver {

Public CalculatorServer () {

Try {

Calculator c = new CalculatorImpl (); Naming.Rebind ("RMI: // localhost: 1099 / CalculatorService", C);

} catch (exception e) {

System.out.println ("Trouble:" E);

}

}

Public static void main (string args []) {

New CalculatorServer ();

}

}

Create this server program and save it to your directory and compile it.

5, client

The client source code is as follows:

//Calculatorclient.java

Import java.rmi.naming;

Import java.rmi.remoteexception;

Import java.net.malformedurlexception;

Import java.rmi.notboundexception;

Public class calculatorclient {

Public static void main (String [] args) {

Try {

Calculator C = (Calculator)

Naming.lookup

"RMI: // localhost

/ CalculatorService ");

System.out.println (C.SUB (4, 3));

System.out.println (C.Add (4, 5));

System.out.Println (C.mul (3, 6));

System.out.println (C. Div (9, 3));

}

Catch (Malformedurlexception murle) {

SYSTEM.OUT.Println ();

System.out.println (

"MalFormedurlexception");

System.out.println (murle);

}

Catch (RemoteException Re) {

SYSTEM.OUT.Println ();

System.out.println (

RemoteException ");

System.out.println (re);

}

Catch (notboundexception nbe) {

SYSTEM.OUT.PRINTLN ();

System.out.println (

"Notboundexception");

System.out.println (NBE);

}

Catch (

Java.lang.arithMeticexception

AE) {

SYSTEM.OUT.Println ();

System.out.println (

"java.lang.arithmeticexception");

System.out.println (AE);

}

}

}

Save this client program to your directory (note that this directory is started at the beginning, all our files are in that directory) and compile him.

6, run the RMI system

Now we have established all the documents required to run this simple RMI system, now we can finally run this RMI system! Come and enjoy it.

We are running this system under the command console, you must open three console windows, a running server, a run client, and a running RMIREGISTRY.

First run the registration program RMIREGISTRY, you must run this registration program in the directory containing the class you just write.

> RMIREGISTRY

Ok, if this command succeeds, the registration program has begun to run, do not manage him, now switch to another console, in the second console, we run the server CalculatorService, enter the following command:> Java CalculatorServer

This server starts working, and load the interface to the memory waiting for the client. It is good to switch to the third console and launch our client.

> Java CalculatorClient

If all of these are successful, you should see the output below:

1

9

18

3

If you see the above output, congratulations, you have succeeded, you have successfully created an RMI system and make him work correctly. Even if you run on the same computer, RMI still uses your network stack and TCP / IP to communicate, and is running on three different Java virtual machines. This is already a complete RMI system.

转载请注明原文地址:https://www.9cbs.com/read-5281.html

New Post(0)