Basic Principles and Examples of RMI Design Patterns in Java

Source: Internet
Author: User

1. Name

RMI is the abbreviation of romote Method invocation, that is, remote method call.

2. Intent

Specifically, this mechanism can be used to call the Java object methods on the other JVM on one JVM.

3. Structure

(1) RMI interface and class

Step 1: Inherit the remote interface

Define an interface that inherits remote and name it testrmiservice. In this example, we want to call the testhello () method on the client side. Note that a RemoteException should be thrown.

Step 2: Inherit unicastremoteobject and implement testrmiservice class

Define a class that inherits unicastremoteobject and implements testrmiservice, and name it testrmiserviceimpl. Implement the testhello method.

(2) RMI server

Step 1:

Define a class named testserver, with the main method. Instantiate testrmiservice as a remote object in the main method and name it testrmi.

Step 2:

Create a registry and register with the remote object testrmi. The registration name is "RMI: // locahost: 1234/testrmi ".

(3) RMI Client

Step 1:

Define a class named testclient with the main method. Query Registration

Step 2:

Query the remote testrmiservice object whose registration name is "RMI: // locahost: 1234/testrmi". The query result is testrmi.

Step 3:

Call the remote object testrmi's method testhello.

4. Sample Code

(1) testrmiservice

package com.sinosuperman.rmi;import java.rmi.Remote;import java.rmi.RemoteException;public interface TestRMIService extends Remote {public String testHello() throws RemoteException;}

(2) testrmiserviceimpl

package com.sinosuperman.rmi;import java.rmi.RemoteException;import java.rmi.server.UnicastRemoteObject;public class TestRMIServiceImpl extends UnicastRemoteObject implements TestRMIService {private static final long serialVersionUID = -9039976499370682232L;public TestRMIServiceImpl() throws RemoteException {}@Overridepublic String testHello() throws RemoteException {return "I'm RMI, Hello World!";}}

(3) testserver

Package COM. sinosuperman. server; import java.net. malformedurlexception; import Java. RMI. alreadyboundexception; import Java. RMI. naming; import Java. RMI. remoteException; import Java. RMI. registry. locateregistry; import COM. sinosuperman. RMI. testrmiservice; import COM. sinosuperman. RMI. testrmiserviceimpl; public class testserver {public static void main (string [] ARGs) {try {/* Create a remote object. */testrmiservice testrmi = new testrmiserviceimpl ();/* create a registry object with 1234 port. */locateregistry. createregistry (1234);/* register the remote object to RMI Registry server, and name it as testrmi. */naming. binary ("RMI: // localhost: 1234/testrmi", testrmi); system. out. println ("successful Remote Object Registration");} catch (RemoteException e) {system. out. println ("remote object creation exception"); E. printstacktrace ();} catch (malformedurlexception e) {system. out. println ("url malformed"); E. printstacktrace ();} catch (alreadyboundexception e) {system. out. println ("Duplicate binding exception"); E. printstacktrace ();}}}

(4) testclient

package com.sinosuperman.rmi;import java.net.MalformedURLException;import java.rmi.Naming;import java.rmi.NotBoundException;import java.rmi.RemoteException;public class TestRMIClient {public static void main(String[] args) {try {TestRMIService testService = (TestRMIService) Naming.lookup("rmi://localhost:1234/testrmi");System.out.println(testService.testHello());} catch (MalformedURLException e) {e.printStackTrace();} catch (RemoteException e) {e.printStackTrace();} catch (NotBoundException e) {e.printStackTrace();}}}

5. Test

(1) Run testserver

In the package explorer on the eclipse main interface, in the com. sinosuperman package of the RMI project you created, right-click testserver, select Run as, and select Java application.

The page displays:

"Remote Object Registration successful"

(2) Run testclient

In the package explorer on the eclipse main interface, in the com. sinosuperman. Rmi package of the RMI project you created, right-click testclient, select Run as, and select Java application.

The page displays:

"I'm RMI, hello World !"

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.