RMI& Spring在客户端上以ClassCastException结束

时间:2013-03-20 15:05:48

标签: spring proxy rmi classcastexception

我编写了一个简单的客户端服务器架构,可以帮助我从MS Office文档中生成PDF文件。通过RMI处理通信,Spring将服务器端的整个复杂性包装起来。

我无法在客户端使用Spring,因为我从Matlab 2007b调用了这些方法。由于在Matlab中对静态和动态类路径进行了特殊处理,因此依赖于spring的Jar会产生异常。

长话短说:我在普通的java中编写了一个简单的RMI客户端:

import com.whatever.PDFCreationService;    

Object service = Naming.lookup("rmi://operations:1099/pdfCreationService");
System.out.println((PDFCreationService)service); //produces ClassCastException

接口:

public interface PDFCreationService {
    public PDFCreationConfig createPDF(PDFCreationConfig config) throws IOException, InterruptedException, OperationInterruptionException;
}

从我的“前”弹簧配置(客户端)中提取出来:

<bean id="pdfCreationService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
    <property name="serviceUrl" value="rmi://operations:1099/pdfCreationService"/>
    <property name="serviceInterface" value="com.whatever.creator.PDFCreationService"/>
</bean>

并在服务器端:

<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
    <property name="serviceName" value="pdfCreationService"/>
    <property name="service" ref="pdfCreationService"/>
    <property name="serviceInterface" value="com.whatever.creator.PDFCreationService"/>
    <!-- defaults to 1099 -->
    <property name="registryPort" value="1099"/>
</bean>

当我运行代码时,抛出以下异常:

Exception in thread "main" java.lang.ClassCastException: $Proxy0 cannot be cast to com.whatever.creator.PDFCreationService

我100%确定我不会尝试投放到这篇文章中的类:"ClassCastException: $Proxy0 cannot be cast" error while creating simple RMI application

spring是否将我的界面封装在不同的界面中?有没有办法找出代理隐藏的接口?

如果您需要更多详细信息以澄清我的问题,请与我们联系。

1 个答案:

答案 0 :(得分:1)

如果远程服务未实现RmiServiceExporterRmiInvocationHandler导出Remote,(即,不是传统的RMI服务器)

如果你不能在客户端使用RmiProxyFactoryBean,那是将服务调用转换为RemoteInvocations的服务接口代理的bean工厂,那么使用传统RMI似乎是更好的选择。< / p>

您也可以使用RmiServiceExporter导出传统的RMI服务,例如

public interface PDFCreationService extends Remote {
    public PDFCreationConfig createPDF(PDFCreationConfig config) throws RemoteException;
}