JAVA: RMI 回调 -> 对象已经导出

JAVA: RMI Callback -> object already exported

这是我的第一个问题,很抱歉,如果我的英语不正确的话。

我要在java做一个分布式刽子手项目,有嘉宾,有玩家,有高手(比赛比较多)。当一个用户(已经注册)自己登录或者当一个高手打开比赛时,服务器必须通知所有客人(只有客人)这个事件。 在客户端中,我必须创建 2 个存根,一个用于服务器,另一个用于图形界面 (swing)。

线程池是用来管理匹配的,现在不用了。 这是客户端代码:

package User;

public class Utente extends RemoteServer implements UserInterface, GraphicInterface,Serializable {

private static final long serialVersionUID = 2L;
private String name;
private String password;
private String host = "localhost";
private Socket soc;
private int port = 1800;
private UserInterface stub = null;
private RegistryInterface server_interface = null;
private Registry server_registry;


public Utente(){
    super();
    this.name = "user";
    this.password = "passwd";
    //per connettersi al server
    try {
        server_registry = LocateRegistry.getRegistry(host);
        server_interface = (RegistryInterface)server_registry.lookup("HANGMAN-SERVER");
    } catch (RemoteException | NotBoundException e) {
        System.out.println("errore connessione lato client");
        e.printStackTrace();
    }
    //per l' rmi con la gui

}


public synchronized boolean login(String n, String p) throws RemoteException {
    boolean esito = false;
    export();
    esito = server_interface.login(n,p,stub);
    return esito;
}

public synchronized boolean logout() throws RemoteException {
    boolean esito = false;
    esito = server_interface.logout(name,stub);
    return esito;
}

public synchronized void notifyMatch(ArrayList<Partita> lista_partite) throws RemoteException {
    if(lista_partite!=null){
        if(lista_partite.size()==0){
            System.out.println("Ancora nessuna partita aperta.");
        }
        else{
            for(int i = 0;i<lista_partite.size();i++){
            System.out.println(lista_partite.get(i).getNameMatch());
            }
        }
    }
}

public void connect(){
    try {
        soc = new Socket(host, port);
    } catch (UnknownHostException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

}


public static void main(String[] args) {
    Utente user = new Utente();
    Registry c;
    GraphicInterface stubExport = null;
    try{
        stubExport = (GraphicInterface)UnicastRemoteObject.exportObject(user,3900);
        LocateRegistry.createRegistry(5000);
        c=LocateRegistry.getRegistry();
        c.rebind("GraphicUI", stubExport);
    }
    catch (RemoteException e) {
        System.out.println("errore nel client: " + e.toString());
    }
    MainPanel startPanel = new MainPanel();
    startPanel.setVisible(true);    
}



}

错误列表:

UI export problems
java.rmi.server.ExportException: object already exported
    at sun.rmi.transport.ObjectTable.putTarget(Unknown Source)
    at sun.rmi.transport.Transport.exportObject(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport.exportObject(Unknown Source)
    at sun.rmi.transport.tcp.TCPEndpoint.exportObject(Unknown Source)
    at sun.rmi.transport.LiveRef.exportObject(Unknown Source)
    at sun.rmi.server.UnicastServerRef.exportObject(Unknown Source)
    at java.rmi.server.UnicastRemoteObject.exportObject(Unknown Source)
    at java.rmi.server.UnicastRemoteObject.exportObject(Unknown Source)
    at User.Utente.export(Utente.java:59)
    at User.Utente.login(Utente.java:100)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
    at sun.rmi.transport.Transport.run(Unknown Source)
    at sun.rmi.transport.Transport.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run4(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda/1989325584.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at registry.Server.callbackClient(Server.java:189)
    at registry.Server.login(Server.java:201)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
    at sun.rmi.transport.Transport.run(Unknown Source)
    at sun.rmi.transport.Transport.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run4(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda/1786588690.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
    at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
    at sun.rmi.server.UnicastRef.invoke(Unknown Source)
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
    at com.sun.proxy.$Proxy0.login(Unknown Source)
    at User.Utente.login(Utente.java:101)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
    at sun.rmi.transport.Transport.run(Unknown Source)
    at sun.rmi.transport.Transport.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run4(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda/1989325584.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
    at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
    at sun.rmi.server.UnicastRef.invoke(Unknown Source)
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
    at com.sun.proxy.$Proxy1.login(Unknown Source)
    at gui.MainPanel.actionPerformed(MainPanel.java:110)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access0(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

感谢您的耐心等待,希望问题格式正确。我知道代码很丑,时间不多,我改了很多。

可能,我想我发现了您的 Utente class 中的错误。您已在代码中的 2 个位置导出对象,结果为 java.rmi.server.ExportException:object already exported.

在你的 Utente 的 main() 方法中,你有

stubExport = (GraphicInterface)UnicastRemoteObject.exportObject(user,3900);

其中 user 是 class Utente 的对象。

此外,在同一个 class' login() 方法中,您正在调用 export() 方法,其中包含:-

stub = (UserInterface) UnicastRemoteObject.exportObject(this,0);

在这里,this 将属于 Utente 当前的调用对象,在这种情况下,它是 user,它已经在之前导出了。

因此,您似乎导出对象两次,导致抛出异常。

请回复这是否有帮助。祝你好运!