Java RMI Quick Start instance

Source: Internet
Author: User
This example is written by referring to multiple articles. Various articles on the Internet and books show you how to use RMI. There are multiple examples for reference, such:

1. Run the rmiregistry command to start the RMI Registration Service
2. Create both stub and skeleton
3. Create only the stub class (JDK and later)
4. Using remoteref and RMI: // protocol string
5. locateregistry is rarely used to directly start the RMI registration service on the code.

The preceding description is not clearly classified. For example, you can always use rmiregistry or the Code locateregistry to start the RMI registration service.

Next I will introduce a complete example, so that beginners can quickly experience the functions of RMI.

Perform the following four steps:

1. Create a remote interface and declare a remote method (hellointerface. Java)
2. implement remote interfaces and remote methods (inherit unicastremoteobject) (hello. Java)
3. Start RMI Registration Service and register remote objects (helloserver. Java)
4. The client searches for remote objects and calls the remote method (helloclient)
5. Execute the program: start the service helloserver; run the client helloclient to call

The code and corresponding steps are as follows:

1. Create a remote interface and declare a remote method (hellointerface. Java)

Package

2. implement remote interfaces and remote methods (inherit unicastremoteobject) (hello. Java)

Package

3. Start RMI Registration Service and register remote objects (helloserver. Java)

PackageCom. unmi;

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

Public ClassHelloserver
{
/**
* Start RMI Registration Service and register objects
*/
Public Static VoidMain (string [] argv)
{
Try
{
// Start the RMI registration service and set the port to 1099 (1099 as the default port)
// You can also run the $ java_home/bin/rmiregistry 1099 command to start
// This method is used to avoid opening another DOS window.
// You must use the rmiregistry command to start the registration service. You must use rmic to generate a stub class for this purpose.
Locateregistry. createregistry (1099 );

// Create one or more instances of the remote object. The following is the hello object.
// You can register different instances with different names.
Hellointerface Hello =NewHello ("Hello, world! ");

// Register hello on the RMI registration server and name it Hello
Naming. rebind ("hello", hello );

// If you want to register the hello instance to another machine that has started the RMI registration service
// Naming. rebind ("// 192.168.1.105: 1099/Hello", hello );

System. Out. println ("Hello server is ready .");
}
Catch(Exception E)
{
System. Out. println ("Hello server failed:" + E );
}
}
}

4. The client searches for remote objects and calls the remote method (helloclient)

Package

5. Execute the program: start the service helloserver; run the client helloclient to call

I will not elaborate on how to compile the code here.

(1) Open a DOS window and execute the command Java com. unmi. helloserver to start the service helloserver.

E:/workspace/testrmi/bin> JAVA com. unmi. helloserver
Hello server is ready.

After running successfully, you can see hello server is ready.

(2) open another DOS window and execute the command Java com. unmi. helloclient to run the client program

E:/workspace/testrmi/bin> JAVA com. unmi. helloclient
Hello, world!

If the call is successful, you can see hello, world!

In addition, the hello server is ready.
Called by helloclient

Congratulations! You have had a brisk RMI journey.

The last note is as follows:

In this example, the JDK command rmic compilation implementation class is not used to get the stub class, nor the rmiregistry command is used to start the RMI registration service. Before starting rmiregistry, you must load it to the corresponding stub class. This is why ** _ stub class cannot be found.

If you just follow the above Code, the service program helloserver and client program helloclient must both run on the local machine (so what is the significance of RMI ?); Don't worry, just modify the helloclient class, use the second form of lookup query statement, comment the first lookup statement, and cancel the comment of the second lookup statement

// Hellointerface Hello = (hellointerface) Naming. Lookup ("hello ");

// If you want to find the hello instance from another machine that has started the RMI Registration Service
Hellointerface Hello = (hellointerface) Naming. Lookup ("// 192.168.1.105: 1099/Hello ");

The IP address and port number 1099 are the IP address and port number of the RMI registration server, so that your helloclient can run on another machine, of course, the hellointerface class must be available (but you can also specify the parameter-djava. RMI. server. codebase loads the hellointerface class from the network ). By default, Lookup ("hello") searches for the hello command object from Port 127.0.0.1 of the Local Machine. If the second statement is written as Lookup ("192.168.1.105/Hello"), it is equivalent to the original statement, because the default port number is 1099.

In the code, helloserver and helloclient omit the process of setting the security manager system. setsecuritymanager (New rmisecuritymanager (); If you set security management, you must write the corresponding access policy file and specify parameters during execution.

You can use the djava parameter to start the server or the client. RMI. server. codebase = http://unmi.blogcn.cn/bin form, load classes from the network like jnp, which is more convenient for RMI client deployment, such as RMI client is an applet

You can run rmiregistry on a single machine (it must be able to load to the corresponding stub class and set classpath) or use locateregistry. createregistry (port) is only a server registered in the RMI cluster of the RMI remote object. It provides service objects for registration only. The client only needs to search for remote object references from the registration server, then the remote method is called, and the registration server is responsible for contacting the user who provides the service.

You can also use RMI activation programming method to achieve RMI remote method call, please refer to the specific http://java.sun.com/j2se/1.4.2/docs/guide/rmi/activation.html

In helloserver and helloclient"// 192.168.1.105: 1099/HelloWriteRMI:/192.168.1.105: 1099/HelloIt looks better, because it directly feels that it is processing the RMI protocol.

References:
1. Java RMI tutorial
2. Getting started using RMI
3. Get started with javarmi
4. Use RMI and CORBA for Distributed Java programming
5. Network Programming RMI in think in Java

Com. unmi;ImportJava. RMI. Naming;Public ClassHelloclient {/*** Search for remote objects and call remote methods*/ Public Static VoidMain (string [] argv ){Try{Hellointerface Hello = (hellointerface) Naming. Lookup ("hello ");// If you want to find the hello instance from another machine that has started the RMI Registration Service // Hellointerface Hello = (hellointerface) Naming. Lookup ("// 192.168.1.105: 1099/Hello "); // Call the Remote MethodSystem. Out. println (hello. Say ());}Catch(Exception e) {system. Out. println ("helloclient exception:" + E );}}}

Com. unmi;
ImportJava. RMI .*;
ImportJava. RMI. server .*;
/**
* Extends the unicastremoteobject class and implements the remote interface hellointerface
*/
Public ClassHelloExtendsUnicastremoteobjectImplementsHellointerface
{
PrivateString message;
/**
* The constructor must be defined. Even the default constructor must be explicitly written because it must throw a RemoteException.
*/
PublicHello (string MSG)ThrowsRemoteException
{
Message = MSG;
}
/**
* Remote interface method implementation
*/
PublicString say ()ThrowsRemoteException
{
System. Out. println ("called by helloclient ");
ReturnMessage;
}
}

Com. unmi;
ImportJava. RMI .*;
/**
* The remote interface must be extended to Java. RMI. Remote.
*/
Public InterfaceHellointerfaceExtendsRemote
{
/**
* The remote interface method must throw java. RMI. RemoteException.
*/
PublicString say ()ThrowsRemoteException;
}

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.