同一程序中的RMI多级

时间:2011-12-01 09:34:31

标签: java rmi

我想从服务器连续调用Java RMI到其他服务器?

RMI客户端1> - (1)---> RMI服务器1> ---(2)-----> RMI Server 2

RMI客户端1上的方法将调用RMI服务器1上的方法,并且RMI服务器1将在相同执行程序中的RMI服务器2的其他RMI服务器2上调用方法

异常: java.lang.ClassCastException:rmiserver1_Stub无法强制转换为interfc2

请帮助..

代码在这里:

rmiserver1.java

import java.rmi.*;
import java.rmi.server.*;

import java.io.*;
import java.util.*;


class rmiserver1 extends UnicastRemoteObject implements interfc1
{
public rmiserver1() throws RemoteException
{
    System.out.println("RMIServer 1 Constructor ");
}


public String remote1() 
{
    System.out.println("here Calling RMIServer2 method remote2 ");
                try
                {
//here is Exception
                    interfc2 obj2=(interfc2) Naming.lookup("rmi://localhost/rmiserver2");
                    String r2=obj2.remote2();
                    System.out.println("Result from rmiserver2 :"+r2);
                }
                catch(Exception e){e.printStackTrace();}

    return "RMIServer1 remote 1 method return here....";

}


public static void main(String[] args) 
{
    System.out.println("RMIServer 1 Main method ");

    try
            {
                rmiserver1 p1=new rmiserver1();
                    Naming.rebind("rmiserver1",p1);

            System.out.println("RMIServer 1 rebinded ");

            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

}
 }

interfc1.java

 import java.rmi.*;
 import java.io.*;
 import java.util.*;

public interface interfc1 extends Remote
{
public String remote1() throws RemoteException;
}

interfc2.java

import java.rmi.*;
import java.io.*;
import java.util.*;

public interface interfc2 extends Remote
{
public String remote2() throws RemoteException;
}

rmiserver2.java

import java.rmi.*;
import java.rmi.server.*;

import java.io.*;
import java.util.*;


class rmiserver2 extends UnicastRemoteObject implements interfc2
{
public rmiserver2() throws RemoteException
{
    System.out.println("RMIServer 2 Constructor ");
}


public String remote2() 
{
    return "RMIServer2 remote 2 method return here....";
}


public static void main(String[] args) 
{
    System.out.println("RMIServer 2 Main method ");

    try
            {
                rmiserver1 p1=new rmiserver1();
                    Naming.rebind("rmiserver2",p1);

            System.out.println("RMIServer 2 rebinded ");

            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

}
}

rmiclient1.java

import java.rmi.*;
import java.rmi.server.*;

import java.io.*;
import java.util.*;



class rmiclient1  
{
public static void main(String[] args) 
{
    System.out.println("RMIClient 1 Main method");

    try
    {
        interfc1 obj1=(interfc1) Naming.lookup("rmi://localhost/rmiserver1");
        String r1=obj1.remote1();
        System.out.println("Result from rmiserver1 :"+r1);
    }
    catch(Exception e){e.printStackTrace();}
}
}

有关rmiserver1提示的例外情况:

  java.lang.ClassCastException: rmiserver1_Stub cannot be cast to interfc2
    at rmiserver1.remote1(rmiserver1.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
 sorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)
    at sun.rmi.transport.Transport$1.run(Transport.java:159)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:5
 35)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTranspor
 t.java:790)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
 .java:649)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExec
 utor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:908)
    at java.lang.Thread.run(Thread.java:619)

请帮助我.....

1 个答案:

答案 0 :(得分:1)

您的问题出在班级rmiserver2的主要方法中,您绑定rmiserver1,名称为“rmiserver2”。

rmiserver2

主要方法中的当前代码
rmiserver1 p1=new rmiserver1();
                Naming.rebind("rmiserver2",p1);

你想要的是:

rmiserver2 p2=new rmiserver2();
                Naming.rebind("rmiserver2",p1);

看起来像复制粘贴问题:)