Hello, I've implemented a TryCommand, it's a magnolia command that tries a certain number of time to execute another command. It can try a certain number of times before calling it quit (see the example configuration in the PNG file).
I didn't want the implementation of this retry logic to get into our publication workflow, so I stashed that into this command. Maybe it will be useful to someone else here. Best regards, -- John Mettraux - http://jmettraux.wordpress.com
<<attachment: tryactivate.png>>
package com.example.magnolia.commands;
//import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import info.magnolia.commands.MgnlCommand;
import info.magnolia.commands.CommandsManager;
//import info.magnolia.context.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Tries a command multiple times
*/
public class TryCommand extends MgnlCommand {
Logger log = LoggerFactory.getLogger(TryCommand.class);
//
// FIELDS
protected long frequency = 5 * 60;
protected long count = 2;
protected String commandName = null;
//
// METHODS
/**
* How many seconds should this command wait before retrying the
* target command ?
*
* Default is to wait 5 minutes before each try.
*/
public void setFrequency (long l) { this.frequency = l; }
/**
* How many tries before calling it quit ?
*
* Will try 2 times by default.
*/
public void setCount (long l) { this.count = l; }
/**
* The name (in the command catalog) of the command to try.
*/
public void setCommandName (String s) { this.commandName = s; }
public long getFrequency () { return this.frequency; }
public long getCount () { return this.count; }
public String getCommandName () { return this.commandName; }
//
// execute METHOD
public boolean execute (info.magnolia.context.Context c)
throws Exception {
log.info("execute(magnolia.context)... should not get called...");
return false;
}
/**
* The execute command (note that this one uses the Apache Context,
* not the Magnolia one.
*/
public boolean execute (Context c)
throws Exception {
if (this.commandName == null) {
log.error(""+hashCode());
log.error
("please set the target command name with "+
"the 'commandName' parameter (nodedata)");
return true;
}
//
// ok, do the job, well try ...
for (long l=0; l < this.count; l++) {
log.info("try #"+l+" for '"+this.commandName+"'");
boolean result = true;
try {
result = executeCommand(c);
}
catch (Throwable t) {
String msg =
"run #"+l+
" of '"+this.commandName+"' failed because of "+t;
log.warn(msg);
log.debug(msg, t);
}
if (result == false) {
log.info("success at try #"+l);
return false; // success
}
log.info("will retry in "+this.frequency+" seconds");
try {
Thread.sleep(((int) this.frequency) * 1000);
}
catch (InterruptedException ie) {
}
// loop
}
log.info("failed to '"+this.commandName+"' after "+this.count+" tries");
return true; // failed
}
/**
* Looks up command in catalog and executes it with the current context.
*/
protected boolean executeCommand (Context c)
throws Throwable {
MgnlCommand com = (MgnlCommand) CommandsManager.getInstance()
.getCommand(this.commandName);
if (com == null) {
throw new RuntimeException
("no command named '"+this.commandName+"'");
}
return com.execute(c);
}
}
---------------------------------------------------------------- for list details see http://documentation.magnolia.info/ ----------------------------------------------------------------
