RMIServiceExporter not working, does not connect to RMI localhost at port 1099

14,622

Solution 1

I think if you remove the registryHost property it will dynamically create a host on 1099 since there isn't one already.

<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
    <property name="serviceName" value="spitterService"/>
    <property name="service" ref="spitterService"/>
    <property name="serviceInterface" value="com.spitter.service.SpitterService"/>
    <property name="registryPort" value="1099"/>
</bean>

Either that or start an RmiRegistry on 1099.

Solution 2

You must create myRmiExporter and extend RMIServiceExporter and then implement this method with this body

protected Registry getRegistry(String registryHost, int registryPort,
                                   RMIClientSocketFactory clientSocketFactory, RMIServerSocketFactory serverSocketFactory)
            throws RemoteException
    {

        if (registryHost != null)
        {
            // Host explictly specified: only lookup possible.
            if (logger.isInfoEnabled())
            {
                logger.info("Looking for RMI registry at port '" + registryPort + "' of host [" + registryHost + "]");
            }
            try
            {
                Registry reg = LocateRegistry.getRegistry(registryHost, registryPort, clientSocketFactory);
                testRegistry(reg);
                return reg;
            }
            catch (RemoteException ex)
            {
                logger.debug("RMI registry access threw exception", ex);
                logger.warn("Could not detect RMI registry - creating new one");
                // Assume no registry found -> create new one.
                LocateRegistry.createRegistry(registryPort);
                Registry reg = LocateRegistry.getRegistry(registryHost, registryPort, clientSocketFactory);
                testRegistry(reg);
                return reg;
            }
        }

        else
        {
            return getRegistry(registryPort, clientSocketFactory, serverSocketFactory);
        }
    }

it`s worked

Share:
14,622
rohit
Author by

rohit

Java Developer

Updated on August 28, 2022

Comments

  • rohit
    rohit almost 2 years

    I have exported Spring managed service beans into RMI service using following configuration:

    <bean class="org.springframework.remoting.rmi.RmiServiceExporter">
            <property name="serviceName" value="spitterService"/>
            <property name="service" ref="spitterService"/>
            <property name="serviceInterface" value="com.spitter.service.SpitterService"/>
            <property name="registryHost" value="127.0.0.1"/>
            <property name="registryPort" value="1099"/>
     </bean>
    

    but when i load my spring configuration file(deploy my application in Tomcat), it throws following exception

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.remoting.rmi.RmiServiceExporter#0' defined in ServletContext resource [/WEB-INF/spitter-servlet.xml]: Invocation of init method failed; nested exception is java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: 
        java.net.ConnectException: Connection refused: connect
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:872)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
        at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4681)
        at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5184)
        at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5179)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:662)
    Caused by: java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: 
        java.net.ConnectException: Connection refused: connect
        at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)
        at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
        at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
        at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
        at sun.rmi.registry.RegistryImpl_Stub.list(Unknown Source)
        at org.springframework.remoting.rmi.RmiServiceExporter.testRegistry(RmiServiceExporter.java:415)
        at org.springframework.remoting.rmi.RmiServiceExporter.getRegistry(RmiServiceExporter.java:326)
        at org.springframework.remoting.rmi.RmiServiceExporter.prepare(RmiServiceExporter.java:264)
        at org.springframework.remoting.rmi.RmiServiceExporter.afterPropertiesSet(RmiServiceExporter.java:227)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
        ... 20 more
    Caused by: java.net.ConnectException: Connection refused: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
        at java.net.Socket.connect(Socket.java:529)
        at java.net.Socket.connect(Socket.java:478)
        at java.net.Socket.<init>(Socket.java:375)
        at java.net.Socket.<init>(Socket.java:189)
        at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
        at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
        at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595)
        ... 30 more
    

    please help me out..Thanks in advance

    I also created an rmi client for consuming RMI service

    public class SpitterMain {
    
        public static void main(String[] args){
            String serviceURL = "rmi://localhost:1099/spitterService";
            try {
                SpitterService spitterService = (SpitterService) Naming.lookup(serviceURL);
                Spitter spitter = spitterService.getSpitter("panneerselvam");
                List<Spittle> spittles = spitterService.getSpittlesForSpitter(spitter.getId(), 25);
    
                System.out.println("Welcome>"+spitter.getName()+"( "+spitter.getUsername()+" )");
    
                System.out.println("Your Recent Spittles are:");
    
                for(Spittle spittle: spittles){
                    System.out.println(spittle.getText()+" ON "+spittle.getWhen());
                }
    
            } catch (NotBoundException ex) {
                Logger.getLogger(SpitterMain.class.getName()).log(Level.SEVERE, null, ex);
            } catch (MalformedURLException ex) {
                Logger.getLogger(SpitterMain.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RemoteException ex) {
                Logger.getLogger(SpitterMain.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    

    Now i get following exception..

    java.rmi.UnmarshalException: error unmarshalling return; nested exception is: 
        java.lang.ClassNotFoundException: org.springframework.remoting.rmi.RmiInvocationHandler (no security manager: RMI class loader disabled)
        at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
        at java.rmi.Naming.lookup(Naming.java:84)
        at com.spitter.client.SpitterMain.main(SpitterMain.java:28)
    Caused by: java.lang.ClassNotFoundException: org.springframework.remoting.rmi.RmiInvocationHandler (no security manager: RMI class loader disabled)
        at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:535)
        at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:628)
        at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:294)
        at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStream.java:238)
        at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1530)
        at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1492)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1731)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
        ... 3 more
    

    Please check this guys...Thanks again

  • rohit
    rohit about 12 years
    Great help From you @Gareth Davis, but can you tell me why i can't write "localhost" in registryHost property
  • Gareth Davis
    Gareth Davis about 12 years
    basically because of this line of code github.com/SpringSource/spring-framework/blob/3.0.x/… if registryHost is specified then it assumes it isn't localhost (because that is the default) and since it is likely to be remote it can not hope to lazily create it