我有两个对等点可以通过 RMI 连接。在 RMI 服务器端,我使用 Spring 接口(interface) ( http://www.studytrails.com/frameworks/spring/spring-remoting-rmi.jsp )。在 RMI 客户端,我需要在不知道服务器是否启动的情况下启动它。因此,如果我使用 spring,我的客户端不会启动,并且出现连接拒绝异常。
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="RestoreRMIService" />
<property name="serviceInterface" value="br.com.eb.service.iscsi.RestoreService" />
<property name="service" ref="restoreService" />
</bean>
所以我尝试在客户端使用纯java RMI( http://www.javacoffeebreak.com/articles/javarmi/javarmi.html )。但我收到 ClassCastException: com.sun.proxy.$Proxy73 无法转换为 br.com.eb.service.iscsi.RestoreService。我相信是因为我没有来自 RMI 服务器的 stub 。
我在客户端使用纯java RMI,因为我需要能够重新启动服务器端。
protected void connectRMI() {
if (!rmiConected) {
try {
Registry registry = LocateRegistry.getRegistry(1099);
serviceRestore = (RestoreService) registry.lookup("RestoreRMIService");
// serviceRestore = (RestoreService) Naming.lookup("rmi://127.0.0.1/RestoreRMIService");
rmiConected = true;
} catch (ConnectException ce) {
System.err.println("Error: server not started");
rmiConected = false;
} catch (ConnectIOException cioe) {
System.err.println("Error: Cannot connect to server at 127.0.0.1");
rmiConected = false;
} catch (Exception e) {
e.printStackTrace();
}
}
}
最佳答案
您不需要仅仅为了解决“连接被拒绝”异常而切换 RMI 实现。
Java RMI 也会出现“连接被拒绝”异常。
Java RMI 和 Spring RMI 不互操作。
你的问题没有意义。
关于java - Spring RMI服务器和纯java RMI客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35851491/