How to publish and reference services using RMI

Source: Internet
Author: User

RMI is a native distributed service mechanism of Java. It supports Java distributed access to Java and uses Java serialization protocol for Codec operations. Here we will briefly describe how RMI publishes services and how clients reference services.


RMI supports two methods to publish a service. One is the release protocol of RMI, and the other is to publish the service by using the common JNDI method.

Using the publishing protocol of jmi, you can use the registry interface or the naming tool class.

When using the registry interface, you only need to write the service name for the bind Service name. Rmi changes the service name to RMI: // ip: Port/servicename.

Public class server {public static void main (string [] ARGs) {try {demoservice service = new demoserviceimpl ("iter_zc"); // specify the port registry = locateregistry. createregistry (8888); // register the registry. BIND ("demoservice", service);} catch (exception e) {e. printstacktrace ();} system. out. println ("demoservice is running at server ");}}


When using the naming tool class, you must specify the service name for the complete URI method. It is worth noting that you also need to bind the port number first, otherwise the connectionrefuse exception will be reported.

Public class server {public static void main (string [] ARGs) {try {demoservice service = new demoserviceimpl ("iter_zc"); // specify the port locateregistry. createregistry (8888); // name of the Service in the complete URI method naming. BIND ("RMI: // 10.2.43.50: 8888/demoservice", service);} catch (exception e) {e. printstacktrace ();} system. out. println ("demoservice is running at server ");}}

To publish the RMI service in the JNDI mode, you must specify the service name in the complete URI mode.

public class Server {public static void main(String[] args){try {DemoService service = new DemoServiceImpl("ITer_ZC");Registry registry = LocateRegistry.createRegistry(8888); Context nameContext = new InitialContext();nameContext.rebind("rmi://10.2.43.50:8888/demoservice", service);} catch (Exception e) {e.printStackTrace();}System.out.println("DemoService is running at Server");}}


When the client references the RMI service, you can also search for the service in two ways: RMI class and JNDI interface class.

Use the RMI class to reference the RMI Service

public class Client {public static void main(String[] args){String url = "rmi://10.2.43.50:8888/demoservice";try {DemoService service = (DemoService)Naming.lookup(url);System.out.println(service.sayHi());} catch (Exception e) {e.printStackTrace();}}}


Use the JNDI interface to reference the RMI Service

public class Client {public static void main(String[] args){String url = "rmi://10.2.43.50:8888/demoservice";Context nameContext;try {nameContext = new InitialContext();DemoService service = (DemoService)nameContext.lookup(url);System.out.println(service.sayHi());} catch (Exception e) {e.printStackTrace();}}}


Reprinted please indicate Source: http://blog.csdn.net/iter_zc


How to publish and reference services using RMI

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.