RMI is a set of Java APIs that support the development of distributed applications. RMI defines a remote object using the Java language interface, which aggregates the Java serialization and Java Remote Method Protocol (Java Protocol). Simply put, this allows the original program to be called on the same operating system method. It becomes a method call to a program between different operating systems. Because the Java EE is a distributed program platform. It implements the communication between the program components and the different operating systems with the RMI mechanism.
For example, an EJB can invoke an EJB remote method on the web and on a machine via RMI.
The use case server-side structure is probably so
First define the entity class to be transferred, due to the network transmission. Serialization must be implemented
User.java
Package Com.yiyuwanglu.rmi.entity;import Java.io.serializable;public class User implements Serializable {/** * */ Private static final Long Serialversionuid = -8400949180923337013l;private string id;private string name;private int age;p Ublic String getId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}
The second defines the entity interface, which must inherit Java.rmi.Remote
UserService
Package Com.yiyuwanglu.rmi.service;import Java.rmi.remote;import Java.rmi.remoteexception;import Com.yiyuwanglu.rmi.entity.user;public interface UserService extends Remote {public User Getuserbyid (String id) throws RemoteException;}
Thirdly, implement interface remember to inherit UnicastRemoteObject
Userserviceimpl
Package Com.yiyuwanglu.rmi.service.impl;import Java.rmi.remoteexception;import Java.rmi.server.unicastremoteobject;import Com.yiyuwanglu.rmi.entity.user;import Com.yiyuwanglu.rmi.service.userservice;public class Userserviceimpl extends UnicastRemoteObject implements UserService {/** * */private static final long serialversionuid = 6222175854495075991l;public Userserviceimpl () Throws RemoteException {super ();//TODO auto-generated constructor stub} @Overridepublic User Getuserbyid (String ID) { User User=new User (); User.setid (id); user.setage; User.setname ("test"); return user;}}
Server-side Startup
Program.java
Package Com.yiyuwanglu.rmi.program;import Java.net.malformedurlexception;import Java.nio.channels.alreadyboundexception;import java.rmi.Naming; Import Java.rmi.remoteexception;import java.rmi.registry.LocateRegistry; Import Com.yiyuwanglu.rmi.service.userservice;import Com.yiyuwanglu.rmi.service.impl.UserServiceImpl; public class program{public static void Main (string[] args) {//TODO auto-generated method stub t ry {userservice userservice=new userserviceimpl (); Register the communication Port Locateregistry.createregistry (6600); Register the Communication Path Naming.rebind ("Rmi://127.0.0.1:6600/userservice", UserService); System.out.println ("Service start!"); } catch (RemoteException e) {System.out.println ("Creating a remote object has an exception!"); E.printstacktrace (); } catch (Alreadyboundexception e) {System.out.println ("a recurring bound object exception occurred!
"); E.printstacktrace (); } catch (Malformedurlexception e) {System.out.println ("The URL malformed exception occurred!") "); E.printstacktrace (); } } }
Client test, the best something else have a machine (of course, the same machine perhaps, just to simulate the real situation, or there is a machine, remember IP to change) to create a client. Remember:The
path to the package for the related class is the same as the server
For example: client's UserService
Package com.yiyuwanglu.rmi.service;
Last Client call
Package Com.yiyuwanglu.rmi.program;import Java.rmi.naming;import Com.yiyuwanglu.rmi.entity.user;import Com.yiyuwanglu.rmi.service.userservice;public class Client {public static void main (string[] args) { try{ // Call the remote object, note that the RMI path and interface must be consistent with the server configuration userservice userservice= (userservice) naming.lookup ("Rmi://127.0.0.1:6600/ UserService "); User User =userservice.getuserbyid ("1245"); System.out.println (User.getname ()); } catch (Exception ex) { ex.printstacktrace ();}} }
Start the server-side program and start the Clientclient
The results of the display are naturally
Service start!
Client
Test
Java remote invoke RMI Getting started instance