我实现了一个带有Java RMI的客户端/服务器应用程序,客户端和服务器在同一个项目中的不同包中(当然它们在同一台机器上运行),接口在"中定义。常见" package.When我试图获得两个远程对象中的一个时,我得到了前面提到的异常。我已经阅读了一些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批注进行批注。但我确定问题不是来自这个
这对我来说真的很神秘,这就是堆栈跟踪......非常感谢您的帮助!
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中有一个 public static RMIServer server;
封装了一个Registry引用。如果我所做的这些事情是正确的,我认为添加静态参考文献可能还不够,甚至是强硬的#s; suddendly&#34;开始为我工作。我错了吗?
答案 0 :(得分:1)
java.rmi.NoSuchObjectException但添加了静态引用
它没有被添加。您正在尝试调用添加它的远程方法。你已经失败了,所以它还没有成功。
您需要在远程对象的构造函数中设置静态实例。在远程方法中执行它可能已经太晚了。
我建议您将Registry保留在静态变量服务器端。事实上,严格来说,这是保持静态所需的唯一参考。