Remote invocation of RMI technology

Source: Internet
Author: User

---restore content starts---

RMI is not a new technology, but compared to WebService, RMI is relatively simple, relatively suitable for some small applications, the following Helloword can introduce RMI related technology

Server-side code:

Server Interface for remote

1  PackageCom.qcf.server;2 3 ImportJava.rmi.Remote;4 Importjava.rmi.RemoteException;5 /**6 * Service class7 * Define the behavior set8  * @authorAdministrator9  *Ten  */ One  Public InterfaceIhelloextendsRemote { A  -     //Print the string that the client passed in on the server side and return the string -      PublicString SayHello (String str)throwsremoteexception; the}

Implementation class of the interface

1  PackageCom.qcf.server;2 3 Importjava.rmi.RemoteException;4 ImportJava.rmi.server.UnicastRemoteObject;5 6 /**7 * Server-side implementation class8 * need to inherit UnicastRemoteObject9  * @authorAdministratorTen  * One  */ A  Public classIhelloimplextendsUnicastRemoteObjectImplementsihello{ -  -     //necessary for the     protectedIhelloimpl ()throwsRemoteException { -         Super(); -     } -  + @Override -      PublicString SayHello (String str)throwsRemoteException { +System.out.println ("The string that the client passes over is:" +str); A         returnstr; at     } -  -}

Service class Start service class

1  PackageCom.qcf.server;2 3 Importjava.net.MalformedURLException;4 Importjava.rmi.AlreadyBoundException;5 Importjava.rmi.Naming;6 Importjava.rmi.RemoteException;7 ImportJava.rmi.registry.LocateRegistry;8 9 /**Ten * RMI Server One * 1. Create a service A * 2, start the server - * 3. Registration Service -  * @authorAdministrator the  * -  */ -  Public classHelloServer { -  +      Public Static voidMain (string[] args)throwsremoteexception, Malformedurlexception, alreadyboundexception { -         //1. Create a service +Ihello ihello=NewIhelloimpl (); A         //2. Start the server to start a registry and bind the registry to a port (default port 1099) atLocateregistry.createregistry (8189); -          -         //3 The standard format for registering a service to a registry binding is: RMI://Host:port/name (where the protocol name can be omitted, the following two formulations are correct) -Naming.bind ("Rmi://localhost:8189/ihello", Ihello); -SYSTEM.OUT.PRINTLN ("Server Start success!"); -     } in}

Client class

Create an interface class as above

Client Test class

1  Packagecom.qcf.client;2 3 Importjava.net.MalformedURLException;4 Importjava.rmi.Naming;5 Importjava.rmi.NotBoundException;6 Importjava.rmi.RemoteException;7 8 /**9 * Client Test classTen * Server is who server One * How to get service lookup A * Call Interface -  * @authorAdministrator -  * the  */ -  Public classhelloclient { -      Public Static voidMain (string[] args)throwsException, Exception, Exception { -         //Find a service +Ihello hello= (Ihello) naming.lookup ("Rmi://localhost:8489/ihello"); -         //Calling Interface +String Str=hello.sayhello ("hahaha. I've done my test. "); ASYSTEM.OUT.PRINTLN ("Client" +str); at     } -}
Run RMI Server program: Run RMI client program: summary: From the above process, RMI on the server IP address and port dependence is very close, but in the development of the time do not know the future server IP and port, but the client program depends on this IP and port. This is one of the limitations of RMI. There are two ways to solve this problem: one is through DNS, and the other is to expose IP to program code through encapsulation. The second limitation of RMI is that RMI is a remote invocation of the Java language, the programming language must be Java implementation, the communication between different languages can be considered by the Web service or the Common Object Request Agent system (CORBA) to achieve.

---restore content ends---

Remote invocation of RMI technology

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.