我正致力于一个有效的差异化因素。现在我希望它是遥远的。 所以我有一个“Function”-Interface,一个实现Function接口的“Funktion”类。 然后我有一个服务器,一个客户端和一个通用的服务接口。部分代码:
public class DifferentiatorClient {
public static void main(String[] args) throws RemoteException, MalformedURLException, IllegalArgumentException, NotBoundException, RecognitionException {
execute();
}
public static Double execute() throws RemoteException, MalformedURLException, NotBoundException, IllegalArgumentException, RecognitionException {
Service<Funktion, Double, Double> service;
String url = "//localhost/DifferentiatorService";
Double r;
Script script = new Script();
Funktion f = script.getFunction("f");
service = (Service<Funktion, Double, Double>) Naming.lookup(url);
r = service.execute(f, 2.0);
return r;
}
}
“Funktion”课程:
public class Funktion extends UnicastRemoteObject implements Function, Serializable {
/**
*
*/
private static final long serialVersionUID = -1234132759512350836L;
//some methods
}
界面“功能”:
public interface Function extends java.rmi.Remote {
double eval(final double ... args) throws IllegalArgumentException, RecognitionException, RemoteException;
}
服务器:
public class DifferentiatorService extends java.rmi.server.UnicastRemoteObject
implements Service<Funktion, Double, Double> {
private static final long serialVersionUID = -3236697150408344006L;
protected DifferentiatorService() throws RemoteException {
}
@Override
public String getName() throws RemoteException {
return "DifferentiatorService";
}
public Double execute(Funktion f, Double... args) throws RemoteException {
Differentiator diff = new Differentiator();
double result = diff.differentiate(f, args[0]);
return result;
}
public static void main(String[] args) throws Exception {
String url = "//localhost/DifferentiatorService";
Registry registry = LocateRegistry.createRegistry(1099);
DifferentiatorService service = new DifferentiatorService();
Naming.rebind(url, service);
}
}
最后,服务界面:
public interface Service<T,A,R> extends java.rmi.Remote {
String getName() throws java.rmi.RemoteException;
R execute(T task,A ... args) throws java.rmi.RemoteException;
}
对不起这段代码,我认为可能有必要找到错误。 首先,如果我使用的服务如下:
Service<String, Double, Double> service;
然后解析“服务器端”上的字符串并返回结果,使其完美运行。
演员:service = (Service<String, Double, Double>) Naming.lookup(url);
然后没有任何问题。
但是我试图让它与Funktion对象一起使用。我总是从标题中得到例外。 我用Google搜索了很多人,并说人们说重要的是投射到界面而不是具体的类。 我想我正在使用
进行界面转换service = (Service<Funktion, Double, Double>) Naming.lookup(url);
我也尝试过:
service = (Service<Function, Double, Double>) Naming.lookup(url);
但得到同样的例外。
我希望有人可以帮助我。 最好的问候
Exception in thread "main" java.rmi.UnmarshalException: Error unmarshaling return header; nested exception is:
java.io.EOFException
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:229)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:162)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
at com.sun.proxy.$Proxy1.execute(Unknown Source)
at de.lab4inf.wrb.DifferentiatorClient.execute(DifferentiatorClient.java:25)
at de.lab4inf.wrb.DifferentiatorClient.main(DifferentiatorClient.java:13)
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:267)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:215)
... 6 more
答案 0 :(得分:1)
您无法将存根转换为实现类Funktion
。它不是该类的实例。它是远程接口的一个实例。因此,将其强制转换为远程接口:Function
。并相应地更改Service
课程中的签名。
请注意,您仍然难以保持这些名称如此相似。将第一个更改为FunctionImpl
,FunctionServer
,FunctionRemoteObject
或其他任何内容。