A simple example of Java RMI __java

Source: Internet
Author: User

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/>}}}
	



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.