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

问题描述 投票:0回答:1

这是我的第一个问题,很抱歉,如果我说错了,我的英语。

我必须在java中做一个分布式挂起项目,有客人,玩家和主人(更多匹配)。当用户(已注册)自己登录或主人打开比赛时,服务器必须通知所有客人(仅限客人)该事件。在客户端我必须创建2个存根,1为服务器,另一个为图形界面(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$1.run(Unknown Source)
    at sun.rmi.transport.Transport$1.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$run$254(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda$1/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$1.run(Unknown Source)
    at sun.rmi.transport.Transport$1.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$run$254(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda$1/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$1.run(Unknown Source)
    at sun.rmi.transport.Transport$1.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$run$254(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda$1/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$2.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.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.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)

感谢您的耐心等待,我希望这个问题能够很好地形成。代码是丑陋我知道,没有太多时间,我改变了很多。

java callback server rmi stub
1个回答
0
投票

可能,我想我在你的Utente课程中发现了错误。您已在代码中的2个位置导出对象,从而生成java.rmi.server.ExportException:object already exported.

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

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

其中user是Utente类的对象。

此外,在同一个类的login()方法中,您调用的是export()方法,其中包含: -

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

在这里,this将与Utente目前的调用对象有关,在这种情况下将是user,它已经在早些时候出口了。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.