簡體   English   中英

java.rmi.NoSuchObjectException但添加了靜態引用

[英]java.rmi.NoSuchObjectException but static reference added

我正在使用Java RMI實現客戶端/服務器應用程序,客戶端和服務器位於同一項目中的不同程序包中(當然,它們在同一台計算機上運行),並且接口在“通用”程序包中定義。得到兩個遠程對象之一,我得到了上述例外。 我讀過一些SO答案,人們建議將靜態引用添加到遠程對象,這樣就永遠無法收集它們,但這是行不通的!

這是客戶端和服務器的代碼

NotificheServiceProxy

public class NotificheServiceProxy implements INotificheService
{

    @Override
    public ArrayList <NotificaDTO> generaNotifiche(String autostrada, Date data, Date oraInizio, Date oraFine)
    {
        try 
        {
            RMIClient client = new RMIClient ("localhost" , 1099);
            INotificheService service = (INotificheService) client.getService(INotificheService.class);
            return service.generaNotifiche(autostrada, data, oraInizio, oraFine);
        }
        catch (RemoteException ex)
        {
            Logger.getLogger(TrattaServiceProxy.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
        catch (NotBoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

RMIClient

    public class RMIClient
    {
        private Registry reg;

     public RMIClient  (String url , int port) throws RemoteException
    {
        reg = LocateRegistry.getRegistry(url, port);
    }


    public Remote getService(Class service) throws NotBoundException, RemoteException
    {
        RMIMapping RMIMappingAnn = (RMIMapping) service.getAnnotation(RMIMapping.class);

        return reg.lookup(RMIMappingAnn.name());
    }

}

NotificheService(服務器端)

public class NotificheService implements INotificheService 
{
    private static NotificheService instance;


    @Override
    public ArrayList<NotificaDTO> generaNotifiche(String autostrada, Date data, Date oraInizio, Date oraFine) throws RemoteException
    {
        instance = this;
        return new NotificheBusinessLogic().generaNotifiche(autostrada, data, oraInizio, oraFine);
    }
}

scServerConfig(服務器主)

public class SCServerConfig
{
    public static RMIServer server;
    public static void main (String[] args)
    {
         server = new RMIServer(1099);

        //avvia il server ed esporta oggetti remoti
        server.start().export(TrattaService.class).export(NotificheService.class);

        //Other stuff


RMIServer (starts server and provide method for remote objects exporting

public class RMIServer
{

    private Registry reg;
    private int regPort;



    public RMIServer (int regPort)
    {
        this.regPort = regPort;
    }

    public RMIServer start()
    {
        try
        {
            reg = LocateRegistry.createRegistry(regPort);
            System.out.println("SERVER OK");

        }
        catch (RemoteException ex)
        {
            Logger.getLogger(RMIServer.class.getName()).log(Level.SEVERE, null, ex);
        }

        return this;
    }

    public RMIServer export (Class object)
    {
        try
        {

            RMIMapping RMIMappingAnn = (RMIMapping) object.getInterfaces()[0].getAnnotation(RMIMapping.class);
            Remote tmp = UnicastRemoteObject.exportObject((Remote)object.newInstance(), 0);

            reg.rebind( RMIMappingAnn.name() , tmp );

            System.out.println("oggetto remoto esportato " + object);

        } 
        catch (InstantiationException ex)
        {
            System.err.println("InstantiationExeception!!\ncause:\n 1. il costruttore dell' oggetto remoto non deve avere argomenti");
        }
        catch (IllegalAccessException ex)
        {
            Logger.getLogger(RMIServer.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (RemoteException ex)
        {
            Logger.getLogger(RMIServer.class.getName()).log(Level.SEVERE, null, ex);
        }


        return this;
    }

 }

每個接口都有@RMIMapping注釋,該注釋提供了對象綁定名稱。 但我確定問題不會因此而來

對我來說,這真是個難題,這是stacktrace……非常感謝您的幫助!

ott 03, 2014 5:13:17 PM it.csbeng.speedcontrolsystem.addettoApp.proxy.NotificheServiceProxy generaNotifiche
SEVERE: null
java.rmi.NoSuchObjectException: no such object in table
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273)
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:160)
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
    at $Proxy3.generaNotifiche(Unknown Source)
    at it.csbeng.speedcontrolsystem.addettoApp.proxy.NotificheServiceProxy.generaNotifiche(NotificheServiceProxy.java:25)
    at it.csbeng.speedcontrolsystem.addettoApp.coordinator.AddettoAppCoordinator.generaNotifiche(AddettoAppCoordinator.java:17)
    at it.csbeng.speedcontrolsystem.addettoApp.boundary.AddettoInteraction.generaNotificheEvent(AddettoInteraction.java:179)
    at it.csbeng.speedcontrolsystem.addettoApp.boundary.AddettoInteraction.access$000(AddettoInteraction.java:15)
    at it.csbeng.speedcontrolsystem.addettoApp.boundary.AddettoInteraction$1.mousePressed(AddettoInteraction.java:78)
    at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
    at java.awt.Component.processMouseEvent(Component.java:6502)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at it.csbeng.speedcontrolsystem.addettoApp.coordinator.AddettoAppCoordinator.generaNotifiche(AddettoAppCoordinator.java:20)
    at it.csbeng.speedcontrolsystem.addettoApp.boundary.AddettoInteraction.generaNotificheEvent(AddettoInteraction.java:179)
    at it.csbeng.speedcontrolsystem.addettoApp.boundary.AddettoInteraction.access$000(AddettoInteraction.java:15)
    at it.csbeng.speedcontrolsystem.addettoApp.boundary.AddettoInteraction$1.mousePressed(AddettoInteraction.java:78)
    at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
    at java.awt.Component.processMouseEvent(Component.java:6502)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

編輯:@EJP在此之前,我已經做了類似

public class RemoteObject implements IRemoteObject
{
    private static RemoteObject instance;

    public RemoteObject ()
    {
       instance = this;
    }

    public someType someMethod (...)
}

但是它拋出了同樣的異常。 此外,如您所見,在scServerConfig中有一個公共的靜態RMIServer服務器。

它封裝了注冊表參考。 如果我所做的這些事情是正確的,我認為添加靜態引用可能還不夠,即使艱難的“突然”也開始為我工作。 我錯了嗎?

java.rmi.NoSuchObjectException但添加了靜態引用

沒有添加。 您正在嘗試調用添加它的遠程方法。 您在那方面失敗了,所以它還沒有得到加強。

您需要在遠程對象的構造函數中設置靜態實例。 用遠程方法執行此操作可能已經為時已晚。

我建議您也將注冊表保留在靜態變量服務器端。 實際上嚴格來說,這是您需要保持靜態的唯一參考。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM