Search code examples
javaspringservicermi

How to configure rmi service exporter endpoint address?


i have exported some rmi services .

 <bean id="entityRmiServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
    <property name="serviceName" value="entityService"/>
    <property name="service" ref="entityServiceImpl"/>
    <property name="serviceInterface" value="IEntityService"/>
    <property name="registryPort" value="1099"/>
</bean>

When run on my machine endpoint is 127.0.0.1:1099 but on VM it is 10.0.2.15:1099 ,ip address.

RmiServiceExporter:276 - Binding service 'entityService' to RMI registry: RegistryImpl[UnicastServerRef [liveRef: [endpoint:[127.0.0.1:1099](local),objID:[0:0:0, 0]]]]

where can i configure it manually ?


Solution

  • You can use placeholders in your Spring configuration and move the specific values to a properties file. To do that, you first need a bean that will resolve properties from file:

    <!-- Read file that contains properties -->
    <bean id="properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:app.properties" />
    </bean>
    

    Next you can modify your entityRmiServiceExporter bean to use the values from that file:

    <bean id="entityRmiServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
        <property name="serviceName" value="entityService"/>
        <property name="service" ref="entityServiceImpl"/>
        <property name="serviceInterface" value="IEntityService"/>
        <property name="registryPort" value="1099"/>
    
        <property name="registryHost" value="${rmi.endpoint}"/>
    </bean>
    

    And you need an app.properties file with a line like this:

    rmi.endpoint=10.0.2.15
    

    Alternative approach

    According to the RmiServiceExporter Javadoc, there might be an alternative approach. This Javadoc says:

    Note: RMI makes a best-effort attempt to obtain the fully qualified host name. If one cannot be determined, it will fall back and use the IP address. Depending on your network configuration, in some cases it will resolve the IP to the loopback address.

    You can tell RMI what the machines hostname is by passing -Djava.rmi.server.hostname=server.mycompany.com to your JVM when its launched.

    This means you don't have to configure your Spring bean - instead, you configure your JVM to expose the RMI interfaces on a different interface. If your machine is directly exposed to the internet (i.e., no firewall or something in between), I would not do that. If the machine is inside a company network, it might be acceptable or even preferable to solve it this way.