Java RMI 的簡單樣本__Java

來源:互聯網
上載者:User

1. 遠程傳輸對象:

package com.huey.dream.bean;import java.io.Serializable;/** * 遠程傳輸對象,須實現java.io.Serializable介面 * @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;}@Overridepublic String toString() {return "Book [title=" + title + ", author=" + author + "]";}}


2. 遠程對象調用介面:

package com.huey.dream.serv;import java.rmi.Remote;import java.rmi.RemoteException;import java.util.List;import com.huey.dream.bean.Book;/** * 遠程對象調用介面,須實現java.rmi.Remote介面 * @author  huey * @version 1.0  * @created 2014-11-18 */public interface BookServ extends Remote {/** * 遠程對象調用介面的方法,須拋出java.rmi.RemoteException異常 * @param author * @return * @throws RemoteException */public List<Book> getBooksByAuthor(String author) throws RemoteException;}

3. 遠程對象調用介面實現:

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;/** * 遠程調用對象實作類別,須繼承java.rmi.server.UnicastRemoteObject類 * @author  huey * @version 1.0  * @created 2014-11-18 */public class BookServImpl extends UnicastRemoteObject implements BookServ {/** *  */private static final long serialVersionUID = -3185430753116015923L;private List<Book> allBooks;public BookServImpl() throws RemoteException {allBooks = new ArrayList<Book>();allBooks.add(new Book("白夜行", "東野圭吾"));allBooks.add(new Book("嫌疑人X的獻身", "東野圭吾"));allBooks.add(new Book("百年孤獨", "加西亞·馬爾克斯"));}@Overridepublic 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 books;}}

4. 服務端:

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服務端 * @author  huey * @version 1.0  * @created 2014-11-18 */public class RmiServer {public static void main(String[] args) {try {BookServ bookServ = new BookServImpl();// 註冊連接埠LocateRegistry.createRegistry(8098);// 註冊路徑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. 用戶端:

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用戶端 * @author  huey * @version 1.0  * @created 2014-11-18 */public class RmiClient {public static void main(String[] args) {try {// 擷取遠程對象BookServ bookServ = (BookServ) Naming.lookup("rmi://127.0.0.1:8098/bookserv");List<Book> books = bookServ.getBooksByAuthor("東野圭吾");for (Book book : books) {System.out.println(book);}} catch (MalformedURLException e) {e.printStackTrace();} catch (RemoteException e) {e.printStackTrace();} catch (NotBoundException e) {e.printStackTrace();}}}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.