I am trying to implement rmi-iiop using spring. I searched a lot but didn't find any latest example of it. So I started with Exposing Corba Services section in this book. Even though it is written based on older spring version, it seems to work.
These are my configurations
Server
public interface IRemote extends Remote{
String getMsg() throws RemoteException;
}
public class RemoteImpl implements IRemote {
@Override
public String getMsg() throws RemoteException {
return "Hello";
}
}
Beans
<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>
Client
<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>
Client class
public class Client {
@Autowired
IRemote remote;
public void clientMethod() {
try {
System.out.println(remote.getMsg());
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
This works fine as such.
But the problem is when using spring aop. As aop creates a proxy bean, the stub/tie lookup fails in the server with
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
Even though the exception says $Proxy203_Stub not found, on debugging I found that the actual exception occurs when Tie lookup fails.
So as another option mentioned in the JndiRmiServiceExporter javadoc, I tried without the java.rmi.Remote interface and RemoteException. But then bean creation fails with RmiInvocationWrapper_Stub not found.
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
Here also, the actual exception occurs on failure of Tie lookup.
I saw that RmiInvocationWrapper stub and tie are not part of spring-context.jar in the latest releases and are no longer needed for rmi. But I didn't see anything as such related to rmi-iiop. So assuming the stub and tie are needed, I created _RmiInvocationWrapper_Tie.class and _RmiInvocationHandler_Stub.class and added them to the jar. Then the server started working.
But on starting the client, it fails with
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
So I am completely stuck now and don't know how to proceed. Please advise. Also it would be better if someone can provide any latest example configurations based on this.