Java RMI Demo-- Java RMI 樣本__Java

來源:互聯網
上載者:User

Java RMI Demo-- Java RMI 樣本
本文給出了一個Java RMI的樣本,其中註冊過程是使用Registry

本例子包含了共5個檔案,分別是HelloClient,Hello,HelloServer,Configur,config.properti

/*Hello.java*/
/*
 * Created on 2004-11-10
 *
 */
package test.rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;

/**
 * @author dogcome
 *
 * <p>遠程方法調用介面,定義遠程調用方法</p>
 */
public interface Hello extends Remote {
   
    String sayHello() throws RemoteException;

}

 


/*HelloServer*/
/*
 * Created on 2004-11-10
 *
 */
package test.rmi;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

/**
 * @author dogcome
 *
 *<p>伺服器端,實現了Hello介面中的方法,用於實現遠程調用方法的具體商務邏輯</p>
 */
public class HelloServer extends UnicastRemoteObject implements Hello {
    String name;
    public HelloServer(String s) throws RemoteException {
        super();
        name=s;
    }

    public String sayHello() throws RemoteException {
        // TODO Auto-generated method stub
        return "Hello world!";
    }

    public static void main(String[] args) {
        /**下面這句話若要加上,則需要進行許可權的認證,即增加.policy檔案
         * 並且在命令列中使用如下格式
         * java -Djava.security.policy=java.policy test.rmi.HelloServer
        */
        /*System.setSecurityManager(new RMISecurityManager());*/
        Registry registry = null;
        try {
            /**啟動註冊伺服器,使用了這個語句就不再需要在命令列環境中
             *啟動registry服務了
            */
            registry = LocateRegistry.getRegistry();
            /* 若沒有獲得串連,則此句會拋出異常,後面在捕獲後進行相關處理 */
            registry.list();
            System.out.println("Register the exist server!"); //$NON-NLS-1$
        } catch (RemoteException re) {
            try {
                int port = Integer.parseInt(Configur
                        .getString("HelloServer.RegistryServerPort")); //$NON-NLS-1$
                registry = LocateRegistry.createRegistry(port);
                System.out.println("Create Registry Server!"); //$NON-NLS-1$
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            HelloServer helloServer = new HelloServer("Hello");
            registry
                    .rebind(
                            Configur.getString("HelloServer.HelloServerName"), helloServer); //$NON-NLS-1$
            System.out.println("HelloServer server start!"); //$NON-NLS-1$
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 


/*HelloClient*/
/*
 * Created on 2004-11-10
 *
 */
package test.rmi;

import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

/**
 * @author dogcome
 *
 * <p>用戶端,需要註冊伺服器並使用jndi</p>
 */
public class HelloClient {

    public static void main(String[] args) {
        /**下面這句話若要加上,則需要進行許可權的認證,即增加.policy檔案
         * 並且在命令列中使用如下格式
         * java -Djava.security.policy=java.policy test.rmi.HelloServer
        */
        /*System.setSecurityManager(new RMISecurityManager());*/
        try {
            /*註冊伺服器*/
            String hostName=Configur.getString("HelloServer.RegistryServerName");
            int port=Integer.parseInt(Configur.getString("HelloServer.RegistryServerPort"));
            Registry registry=LocateRegistry.getRegistry(hostName,port);

            Hello hello=(Hello)registry.lookup(Configur.getString("HelloServer.HelloServerName"));
            String message=hello.sayHello();
            System.out.println(message);
           
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}

 


/*
 * Created on 2004-11-12
 *
 */
package test.rmi;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * @author dogcome
 *
 * <p>擷取系統運行所需要的配置資訊,對應的設定檔名稱為config.properties</p>
 */
public class Configur {
    private static final String BUNDLE_NAME = "test.rmi.config";

    private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
            .getBundle(BUNDLE_NAME);

    private Configur() {
    }
   
    /**
     * <p>通過key名稱獲得設定檔的相關資訊</p>
     * @param key key名稱
     * @return String 設定檔資訊
     */
    public static String getString(String key) {
        try {
            return RESOURCE_BUNDLE.getString(key);
        } catch (MissingResourceException e) {
            return '!' + key + '!';
        }
    }
}

 


#config.properties
HelloServer.RegistryServerPort=1111
HelloServer.HelloServerName=HelloServer
HelloServer.RegistryServerName=127.0.0.1

首先使用rmic test.rmi.HelloServer命令產生需要的stub及skel兩個類檔案
然後運行HelloServer伺服器 java test.rmi.HelloServer
最後運行HelloClient用戶端 java test.rmi.HelloClient
螢幕輸出HelloWorld,這樣,一個最簡單的rmi遠程調用成功了


 

聯繫我們

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