我正在尝试使用 spring 实现 rmi-iiop。我搜索了很多,但没有找到任何最新的例子。因此,我从本书中的“公开 Corba 服务”部分开始。即使它是基于较旧的 spring 版本编写的,它似乎也可以工作。
这些是我的配置
服务器
public interface IRemote extends Remote{
String getMsg() throws RemoteException;
}
public class RemoteImpl implements IRemote {
@Override
public String getMsg() throws RemoteException {
return "Hello";
}
}
豆子
<bean id="helloWorldService"
class="com.sample.remote.RemoteImpl" />
<bean class="org.springframework.remoting.rmi.JndiRmiServiceExporter">
<property name="jndiName" value="HelloWorld"/>
<property name="serviceInterface"
value="com.sample.remote.IRemote" />
<property name="service" ref="helloWorldService"/>
<property name="jndiEnvironment">
<props>
<prop key="java.naming.factory.initial">
com.sun.jndi.cosnaming.CNCtxFactory
</prop>
<prop key="java.naming.provider.url">iiop://localhost:1050</prop>
</props>
</property>
</bean>
客户
<bean id="helloWorldService" class="org.springframework.remoting.rmi.JndiRmiProxyFactoryBean">
<property name="jndiName">
<value>HelloWorld</value>
</property>
<property name="serviceInterface">
<value>com.sample.remote.IRemote</value>
</property>
<property name="jndiEnvironment">
<props>
<prop key="java.naming.factory.initial">
com.sun.jndi.cosnaming.CNCtxFactory
</prop>
<prop key="java.naming.provider.url">iiop://localhost:1050</prop>
</props>
</property>
</bean>
客户端类
public class Client {
@Autowired
IRemote remote;
public void clientMethod() {
try {
System.out.println(remote.getMsg());
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
这样可以正常工作。
但问题是在使用spring aop时。当 aop 创建一个代理 bean 时,服务器中的 stub/tie 查找失败
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.remoting.rmi.JndiRmiServiceExporter#0' defined in URL [jar:file:/D:/Code-base/Workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/sample-web/WEB-INF/lib/tech-ri-remote-1.0.0-M1.jar!/config/test_SpringContext.xml]: Invocation of init method failed; nested exception is java.rmi.StubNotFoundException: Stub class not found: $Proxy203_Stub; nested exception is:
java.lang.ClassNotFoundException: $Proxy203_Stub
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.rmi.StubNotFoundException: Stub class not found: $Proxy203_Stub; nested exception is:
java.lang.ClassNotFoundException: $Proxy203_Stub
at sun.rmi.server.Util.createStub(Util.java:292)
at sun.rmi.server.Util.createProxy(Util.java:140)
at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:196)
at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:310)
at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:237)
at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.exportObject(PortableRemoteObject.java:119)
at javax.rmi.PortableRemoteObject.exportObject(PortableRemoteObject.java:103)
at org.springframework.remoting.rmi.JndiRmiServiceExporter.prepare(JndiRmiServiceExporter.java:121)
at org.springframework.remoting.rmi.JndiRmiServiceExporter.afterPropertiesSet(JndiRmiServiceExporter.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564)
... 22 more
Caused by: java.lang.ClassNotFoundException: $Proxy203_Stub
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at sun.rmi.server.Util.createStub(Util.java:286)
... 32 more
即使异常说未找到 $Proxy203_Stub,但在调试时我发现实际异常发生在 Tie 查找失败时。
因此,作为 JndiRmiServiceExporter javadoc 中提到的另一个选项,我尝试不使用 java.rmi.Remote 接口和 RemoteException。但随后 bean 创建失败,找不到 RmiInvocationWrapper_Stub。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.remoting.rmi.JndiRmiServiceExporter#0' defined in URL [jar:file:/D:/Code-base/Workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/sample-web/WEB-INF/lib/tech-ri-remote-1.0.0-M1.jar!/config/test_SpringContext.xml]: Invocation of init method failed; nested exception is java.rmi.StubNotFoundException: Stub class not found: org.springframework.remoting.rmi.RmiInvocationWrapper_Stub; nested exception is:
java.lang.ClassNotFoundException: org.springframework.remoting.rmi.RmiInvocationWrapper_Stub
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.rmi.StubNotFoundException: Stub class not found: org.springframework.remoting.rmi.RmiInvocationWrapper_Stub; nested exception is:
java.lang.ClassNotFoundException: org.springframework.remoting.rmi.RmiInvocationWrapper_Stub
at sun.rmi.server.Util.createStub(Util.java:292)
at sun.rmi.server.Util.createProxy(Util.java:140)
at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:196)
at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:310)
at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:237)
at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.exportObject(PortableRemoteObject.java:119)
at javax.rmi.PortableRemoteObject.exportObject(PortableRemoteObject.java:103)
at org.springframework.remoting.rmi.JndiRmiServiceExporter.prepare(JndiRmiServiceExporter.java:121)
at org.springframework.remoting.rmi.JndiRmiServiceExporter.afterPropertiesSet(JndiRmiServiceExporter.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564)
... 22 more
Caused by: java.lang.ClassNotFoundException: org.springframework.remoting.rmi.RmiInvocationWrapper_Stub
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at sun.rmi.server.Util.createStub(Util.java:286)
... 32 more
同样,实际异常发生在 Tie 查找失败时。
我看到 RmiInvocationWrapper 存根和 tie不是最新版本中的 spring-context.jar 的一部分,并且不再需要 rmi。但我没有看到任何与 rmi-iiop 相关的东西。因此,假设需要 stub 和 tie,我创建了 _RmiInvocationWrapper_Tie.class 和 _RmiInvocationHandler_Stub.class 并将它们添加到 jar 中。然后服务器开始工作。
但是在启动客户端时,它失败了
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloWorldService' defined in URL [jar:file:/D:/Code-base/Workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/sample-web/WEB-INF/lib/tech-ri-1.0.0-M1.jar!/config/techri_SpringContext.xml]: Invocation of init method failed; nested exception is org.springframework.remoting.RemoteLookupFailureException: Could not narrow RMI stub to service interface [com.sample.remote.IRemote]; nested exception is java.lang.ClassCastException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Caused by: org.springframework.remoting.RemoteLookupFailureException: Could not narrow RMI stub to service interface [com.sample.remote.IRemote]; nested exception is java.lang.ClassCastException
at org.springframework.remoting.rmi.JndiRmiClientInterceptor.lookupStub(JndiRmiClientInterceptor.java:237)
at org.springframework.remoting.rmi.JndiRmiClientInterceptor.prepare(JndiRmiClientInterceptor.java:199)
at org.springframework.remoting.rmi.JndiRmiClientInterceptor.afterPropertiesSet(JndiRmiClientInterceptor.java:187)
at org.springframework.remoting.rmi.JndiRmiProxyFactoryBean.afterPropertiesSet(JndiRmiProxyFactoryBean.java:79)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564)
... 22 more
Caused by: java.lang.ClassCastException
at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:245)
at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:153)
at org.springframework.remoting.rmi.JndiRmiClientInterceptor.lookupStub(JndiRmiClientInterceptor.java:233)
... 27 more
Caused by: java.lang.ClassCastException: Object is not of remote type com.sample.remote.IRemote
at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:237)
... 29 more
所以我现在完全陷入困境,不知道如何继续。请指教。如果有人可以基于此提供任何最新的示例配置,那会更好。