Step1:设置时间戳;
String schedulingExpression="";
时间戳格式:
0 37 10 ? * *
秒分时星期月*
Step2:定义Job执行流程;
final Runnable updateTrialDetail = new Runnable() {
public void run() {
runService();
}
};
Step3:添加Job到任务列表(根据设置的时间戳)
this.scheduler.addJob("update trial detail page", updateTrialDetail, serializable,
schedulingExpression, canRunConcurrently);
addJob(Job名称,Job,任务列表,时间戳,Boolean);
示例AEM Bundle:
package studyconnect.core.schedulers;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.commons.scheduler.Scheduler;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import studyconnect.core.services.SCCommonService;
import studyconnect.core.services.SCDeleteAccountService;
import studyconnect.core.services.impl.SCConfigurationServiceImpl;
/**************************************************************************************
Class Name : SchedulerService
Version : 1.0
Created Date : 07 Auguest 2017
Function : This is common class for scheduler service.
Modification Log :
*************************************************************************************/
@Component
public class SchedulerService {
/** Default log. */
protected final Logger log = LoggerFactory
.getLogger(SchedulerService.class);
@Reference
private Scheduler scheduler;
@Reference
private SCConfigurationServiceImpl configurationServiceImpl;
@Reference
SCCommonService scCommonService;
@Reference
private SCGeneratePagesService scGeneratePagesService;
@Reference
private SCHealthConditionsService sCHealthConditions;
@Reference
private SCPDFService scPDFService;
@Reference
SCDeleteAccountService deleteAccountService;
public static String startDate = "";
public static String startDate_pdf = "";
@Activate
private void activate(ComponentContext componentContext) {
Map<String, Serializable> serializable = new HashMap<String, Serializable>();
boolean canRunConcurrently = true;
String updateUserProfileSE = configurationServiceImpl.getUpdateProfileTime();
final Runnable updateUserProfile = new Runnable() {
public void run() {
updateUserProfile();
}
};
Boolean isPublish = scCommonService.isPublish();
try {
if(!updateUserProfileSE.equals("")&&isPublish){
this.scheduler.addJob("update user profile", updateUserProfile, serializable,
updateUserProfileSE, canRunConcurrently);
}
} catch (Exception e) {
updateTrialDetail.run();
}
}
private void updateUserProfile(){
log.info("Delete user profile service will run...");
deleteAccountService.deleteProfileInfo();
}
}
https://helpx.adobe.com/experience-manager/using/aem-first-components1.html
AEM6.4
@Override
public void run() {
logger.info("This scheduled task is working in wyndham. CurrentTime: "+new Date().getTime());
}
@Activate
protected void activate() {
String expression = configurationService.getTimePattern();
int schedulerID = schedulerName.hashCode();
addScheduler(expression, schedulerID);
}
@Deactivate
protected void deactivate() {
int schedulerID = schedulerName.hashCode();
removeScheduler(schedulerID);
}
/**
* Remove a scheduler based on the scheduler ID
*/
private void removeScheduler(int schedulerID) {
logger.debug("Removing Scheduler Job '{}'", schedulerID);
scheduler.unschedule(String.valueOf(schedulerID));
}
/**
* Add a scheduler based on the scheduler ID
*/
private void addScheduler(String expression, int schedulerID) {
//Only run in the Author instance
Boolean isAuthor = true;
if(isAuthor) {
ScheduleOptions sopts = scheduler.EXPR(expression);
sopts.name(String.valueOf(schedulerID));
sopts.canRunConcurrently(false);
scheduler.schedule(this, sopts);
logger.debug("Scheduler added succesfully");
} else {
logger.debug("OSGIR6SchedulerExample is Disabled, no scheduler job created");
}
}