In fact, I have recently invited the general altar to open a Java distributed application, specializing in Java distributed applications. Such as JINI, CORBA, RMI, RMI-IIOP, etc. Further, there is a problem including communication between various technologies. For example, RMI-IIOP and CORBA Communications, Jini Access to advanced issues such as CORBA objects, EJBs to CORBA mapping. However, there is no realization yet. So let's discuss these things first. RMI is a distributed application program based on Java technology. The client and server are required to write code using Java technology. Its characteristics are simplicity. But this technology is indeed important because Jini technology is based on RMI. CORBA is another object-based distributed application specification established by OMG organization. Realized system-independence (this and RMI similar) and language independence (this is the largest difference between RMI) and established an IDL specification to implement maps (including C / C , Java, Cobol, Ada, Smalltalk, Many languages such as LISP, Python) Combine ORB to implement communication, and due to specifications, different ORB products can also achieve interoperability (such as Sun's ORB, Visibroker, orbacus, etc.). However, other language written orb is theoretically interoperable, but how can I not even say in Sun's documentation, at least they have not tested. There is also a little more important, that is, the agreement used by the ORB is IIOP. Add a point: To make up for the shortcomings of RMI (unable to interoperate with CORBA objects) Sun launched RMI-IIOP technology, you can define interfaces (not IDL) in the Java language, and extend JNDI, add Cosnaming Service Provider. Combined with the two can achieve RMI (the RMI here is not a JRMP protocol, but IIOP) and the CORBA object interoperability. This is a great technology that uses Java to define the remote interface but not only the RMI can use the IIOP protocol, and other IIOP protocols can be used such as EJB, Jini. The ejb to corba mapping article on the Sun Site is to describe the client written by C / C to access the EJB server. Is it very moving? Just say this, it is very troublesome as it is, it can be said.
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 interfaceimport 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 One definition 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//Implementationimport java.rmi.server.UnicastRemoteObjectpublic class CalculatorImpl extends UnicastRemoteObject implements Calculator {// This implementation must have an explicit constructor, and to throw an exception RemoteException public CalculatorImpl () throws java.rmi .RemoteException {Super ();} public long address (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 long (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;} } The same, 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.javaimport java.rmi.Naming; public class CalculatorServer {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 ();}} Set this server program, then save it to you Under the 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); (RemoteException Re) {system.out.println (); System.out.Println ("remoteexception"); system.out.println (Re); } Catch (notboundexception nbe) {SY Stem.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 with that, all our files In that directory) and compiled 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, don't 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 a 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.
Voting scores are particularly good, very good, a little difference is too bad.
At the bottom of the form
JavaRMI Getting combat Keywords: Java RMI Author: renrzg through the network to execute code on another machine, the traditional method is not only difficult to learn, but also error-prone. The best way to solve this problem is: Some objects are just in another machine, we can send a message and get the return result, just like your own machine. Java Remote Method Call (RMI) Features Enables programs running on the client to invoke objects on the remote server. Remote Method Call Features Enables Java programmers to distribute operations in a network environment. Let's introduce the necessary steps to create your own RMI object. I. Remote Interface Concept: RMI has strong dependence on the interface. When you need to create a remote object, we hide the implementation details of the grassroots by passing an interface. So the customer gets a handle of the remote object exactly the same local root code, and the latter is responsible for communicating over the network. But we don't care about these things, send messages through your own interface handle. When you create a remote interface, you must follow the following rules: 1) The remote interface must be a public attribute (not "package access"; that is, he can't be "friendly"). Otherwise, once the customer tries to load a remote object that implements a remote interface, it will get an error. 2) The remote interface must expand the interface java.rmi.remote. 3) In addition to violations related to the application itself, each method in the remote interface must declare java.rmi.RemoteException.4) as a remote object passed as a parameter or return value (whether directly, Or embedding in the local object) must be declared as a remote interface and cannot be declared as a category. The following example is a remote interface, // PerfectTimeI.java // The PerfectTime remote interface package test; import java.rmi *;. Public interface PerfectTimeI extends Remote {long getPerfectTime (throws RemoteException);} on its surface similar to other interface It is only extended to Remote, and all methods will "throw" removeexception. Interfaces and methods are public. Compile PerfectTimei.java, generate PerfectTimei.class (Test is a package, pay attention to the path) G: / RMI> Javac test / perfectTIMEI.JAVA II, Remote Interface Implementation: The server must contain an extended UnicastRemoteObject class and implement remote interface . This class can also contain additional methods, but customers can only use methods in remote interfaces. Because the customer is a handle pointing to the interface, not which class it. The component must be defined for the remote object, even if only one default member device is defined, use it to call the underlying type member. It must be explicitly written because it must "throw" RemoteException violation. The fact that the remote interface PerfectTime is listed below: he represents the exact timing service //perfecttime.java // The Implectation of the PerfectTime Remote Object Package Test; import java.rmi. *; Import java.rmi .registry. *; import java.rmi.server. *; public class perfectTime Extends UnicastRemoteObject IMPLEments PerfectTimei {// Default member, but also "throw" RemoteException violation.
public PerfectTime () throws RemoteException {super ();} public long getPerfectTime () throws RemoteException {return System.currentTimeMillis ();} public static void main (String [] args) {/ * create and install a security manager, so It supports RMI. As part of the Java development package, it is suitable for RMISECurityManager. * ///System.setSecurityManager (new rmisecurityManager ()); try {/ * Create one or more instances of remote objects, below is PerfectTime Object * / PerfectTime Pt = New PerfectTime (); / * Register at least one remote object to the RMI Remote Object Registry. A remote object has a method to generate a handle to other remote objects, so that the customer visits once in the registry, get the first remote object. * / Naming.Rebind ("PerfectTime", PT); System. Out.println ("Ready to Do Time");} catch (Exception E) {E.PrintStackTrace ();}}} Compile PerfectTime.java, generate PerfectTime.class (TEST is package, pay attention to the path when compiling) G: / RMI> Javac Test / PerfectTime.java 3, create roots and dry: Create RemoteObject's backbone and framework. To do this, you can use the RMIC compiler, the RMIC compiler generates the stub and skeleton of the remote object. STUB is a remote object at the client's agent, which passes the RMI call to the server-side skeleton (the latter is responsible for passing the call to the actual remote method) as follows: g: / rmi> RMIC -D G: / RMI Test.PerfectTime executes this command, if the RMIC is successfully run, there will be more new classes in the TEST directory: PerfectTime_stub.class performttime_skel.class They correspond to the root (STUB) and dryTON. IV, use Remote objects: RMI all the purpose is to simplify the use of remote interface objects. The only extra thing to do in our client program is to find the remote interface from the server. Here is a program written in Java: sending messages to objects: //DisplayPerfectTime.java // Users remote object PerfectTime package test; import java.rmi *; import java.rmi.registry *; public class DisplayPerfectTime {/ **.. * DisplayPerfectTime constructor annotation.
* / Public DisplayPerfectTime () {super ();} public static void main (String [] args) {//System.setSecurityManager(new RMISecurityManager ()); try {PerfectTimeI t = (PerfectTimeI) Naming.lookup ( "PerfectTime" ); for (int i = 0; i <10; i ) {system.out.println ("PerfectTime:" T. GetPerfectTime ());}}}} catch (exception e) {E.PrintStackTrace ();}} } Compile DisplayPerfectTime.java.g: / rmi> Javac Test / DisplayPerfectTime.java 5. Start registration and run code: Before running the PerfectTime class and the DisplayPectTime class, the user must first start RMI registration (Registry) on the computer where you want host PerfectTime. This step is also necessary, even if you want to run PerfectTime's computer and run displayPerfectTime. This step is also necessary. The name of the registry server is RMIREGISTRY. In the 32-bit Windows environment, you can use: Start RMIREGISTRY makes it run in the background. Then open two different processes to run the Server and the Client side: Start the registry server: g: / rmi> Start RMIREGISTRY Bind PerfectTime to register, run the server program: Under Windows, enter the following command, start PerfectTime in the background program: G: / RMI> java test.PerfectTime Ready to do Time running the client program: as G: / RMI> java test.DisplayPerfectTime PerfectTime: 961722589649 PerfectTime: 961722589669 PerfectTime: 961722589679 PerfectTime: 961722589679 PerfectTime: 961722589689 PerfectTime: 961722589689 PerfectTime: 96172589689 PerfectTime: 961722589699 PerfectTime: 961722589699 PerfectTime: 96172589699 Java CORBA Getting Started!
Author: jdeveloper
Below Is A Simple Example of A Corba Program
Download the Source File
1. Product a IDL File Like this
Hello.idl
Module HelloApp {
Interface hello {
String syhello ();
}
}
2. Produce Stub and Skeleton Files Through IDltojava.exe
IDLTOJAVA HELLO.IDL
Idltojava is now named as idlj.exe and is inclined in the jdk.
3. Write a Server Program Like this
// HelloServer.java
Import helloApp. *;
Import org.omg.cosnaming. *;
Import org.omg.cosnaming.namingContextPackage. *; import org.omg.corba. *;
Import java.io. *;
Class Helloservant Extends_HelloImplbase
{
Public String Sayhello ()
{
Return "/ NHELLO World !! / N";
}
}
Public class helloserver {
Public static void main (string args [])
{
Try {
// Create and Initialize the ORB
ORB ORB = Orb.init (args, null);
// Create Servant and Register IT with the ORB
Helloservant helloref = new helloservant ();
ORB.CONNECT (Helloref);
// Get the root naming context
Org.omg.corba.Object objref =
Orb.resolve_initial_references ("Nameservice");
NamingContext ncref = NamingContextHelper.narrow (Objref);
// bind the object reason in naming
NameComponent NC = New NameComponent ("Hello", ");
Namecomponent path [] = {nc};
ncref.rebind (path, helloref);
// Wait for Invocations from Clom Clom Clom Clom Clom Clom Clom Cliants
Java.lang.object sync = new java.lang.Object ();
Synchronized (sync) {
Sync.wait ();
}
} catch (exception e) {
System.err.println ("Error:" E);
E.PrintStackTrace (System.out);
}
}
}
4. Write a Client Program Like this
// HelloClient.java
Import helloApp. *;
Import org.omg.cosnaming. *;
Import org.omg.corba. *;
Public class helloclient
{
Public static void main (string args [])
{
Try {
// Create and Initialize the ORB
ORB ORB = Orb.init (args, null);
// Get the root naming context
Org.omg.corba.Object objref =
Orb.resolve_initial_references ("Nameservice");
NamingContext ncref = NamingContextHelper.narrow (Objref);
// Test
System.out.println ("ok ..");
// resolve the object reference in naming
NameComponent NC = New NameComponent ("Hello", ");
Namecomponent path [] = {nc};
Hello Helloref = HelloHelper.narrow (ncref.resolve (path));
// Call The Hello Server Object and Print Results
// String Oldhello = Helloref.lastMessage ();
//System.out.println (OLDHELLO);
String hello = helloref.sayhello ();
System.out.println (Hello);
} catch (exception e) {
System.out.println ("Error:" E);
E.PrintStackTrace (System.out);
}
}
}
5. Complie these Files
Javac * .java helloApp / *.
6. Run the Application
a. first you've to run the name service prior to the others likethis
C: /> TNAMESERV
b. Run Server
C: /> Java HelloServer
C. Run Client
C: /> Java HelloClient