数据库对象不适用于工作的 RMI 包 [英] Database object is not working with a working RMI package

查看:63
本文介绍了数据库对象不适用于工作的 RMI 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个项目.一个在任何部门都能正常工作.我下载并修改了它以更好地理解它.第二个是开发阶段的项目.

I have 2 projects. One works fine in ever department. I downloaded and modified it to better understand it. The 2nd one is a project in development phase.

现在,这两个项目具有几乎完全相同的 RMI 包,这在第一个项目中运行良好,但在第二个项目中不起作用.

Now, both these projects have almost exactly the same RMI package, which works fine in the first project, but not in the 2nd.

我在每个包中的测试类也基本相同.

My test classes in each package are essentially identical as well.

主要区别在于尝试访问哪些对象,它们都是数据库包中的接口.

The main difference is what objects there are attempting to access, which are both interfaces in a database package.

现在,第二个项目中的数据库包可以正常工作,只是无法与 RMI 一起使用.

Now, the database package in the 2nd project otherwise works absolutely fine, it just wont work with the RMI.

简而言之:

  • 数据库包工作正常
  • RMI 包工作正常
  • RMI 包和数据库不能正常工作.

这是我的数据库接口

public interface DB extends Remote {

public String[] read(int recNo) throws RecordNotFoundException;

public void update(int recNo, String[] data, long lockCookie)
throws RecordNotFoundException, SecurityException, IOException;

public void delete(int recNo, long lockCookie)
throws RecordNotFoundException, SecurityException, IOException;

public int[] find(String[] criteria);

public int create(String[] data) throws DuplicateKeyException, IOException;

public long lock(int recNo) throws RecordNotFoundException;

public void unlock(int recNo, long cookie)
throws RecordNotFoundException, SecurityException;
}

这是我的 RMIInterface

and here is my RMIInterface

public interface RMIInterface extends Remote{

     public DB getClient() throws RemoteException; 
}

我的RMII实现

public class RMIImplementation extends UnicastRemoteObject
    implements RMIInterface {  

private static String dbLocation = null;

private DB a;

public RMIImplementation() throws RemoteException{
}

public RMIImplementation(String dbLocation) throws RemoteException{
    System.out.println(dbLocation);
    this.dbLocation = dbLocation;
}

public static DB getRemote(String hostname, String port)
        throws RemoteException {
    String url = "rmi://" + hostname + ":" + port + "/DvdMediator";
    try {
        RMIInterface factory
                = (RMIInterface) Naming.lookup(url);
         // at this point factory equals Proxy[RMIInterface,................etc
        // i want the return to equal Proxy[DB,..............etc
return (DB) factory.getClient(); 
    } catch (NotBoundException e) {

        throw new RemoteException("Dvd Mediator not registered: ", e);
        }

      catch (java.net.MalformedURLException e) {
        throw new RemoteException("cannot connect to " + hostname, e);
    }
}

public DB getClient() throws RemoteException {
      try {
    a = new ContractorDatabase(dbLocation);

    }
    catch(Exception e){
        System.out.println("NewClass exception: " + e.toString());
    }
    return a;
}

还有我的 RMI 注册表

And my the RMI registry

public class RegDvdDatabase {

private RegDvdDatabase() {
}

public static void register()
        throws RemoteException {
    register(".", java.rmi.registry.Registry.REGISTRY_PORT);
}

public static void register(String dbLocation, int rmiPort)
        throws RemoteException {

    Registry r = java.rmi.registry.LocateRegistry.createRegistry(rmiPort);

    r.rebind("DvdMediator", new RMIImplementation(dbLocation));
}

}

让这两个一起工作抛出一个

Getting these two to work together throws a

Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be     cast to sjdproject.remote.RMIImplementation

你能帮我找到阻止它工作的数据库问题吗.

Can u please help me find the database issue that prevents it from working.

推荐答案

您必须将其强制转换为远程接口.

You must cast it to the remote interface.

EDIT 服务器代码中的注册表引用 r 必须是静态的.我找不到在实现类中定位客户端查找代码的任何充分理由.该类应该只存在于服务器,而不是客户端.

EDIT The Registry reference r in your server code must be static. I can't see any good reason for locating the client lookup code inside the implementation class. That class should only exist at the server, not the client.

这篇关于数据库对象不适用于工作的 RMI 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆