2013-06-12 241 views
0

我想用Java實現一個簡單的RMI應用程序。RMI實現Java

這是我的課程;

RMIInterface;

public interface RMIInterface extends Remote 
{ 
    String translate (String wordInTurkish) throws RemoteException ; 
} 

RMIImplementation;

public class RMIImplementation implements RMIInterface 
{ 
    @Override 
    public String translate (String wordInTurkish) 
    { 
     if (wordInTurkish.equalsIgnoreCase("Merhaba")) { return "Hello" ; } 
     if (wordInTurkish.equalsIgnoreCase("..." )) { return "..." ; } 

     return "Not found in the dictionary" ; 
    } 
} 

RMIServer;

public class RMIServer 
{ 
    public static void main (String args[]) throws Exception 
    { 
     String codebase = "http://localhost:8080/rmi/" ; 
     String name  = "RMIInterface"    ; 
     System.setProperty("java.rmi.server.codebase" , codebase) ; 
     RMIImplementation obj = new RMIImplementation(); 
     RMIInterface  stub = (RMIInterface) UnicastRemoteObject.exportObject(obj , 0); 
     LocateRegistry.createRegistry(2020).bind(name, stub); 
    } 
} 

RMIClient;

public class RMIClient 
{ 
    public static void main (String args[]) throws Exception 
    { 
     String host = "localhost" ; 
     String name = "RMIInterface" ;  
    } 
} 

我該如何實現RMIClient,並且其他部分有什麼問題?

+0

你可以看看這裏:http://docs.oracle.com/javase/tutorial/rmi/ – Reda

+0

有你看着RMI教程樣本,其與JDK一起? –

+0

(與RMI無關,但正如你可能知道土耳其語言組有問題(特別是字母'i')。) –

回答

1

試試這個

RMIInterface cl = (RMIInterface) new InitialContext().lookup("rmi://localhost:2020/RMIInterface"); 
    String res = cl.translate("xxx"); 
+0

非常感謝。它工作正常。 – xxlali

1
RMIInterface remote = (RMIInterface) 
LocateRegistry.getRegistry("localhost",8080).lookup("RMIInterface"); 
remote.translate("merhaba"); 
0
serverAddress=127.0.0.1 
rmiRegistiry=rmi://127.0.0.1/DanBankServer 

Registry locateServerRegistery = LocateRegistry.getRegistry(serverAddress); 
System.out.println("Server Registery is been looked up for address: "+rmiRegistiry); 
SekelatonInterface danBankServer = (SekelatonInterface) locateServerRegistery.lookup(serverRmiUrl);