Java remote invoke RMI Getting started instance

Source: Internet
Author: User

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 makes the original program in the same operating system of the method call, into a different operating system method call between the program, because the Java EE is a distributed program platform, it is the RMI mechanism to implement the program components in the communication between different operating systems. For example, an EJB can invoke an EJB remote method on another machine on the web through RMI.

Use case server-side structure probably so

First, define the entity classes to be transferred, because for network transmission, you must implement serialization

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 duplicate bound object exception occurred!            ");        E.printstacktrace (); } catch (MalformedurlexCeption e) {System.out.println ("There is a malformed URL anomaly!            ");        E.printstacktrace ();   }     }  }

Client testing, it is best to find another machine (of course, the same machine may, but in order to simulate the real situation, or another 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 client
The results are shown separately

Service start!

Client

Test



Java remote invoke RMI Getting started instance

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.