Spring Remoting RMI , client failover to standby server

934 Views Asked by At

Env : Spring Framework 3.2.3.RELEASE

Is there a way achieve failover with RmiProxyFactoryBean or any other in a straight forward spring context configuration ?

I have a ServerA as primary & ServerB as alternate RMI servers. If connection to ServerA is down , RMI calls should be directed to ServerB.

1

There are 1 best solutions below

0
On

I have overridden refreshAndRetry & lookupStub method of RMIProxyFactoryBean to acheive this failover switching.

@Override
    protected Object refreshAndRetry(MethodInvocation invocation)   throws Throwable {

        if (logger.isDebugEnabled()) {
            logger.debug("Refreshing & retrying remote invocation.");
        }

        swapHitURLs();//Swap primary<>secondary URL

        return super.refreshAndRetry(invocation);
    }

@Override
    protected Remote lookupStub() throws RemoteLookupFailureException {
        Remote stub=null;

        if (logger.isDebugEnabled()) {
            logger.debug("Looking up the stub.");
        }

        try{
            stub=super.lookupStub();
        }catch(RemoteLookupFailureException rlfx){
            swapHitURLs();//Swap primary<>secondary URL
            if (logger.isDebugEnabled()) {
                logger.debug("Looking up the stub for swapped URL.");
            }
            stub=super.lookupStub();
        }

        return stub;
    }

Configuration XML:

<bean id="beanName" class="com.ahamed.poc.rmi.FailOverRMIProxyFactory" lazy-init="true">
    <property name="serviceUrl" value="PrimaryURL"/>
    <property name="alternativeServiceUrl" value="SecondaryURL"/>
    <property name="serviceInterface" value="com.ahamed.poc.rmi.InterfaceClass"/>
</bean> 

If any better options let me know.