Problem

You want to access a remote service by RMI or web service transparently.

Idea

Obviously there should be a remoting implementation instead of normal local implementation of your service. However, we still need to write code to do real thing. Why reinvent wheels? There is a package named spring-remoting in spring framework, it handle this problem well, and the solution has been proved. Let's make it hivemind way!

Solution

First, a service point to provide service like usual:

    <service-point id="FinanceService" interface="mypackage.FinanceService"/>

Then, a remote implementation:

    <service-point id="rmiFinanceInterceptor" 
        interface="org.aopalliance.intercept.MethodInterceptor">
        <invoke-factory>
            <construct class="org.springframework.remoting.rmi.RmiClientInterceptor">
                <set property="serviceUrl" value="rmi://myserver:1399/financeService"/>
                <set-object property="serviceInterface"
                   value="class:mypackage.FinanceService"/>
            </construct>
        </invoke-factory>    
    </service-point>
    <implementation service-id="mypackage.FinanceService">
       <invoke-factory service-id="hivemind.lib.PlaceholderFactory"/>
       <interceptor service-id="hivemind.lib.MethodInterceptorFactory">
           <impl object="service:rmiFinanceInterceptor"/>
       </interceptor>
    </implementation>

That's it! The magic here is the hivemind.lib.MethodInterceptorFactory service, it opens the door to spring interceptors. In this way, we can even use spring transaction package to declare hivemind transactional service!

How about go further

Make a ServiceImplementationFactory (let's name it RMIRemotingServiceFactory) to declare the service easier and cleaner, so we can do:

    <implementation service-id="mypackage.FinanceService">
        <invoke-factory service-id="hivemind.contrib.RMIRemotingServiceFactory">
            <remote url="rmi://myserver:1399/financeService"/>
        </invoke-factory>
    </implementation>
  • No labels