Java 定时任务

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");

}

}

转载于:https://my.oschina.net/slightScenery/blog/1865180

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值