1. Remote Transfer objects:
Package Com.huey.dream.bean;
Import java.io.Serializable;
/**
* Remote Transfer object, must implement Java.io.Serializable interface
* @author Huey
* @version 1.0 *
@created 2014-11-18
* /Public
class book implements Serializable {
/**
* * *
private static final long Serialversionuid = 8807174227849297542L;
Private String title;
Private String author;
Public String GetTitle () {return
title;
}
public void Settitle (String title) {
this.title = title;
}
Public String Getauthor () {return
author;
}
public void Setauthor (String author) {
this.author = author;
}
Public book () {
super ();
}
Public Book (string title, string author) {
super ();
this.title = title;
This.author = author;
}
@Override public
String toString () {return
"book [title=" + title + ", author=" + author + "]";
}
}
2. Remote object Invoke interface:
Package com.huey.dream.serv;
Import Java.rmi.Remote;
Import java.rmi.RemoteException;
Import java.util.List;
Import Com.huey.dream.bean.Book;
/**
* Remote Object invoke interface, must implement Java.rmi.Remote interface
* @author Huey
* @version 1.0 *
@created 2014-11-18 *
* Public
Interface Bookserv extends remotely {
/**
* Remote Object calls the interface method, must throw Java.rmi.RemoteException exception
* @ param author
* @return
* @throws remoteexception
/public list<book> GetBooksByAuthor ( String author) throws remoteexception;
}
3. Remote object invocation Interface implementation:
Package Com.huey.dream.serv.impl;
Import java.rmi.RemoteException;
Import Java.rmi.server.UnicastRemoteObject;
Import java.util.ArrayList;
Import java.util.List;
Import Com.huey.dream.bean.Book;
Import Com.huey.dream.serv.BookServ; /** * Remote Invoke Object implementation class, must inherit Java.rmi.server.UnicastRemoteObject class * @author Huey * @version 1.0 * @created 2014-11-18 * * Publ IC class Bookservimpl extends UnicastRemoteObject implements Bookserv {/** * */private static final long serial
Versionuid = -3185430753116015923l;
Private list<book> allbooks;
Public Bookservimpl () throws remoteexception {allbooks = new arraylist<book> ();
Allbooks.add (new book ("Daytime Line", "East Wild GUI WU"));
Allbooks.add (new book ("Suspect X's Devotion", "East Wild GUI WU"));
Allbooks.add (new book ("Hundred Years Lonely", "Marquez")); @Override public list<book> GetBooksByAuthor (String author) throws RemoteException {list<book> books =
New Arraylist<book> (); for (book book:allbooks) {if (Author.equals (Book.getauthor ())) {
Books.add (book);
} return to books; }
}
4. Service side:
Package com.huey.dream.main;
Import java.net.MalformedURLException;
Import java.rmi.Naming;
Import java.rmi.RemoteException;
Import Java.rmi.registry.LocateRegistry;
Import Com.huey.dream.serv.BookServ;
Import Com.huey.dream.serv.impl.BookServImpl;
/** *
RMI Server *
@author Huey
* @version 1.0
* @created 2014-11-18
/public class Rmiserver {public
static void Main (string[] args) {
try {
Bookserv bookserv = new Bookservimpl ();
Register Port
Locateregistry.createregistry (8098);
Registration path
naming.rebind ("Rmi://127.0.0.1:8098/bookserv", Bookserv);
System.out.println ("Service start!");
} catch (RemoteException e) {
e.printstacktrace ();
} catch (Malformedurlexception e) {
e.printstacktrace ();
}
}
}
5. Client:
Package com.huey.dream.main;
Import java.net.MalformedURLException;
Import java.rmi.Naming;
Import java.rmi.NotBoundException;
Import java.rmi.RemoteException;
Import java.util.List;
Import Com.huey.dream.bean.Book;
Import Com.huey.dream.serv.BookServ;
/** *
RMI Client
* @author Huey
* @version 1.0
* @created 2014-11-18
/public class rmiclient {public
static void Main (string[] args) {
try {
//Get remote object
bookserv Bookserv = (bookserv) Na Ming.lookup ("Rmi://127.0.0.1:8098/bookserv");
list<book> books = Bookserv.getbooksbyauthor ("The East Wild GUI Wu");
for [book Book:books] {
System.out.println (book);
}
catch (Malformedurlexception e) {
E.printstacktrace ();
} catch (RemoteException e) {
e.printstacktrace ();
} catch (Notboundexception e) {
e.printstacktrace (); c29/>}}}