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