简体   繁体   English

通过rmi参数发送对象时发生异常

[英]Exception when send an object through rmi arguments

When I am trying to send an object as an argument to rmi I am getting such an Exception: 当我尝试将对象作为rmi的参数发送时,出现了这样的异常:

HelloClient exception: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
    java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
    java.lang.ClassNotFoundException: IntegralClient$1 (no security manager: RMI class loader disabled)

Could you please help me to figure out what is wrong with my code and rmi server-client relationship implementation? 您能帮我找出我的代码和rmi服务器-客户端关系实现的问题吗?

Here is my code: 这是我的代码:

ServerSide: 服务器端:

import java.rmi.*;
import java.rmi.server.*; 

public class IntegralServer
{
    public static void main (String[] argv) 
    {
         try 
         {
             IntegralComputeEngine integralCounter = new IntegralComputeEngine();
             Naming.rebind("rmi://localhost/integral", integralCounter);
             System.out.println("Integral Server is ready.");
         }
         catch (Exception e)
         {
             System.out.println("Addition Server failed: " + e);
         }
    }
}

Client side: 客户端:

import java.rmi.*;

public class IntegralClient
{
    public static void main(String[] args) 
    {
        try 
        {
            IntegralComputeEngineInterface integralCounter = (IntegralComputeEngineInterface) Naming.lookup("rmi://localhost/integral");
            double result = integralCounter.computeIntegral(createFunction(), -1, 1);
            System.out.println("Result is: " + result);
        } 
        catch (Exception e) 
        {
            System.out.println("HelloClient exception: " + e);
        }
    }

    private static Function createFunction() 
    {
        return new Function()
        {
            @Override
            public double valueIn(double x) 
            {
                return 1-x;
            }
        };
    }
}

Function interface (which resides in both client and server projects): 功能接口(驻留在客户端和服务器项目中):

import java.io.Serializable;

public interface Function extends Serializable
{
    public double valueIn(double x);
}

IntegralComputeEngineInterface (which resides in both client and server projects): IntegralComputeEngineInterface(位于客户端和服务器项目中):

import java.rmi.*;

public interface IntegralComputeEngineInterface extends Remote 
{
    public double computeIntegral(Function function, double from, double to) throws RemoteException;
}

IntegralComputeEngineInterface (which resides only on server): IntegralComputeEngineInterface(仅位于服务器上):

import java.rmi.*;
import java.rmi.server.*;

public class IntegralComputeEngine  extends UnicastRemoteObject implements IntegralComputeEngineInterface 
{
    private final double ACCURACY = 100;

    protected IntegralComputeEngine() throws RemoteException 
    {super();}

    @Override
    public double computeIntegral(Function function, double from, double to) throws RemoteException 
    {
        double stepValue = (to - from) / ACCURACY;  
        double stepLength = Math.abs(stepValue);
        double result = 0.0; 

        for(int i = 0; i < ACCURACY; i++)
        {result += stepLength * function.valueIn(from + stepValue*(i + 0.5));}

        return result;
    }
}

Here is screenshot of my project organization: 这是我的项目组织的屏幕截图:

在此处输入图片说明

Will appreciate any help with regard to my problem! 感谢您对我的问题有任何帮助!

I found this: An argument to, or a return value from, a remote object can be any object that is serializable. 我发现了这一点: 远程对象的参数或返回值可以是可序列化的任何对象。 This includes primitive types, remote objects, and non-remote objects that implement the java.io.Serializable interface. 这包括实现java.io.Serializable接口的原始类型,远程对象和非远程对象。 For more details on how to make classes serializable, see the "Java Object Serialization Specification." 有关如何使类可序列化的更多详细信息,请参见“ Java对象序列化规范”。 Classes, for parameters or return values, that are not available locally are downloaded dynamically by the RMI system. RMI系统动态下载参数或返回值在本地不可用的类。 See the section on "Dynamic Class Loading" for more information on how RMI downloads parameter and return value classes when reading parameters, return values and exceptions. 有关在读取参数,返回值和异常时RMI如何下载参数和返回值类的更多信息,请参见“动态类加载”一节。 Here. 这里。

According to this there must be no problems in my passing of the arguments, but still they are. 据此,我的论点传递肯定没有问题,但仍然存在。

You should create a new class, extend UnicastRemoteObject and implement your Function simultaneously and it will be able to send such object over rmi arguments without any problems. 您应该创建一个新类,扩展UnicastRemoteObject并同时实现您的Function,它将能够通过rmi参数发送此类对象而不会出现任何问题。

So, I changed your Function to FunctionInterface, created new class Function: 因此,我将您的Function更改为FunctionInterface,创建了新的Function类:

import java.rmi.RemoteException;
import java.rmi.server.*;

public class Function extends UnicastRemoteObject implements FunctionInterface
{
    protected Function() throws RemoteException
    {super();}

    @Override
    public double valueIn(double x) 
    {
        return 1-x;
    }
}

So my IntegralClient looks now so: 所以我的IntegralClient现在看起来像这样:

import java.rmi.*;

public class IntegralClient
{
    public static void main(String[] args) 
    {
        try 
        {
            IntegralComputeEngineInterface integralCounter = (IntegralComputeEngineInterface) Naming.lookup("rmi://localhost/integral");
            double result = integralCounter.computeIntegral(createFunction(), -1, 1);
            System.out.println("Result is: " + result);
        } 
        catch (Exception e) 
        {
            System.out.println("HelloClient exception: " + e);
        }
    }

    private static Function createFunction() throws RemoteException 
    {
//      return new FunctionInterface()
//      {
//          @Override
//          public double valueIn(double x) 
//          {
//              return 1-x;
//          }
//      };

        return new Function();
    }
}

And it works! 而且有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM