【问题标题】:RMI & Spring ends up on ClassCastException on clientRMI 和 Spring 在客户端出现 ClassCastException
【发布时间】:2013-03-20 15:05:48
【问题描述】:

我编写了一个简单的客户端服务器架构,可以帮助我从 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;
}

从我的“前”spring 配置中提取(客户端):

<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 是否将我的接口封装在不同的接口中?有没有办法找出 Proxy 隐藏了哪个接口?

如果您需要更多详细信息来澄清我的问题,请告诉我。

【问题讨论】:

  • 那个异常是从哪里抛出的 from ?堆栈跟踪摘录会很好......
  • 您的远程接口首先需要扩展 Remote。

标签: spring proxy rmi classcastexception


【解决方案1】:

如果远程服务没有实现Remote,则RmiServiceExporter 导出RmiInvocationHandler,(即,不是传统的RMI 服务器)

如果您不能在客户端使用 RmiProxyFactoryBean,这是一个用于将服务调用转换为 RemoteInvocations 的服务接口代理的 bean 工厂,使用传统 RMI 似乎是更好的选择。

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

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

【讨论】:

  • 非常感谢您提供宝贵的意见。它通过删除客户端上的所有 spring 依赖项解决了我的问题。 (如果不是所有相关的 jar 都单独添加到类路径中,Matlab 会不知何故感到困惑。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-19
  • 2011-03-27
  • 2013-12-09
  • 2014-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多