java RMI helloworld

來源:互聯網
上載者:User

首先Spring中整合了RMI:

http://blog.csdn.net/partner4java/article/details/7285055

然後,我們單獨寫個helloworld,分為幾步
1、介面定義,需要實現Remote
2、實現
3、註冊開啟
4、調用

具體使用可查看jdk文檔

package com.partner4java.demo.rmi;import java.rmi.Remote;import java.rmi.RemoteException;/** * 首先定義一個介面<br/> * 要實現Remote *  * @author partner4java *  */public interface Hello extends Remote {public String sayHello(String name) throws RemoteException;}package com.partner4java.demo.rmi;import java.rmi.AlreadyBoundException;import java.rmi.RemoteException;import java.rmi.registry.LocateRegistry;import java.rmi.registry.Registry;import java.rmi.server.UnicastRemoteObject;/** * 對外提供的服務<br/> * 主要有三步:createRegistry、exportObject、bind *  * @author partner4java *  */public class HelloService implements Hello {public String sayHello(String name) throws RemoteException {System.out.println("hello" + name);return "hello" + name;}public void start() throws RemoteException, AlreadyBoundException {// 使用提供的特定連接埠匯出遠程對象,以便能夠接收傳入的調用。Hello hello = (Hello) UnicastRemoteObject.exportObject(this, 9001);// 返回本地主機在指定 port 上對遠程對象 Registry 的引用。Registry registry = LocateRegistry.createRegistry(9000);registry.bind("Hello", hello);}public static void main(String[] args) throws RemoteException,AlreadyBoundException {new HelloService().start();}}package com.partner4java.demo.rmi;import java.rmi.NotBoundException;import java.rmi.RemoteException;import java.rmi.registry.LocateRegistry;import java.rmi.registry.Registry;/** * 調用 *  * @author partner4java *  */public class HelloClient {public static void main(String[] args) throws RemoteException,NotBoundException {Registry registry = LocateRegistry.getRegistry("127.0.0.1", 9000);Hello hello = (Hello) registry.lookup("Hello");System.out.println(hello.sayHello(" world"));}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.