There are times that you want the flexibility of your method to be able to recurse itself when an exception occurs to re-attempt without recursing too deeply. This could possibly be a call to remote Socket or RMI which has been restarted, and therefore the stub or the connector object instance needs re-allocating or other. I have opted to use an RMI call as an example due to reasons which will be clear pretty shortly. Say you are allocating and using RMI connection stub via singleton instance as shown bellow;
public class Foo
{
private RemoteStub stub;
public RemoteStub getRemoteConnection()
{
if(stub==null)
{
try
{
this.stub= ((RemoteStub) Naming.lookup(...));
}
catch (Exception e)
{
e.printStackTrace(System.out);
}
}
return stub;
}
}
Chances are when the remote server instance is restarted, your
stub object will not re-allocate itself to hold the new instance. And hence an attempt to make remote calls will yeild the exception below;
java.rmi.ConnectException: Connection refused to host: 192.168.0.1; nested exception is:
java.net.ConnectException: Connection timed out: connect
A quick and easy way to fix this issue is to have your connection stub builder method ignore null check or simply no singleton instance for your remote calls, which means every remote call will have the connection stub re-allocated and returned. As said, this is quick and easy, but doesn't come cheap neither. And so, this is the reason why I have decided to use this example as an insight into controlling method recursion, to make it pretty easy to follow through.
An alternative and yet effective approach is to extend your connection stub builder method to re-allocate on demand. This way when above exception occurs you can recurse your method to re-attempt once and ignore.
The assumption here is that if you do re-attempt and fails again, there is a high chance remote server is down or something is preventing you from connecting. Now, let us go ahead and expand on
getRemoteConnection() method and introduce the techiques for re-attempting the call.
public class Foo
{
private RemoteStub stub;
private AtomicBoolean reconnect;
public Foo()
{
reconnect= new AtomicBoolean(false);
}
public RemoteStub getRemoteConnection()
{
//Extended the builder to handle on demand
if(stub==null || reconnect.get())
{
try
{
this.stub= ((RemoteStub) Naming.lookup(...));
reconnect.set(false);
}
catch (Exception e)
{
e.printStackTrace(System.out);
}
}
return stub;
}
protected void reconnect()
{
reconnect.set(true);
}
}
As you can see above, we have expanded on the remote stub builder method to handle on demand call to re-allocate the remote stub object. This gives us the flexibility in our calling method to fire
reconnect() whenever we re-attempt. One issue we have not raised so far is the fact that, there is a high chance re-attempt via recursion will fail subsequently due to assumption made above. Here we are likely to enter into deep recursion and hence
StackOverflowError.
public class FooCaller extends Foo
{
private static final CharSequence failedConnect = "Connection refused";
public void callRemoteMethod()
{
try
{
int status = getRemoteConnection().doSomething();
System.out.println("Remote call - " + (status > -1 ? "Succeeded" : "Failed"));
}
catch (RemoteException e)
{
if (e.getMessage().contains(failedConnect))
{
// re-attempt
reconnect();
callRemoteMethod();
}
}
}
}
To control this we will use a token object and a simple stack handler, which does ensure recursions are detached straight after first re-attempt call, to avoid
StackOverflowError. See below for extended version of
FooCaller class with method handler for issuing attempt token and generic handler method for handling re-attempt calls to avoid code verbosity.
/**
Re-Attempt Token
*/
public class ReAttemptToken
{
public long lastAttempted = System.currentTimeMillis();
public String method;
public ReAttemptToken(String method)
{
this.method = method;
}
}
.. simple stack object to control the life span of
ReAttemptToken held for a specific method recurse;
/**
* Simple stack object for holding recursive method recall to avoid {@link StackOverflowError}.
*
* @author Bright Dadson
*
*/
@SuppressWarnings("serial")
public class ReAttemptStack extends ArrayList<ReAttemptToken>
{
/**
* Creates an empty Stack.
*/
public ReAttemptStack()
{
}
/**
* Pushes an item onto the top of this stack. This has exactly the same effect as:
*
*
* add(item)
*
*
*
*
* @param item the item to be pushed onto this stack.
* @return the item
argument.
* @see java.util.ArrayList#add
*/
public ReAttemptToken push(ReAttemptToken item)
{
add(item);
return item;
}
/**
* Tests if this stack is empty.
*
* @return true
if and only if this stack contains no items; false
otherwise.
*/
public boolean empty()
{
return size() == 0;
}
/**
* Locate and return items in this stack.
*
* @param o the desired {@link ReAttemptToken}.
* @return found object else null
*/
public synchronized ReAttemptToken locate(ReAttemptToken o)
{
int i = this.searchToken(o.method);
return (i >= 0) ? get(i) : null;
}
/**
* Scan this stack to search for token with method same as passed argument
*
* @param method - token method
* @return index of the token within this stack
*/
public int searchToken(String method)
{
for (int i = 0; i < size(); i++)
{
if (get(i).method.equals(method)) return i;
}
return -1;
}
}
.. and finally a new extended version of
FooCaller with stack control handlers;
public class FooCaller extends Foo
{
private long reAttemptTTL = 3 * 1000;
private ReAttemptStack stack;
private static final CharSequence failedConnect = "Connection refused";
private FooCaller()
{
stack = new ReAttemptStack();
}
/**
* Typical remote call
*
*/
public void callRemoteMethod()
{
try
{
int status = getRemoteConnection().doSomething();
System.out.println("Remote call - " + (status > -1 ? "Succeeded" : "Failed"));
}
catch (RemoteException e)
{
if (e.getMessage().contains(failedConnect))
{
try
{
ReAttemptToken token = getReAttemptToken("callRemoteMethod");
reAttemptRemoteCall(this.getClass().getMethod("callRemoteMethod"), new Object[] {}, token);
}
catch (Exception ex)
{}
}else e.printStackTrace();
}
}
/**
* This method issues token to calling functions. If the token is not expired and hence still exist, the method returns the same token from the
* stack for usage.
*
* @param method
* @return
*/
private ReAttemptToken getReAttemptToken(String method)
{
int tokenIndex;
if ((tokenIndex = stack.searchToken(method)) > -1)
{
return stack.get(tokenIndex);
}
return (new ReAttemptToken(method));
}
/**
* Using the method we can retry remote reconnection, obtain a fresh connection stub and re-invoke the failed method. In the aid of re-attempt
* token passing control, we can avoid indefinite recurssion from occuring when we re-invoke. Each token is expired every 3secs to enable recall.
* @param methodToRecurse
* @param params
* @param token
*/
private void reAttemptRemoteCall(Method methodToRecurse, Object[] params, ReAttemptToken token)
{
try
{
long now = System.currentTimeMillis();
boolean attempt = false;
if (stack.locate(token) == null)
{
stack.push(token);
attempt = true;
}
else if ((stack.locate(token) != null) && now > (reAttemptTTL + token.lastAttempted))
{
stack.remove(token);
stack.push(getReAttemptToken(token.method));
attempt = true;
}
if (attempt)
{
System.err.println("Re-Attempting failed remote call **");
reconnect();
methodToRecurse.invoke(this, params);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Disclaimer
The code and technique adopted in this post has only been written to demonstrate how to control StackOverFlowError from occuring in method calls which require recursion, to re-attempt situations where an exceptions is possible to occur due to anticipated reasons. Also note that, the approach adopted here is not required for all situations.
Drop a comment if you've spotted a bug, known enhancement possible to extend the technique or any relevant comment which is helpful to other readers.