gpt4 book ai didi

java - RMI:将两个远程对象绑定(bind)到同一注册表时抛出 ClassNotFoundException

转载 作者:行者123 更新时间:2023-11-28 23:38:40 26 4
gpt4 key购买 nike

我有两个远程对象,我试图将它们绑定(bind)到同一个 RMI 注册表。这是我的项目结构:

我的第一个类(RemoteServer1.java)

package com.abc.rmi;

import java.rmi.AccessException;
import java.rmi.AlreadyBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import org.apache.log4j.Logger;


public class RemoteServer1 implements IServer1Interface{

private Logger logger = Logger.getLogger(RemoteServer1.class);

public void init(){
Registry localRegistry = null;
IServer1Interface server1 = null;
try
{
int port = 1099;
logger.info("Service Property rmiRegistryPort: " + port);
server1 = (IServer1Interface)UnicastRemoteObject.exportObject(this, 0);
logger.info("Locating registry");
localRegistry = LocateRegistry.getRegistry();
try
{
localRegistry.list();
}
catch (RemoteException localRemoteException2)
{
logger.info("Creating registry");
try
{
localRegistry = LocateRegistry.createRegistry(port);
}
catch (RemoteException localRemoteException4)
{
logger.error("Server1 RemoteException. " + localRemoteException4);
throw new RuntimeException("Server1 - Registry creation failed. ", localRemoteException4);
}
}
logger.info("Binding");
localRegistry.bind("Server1", server1);
}
catch (AlreadyBoundException localAlreadyBoundException)
{
logger.warn("Server1 - Object already bound." + localAlreadyBoundException);
try
{
logger.info("Re-Binding");
localRegistry.rebind("Server1", server1);
}
catch (AccessException localAccessException)
{
throw new RuntimeException("Server1 - AccessException while re-binding. ", localAccessException);
}
catch (RemoteException localRemoteException3)
{
throw new RuntimeException("Server1 - RemoteException while re-binding. ", localRemoteException3);
}
}
catch (RemoteException localRemoteException1)
{
logger.error("Server1 RemoteException. " + localRemoteException1);
throw new RuntimeException("Server1 - RemoteException. ", localRemoteException1);
}
logger.info("Remote Server1 ready");
}

}

这是我的第二个类(RemoteServer2.java)

package com.abc.rmi2;

import java.rmi.AccessException;
import java.rmi.AlreadyBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import org.apache.log4j.Logger;


public class RemoteServer2 implements IServer2Interface{

private Logger logger = Logger.getLogger(RemoteServer2.class);

public void init(){
Registry localRegistry = null;
IServer2Interface server2 = null;
try
{
int port = 1099;
logger.info("Service Property rmiRegistryPort: " + port);
server2 = (IServer2Interface)UnicastRemoteObject.exportObject(this, 0);
logger.info("Locating registry");
localRegistry = LocateRegistry.getRegistry();
try
{
localRegistry.list();
}
catch (RemoteException localRemoteException2)
{
logger.info("Creating registry");
try
{
localRegistry = LocateRegistry.createRegistry(port);
}
catch (RemoteException localRemoteException4)
{
logger.error("Server2 RemoteException. " + localRemoteException4);
throw new RuntimeException("Server2 - Registry creation failed. ", localRemoteException4);
}
}
logger.info("Binding");
localRegistry.bind("Server2", server2);
}
catch (AlreadyBoundException localAlreadyBoundException)
{
logger.warn("Server2 - Object already bound." + localAlreadyBoundException);
try
{
logger.info("Re-Binding");
localRegistry.rebind("Server2", server2);
}
catch (AccessException localAccessException)
{
throw new RuntimeException("Server2 - AccessException while re-binding. ", localAccessException);
}
catch (RemoteException localRemoteException3)
{
throw new RuntimeException("Server2 - RemoteException while re-binding. ", localRemoteException3);
}
}
catch (RemoteException localRemoteException1)
{
logger.error("Server2 RemoteException. " + localRemoteException1);
throw new RuntimeException("Server2 - RemoteException. ", localRemoteException1);
}
logger.info("Remote Server2 ready");
}

}

IServer1Interface 和 IServer2Interface 实现了 java.rmi.remote。 RemoteServer1.java 类能够在 RMI 注册表中注册,但是在第二个类(RemoteServer2.java)中调用“localRegistry.rebind(“Server2”,server2)”行时出现以下异常:

03-06-2014 18:03:43 ERROR RemoteServer2: - Server2 RemoteException. java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: com.abc.rmi2.IServer2Interface (no security manager: RMI class loader disabled)

我使用以下代码在 Tomcat 服务器中运行它:

URL url = new URL("file:///C:/temp/rmi/rmi1.jar");
URL[] urls = {url};
URLClassLoader child = new URLClassLoader(urls, this.getClass().getClassLoader());
Class classToLoad = Class.forName ("com.abc.rmi.RemoteServer1", true, child);
Method method = classToLoad.getDeclaredMethod ("init");
Object instance = classToLoad.newInstance();
Object result = method.invoke(instance);

URL url2 = new URL("file:///C:/temp/rmi/rmi2.jar");
URL[] urls2 = {url2};
URLClassLoader child2 = new URLClassLoader(urls2, this.getClass().getClassLoader());
Class classToLoad2 = Class.forName("com.abc.rmi2.RemoteServer2", true, child2);
Method method2 = classToLoad2.getDeclaredMethod("init");
Object instance2 = classToLoad2.newInstance();
Object result2 = method2.invoke(instance2);

我在启动 Tomcat 时没有定义任何安全管理器,也没有对 Tomcat 安全策略进行任何更改。我不明白的是它如何能够很好地绑定(bind)第一个远程类,但不能绑定(bind)第二个远程类。我在很多地方读到必须定义安全管理器才能使 RMI 工作,但它能够毫无问题地加载第一类。为什么只有当我尝试绑定(bind)第二个类时才会出现此问题。我还阅读了有关属性“java.rmi.server.codebase”的信息,但我不确定第一个是如何加载的,即使我没有指定此属性。有什么建议吗?

最佳答案

注册表 没有通过其 CLASSPATH 或代码库功能提供的第二个类。所以其中一个有问题。

关于java - RMI:将两个远程对象绑定(bind)到同一注册表时抛出 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22221214/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com