java - RMI实现Java

标签 java rmi

我想用 Java 实现一个简单的 RMI 应用程序。

这是我的类(class);

RMI接口(interface);

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

RMI 实现;

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" ;
    }
}

RMI服务器;

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);
    }
}

RMI客户端;

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

如何实现RMIClient,其他部分有什么问题吗?

最佳答案

试试这个

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

关于java - RMI实现Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17066827/

相关文章:

Java RMI 在同一实例中具有不同的值

java - 更改远程对象上的端点引用。是否可以?

java - 服务器中的 RMI UnmarshalException

java - RMISecurityManager 与 SecurityManager

没有 SecurityManager 的 Java RMI

java - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException : Communications link failure

java - 未找到类定义 : org/glassfish/hk2/ComponentException

java - 不使用系统属性的带 SSL 的 JMX

java - 多次使用来自扫描仪的用户输入?

java - Couchbase - 使用大写单词通过正则表达式查找文档