📄 schedulejobactionhandler.java
字号:
package org.jbpm.delegation.action;
import org.dom4j.*;
import org.jbpm.*;
import org.jbpm.delegation.*;
import org.jbpm.model.scheduler.*;
import org.jbpm.util.xml.*;
public class ScheduleJobActionHandler extends Dom4jConfigurable implements ActionHandler {
private String className = null;
private String configuration = null;
private long delay = 0;
private long repeat = 0;
private String name = null;
public void execute(ExecutionContext executionContext) {
Long tokenId = executionContext.getToken().getId();
Job job = new Job( className, configuration, delay );
job.setTokenId( tokenId );
if ( repeat != 0 ) {
job.setRepeat( new Long( repeat ) );
}
SchedulerService schedulerService = executionContext.getSchedulerService();
if ( name != null ) {
schedulerService.schedule( job, tokenId.toString() + "-" + name );
} else {
schedulerService.schedule( job );
}
}
public void configure(Element root) throws ConfigurationException {
className = Dom4jHelper.getAttribute( root.element("delegation"), "class", null );
configuration = Dom4jHelper.getElementText( root, "delegation", null );
delay = parsePeriod( Dom4jHelper.getElementText( root, "delay", null ) );
repeat = parsePeriod( Dom4jHelper.getElementText( root, "repeat", null ) );
name = Dom4jHelper.getElementText( root, "name", null );
if ( delay < 0 ) {
throw new ConfigurationException( "delay is less then zero in a ScheduleJobActionHandler configuration" );
}
if ( repeat < 0 ) {
throw new ConfigurationException( "repeat is less then zero in a ScheduleJobActionHandler configuration" );
}
}
private static final long SECOND = 1000;
private static final long MINUTE = 60*SECOND;
private static final long HOUR = 60*MINUTE;
private static final long DAY = 24*HOUR;
private static final long MONTH = 30*DAY;
private static final long YEAR = 365*DAY;
private long parsePeriod(String text) {
long period = 0;
if ( text != null ) {
if ( ( period = getPeriod( text, "seconds", SECOND ) ) == 0 )
if ( ( period = getPeriod( text, "minutes", MINUTE ) ) == 0 )
if ( ( period = getPeriod( text, "hours", HOUR ) ) == 0 )
if ( ( period = getPeriod( text, "days", DAY ) ) == 0 )
if ( ( period = getPeriod( text, "months", MONTH ) ) == 0 )
if ( ( period = getPeriod( text, "years", YEAR ) ) == 0 )
if ( ( period = getPeriod( text, "s", SECOND ) ) == 0 )
if ( ( period = getPeriod( text, "m", MINUTE ) ) == 0 )
if ( ( period = getPeriod( text, "h", HOUR ) ) == 0 )
if ( ( period = getPeriod( text, "d", DAY ) ) == 0 )
if ( ( period = getPeriod( text, "y", YEAR ) ) == 0 ) {
try {
period = Long.parseLong( text );
} catch (NumberFormatException e) {
throw new ConfigurationException( "invalid period specification '" + text + "'" );
}
}
}
return period;
}
private long getPeriod( String text, String suffix, long factor ) {
long period = 0;
if ( text.endsWith( suffix ) ) {
String numberText = text.substring( 0, text.length()-suffix.length() ).trim();
if ( "".equals( numberText ) ) {
throw new ConfigurationException( "invalid period specification '" + text + "'" );
}
try {
period = Long.parseLong( numberText ) * factor;
} catch (NumberFormatException e) {
throw new ConfigurationException( "invalid number in period specification '" + text + "'" );
}
}
return period;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -