Java - Dynamic Interface Implementation

215 views Asked by At

I have lots of interfaces. Each of then defines a set of actions of a module. I need to use those interfaces as remote RMI interfaces, but I don't want to specify that in the original interface definition.

I could manually(for each action interface), create a new Interface that extends both 'java.rmi.Remote' and my action interface. Is there a way to do this in a dynamic way?

I don't know If I'm explaining myself right. Feel free to ask any questions.

1

There are 1 answers

2
Robert Moskal On

One way to do this would be to use Spring's RMI remoting feature. It will let you expose a java interfaces that don't descend from java.rmi.Remote via what they call RMI invokers.

All the docs are here:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/remoting.html#remoting-rmi-server

You just register your interface implementations in spring and use the org.springframework.remoting.rmi.RmiServiceExporter to expose your interface implementation via RMI.

<bean id="accountService" class="example.AccountServiceImpl">
        <!-- any additional properties, maybe a DAO? -->
</bean>

<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
    <!-- does not necessarily have to be the same name as the bean to be exported -->
    <property name="serviceName" value="AccountService"/>
    <property name="service" ref="accountService"/>
    <property name="serviceInterface" value="example.AccountService"/>
    <!-- defaults to 1099 -->
    <property name="registryPort" value="1199"/>
</bean>

Completely non-intrusive, but you have to drink the spring kool-aid. Is that such a bad thing, though?

For a truly dynamic solution, you can use java or groovy to build your application context in code. You could iterate over your interfaces and then construct the proper RmiServiceExporter instances in code.

Either one of these methods will handle this:

http://static.springframework.org/spring-javaconfig/docs/1.0.0.M4/reference/html/

http://grails.org/Spring%2BBean%2BBuilder