Sometimes "a" service does not respond and so we need to restart it. Usually it's a glitch in the network. We can have like 100 calls at the same time so the service cannot be restarted for 100 hundred times. The service is a Singleton. The command is called through a dispatcher, it's sync so different calls are executed in sequence.
Do you think that this approach is right to limit the restarting of the service?
public class RecoverServiceCommand extends AbstractCommand {
/**
* Logger for this class
*/
private static final Logger log = LoggerFactory
.getLogger(RecoverServiceCommand.class);
@Override
public boolean isAsynch() {
return false;
}
@Override
public boolean postCondition(Object... arg0) {
return true;
}
@Override
public boolean preCondition(Object... arg0) {
return true;
}
@Override
public void undo() {
}
@Override
protected void doExecute(Object... arg0) throws CommandException {
long now = System.nanoTime();
synchronized (RecoverServiceCommand.class) {
if (now - Service.getInstance().getLastUpdate() > Utils.TimeSpan)
Service.getInstance().restartService(now);
else
log.warn("Messaging queues not restarted because it was already restarted not so long ago");
}
}
}