Quartz使用之:遠程job的執行

quartz提供了遠程執行job的功能。本篇文章通過具體的例子來演示這一功能。

第一步:建立以下幾個文件:

1.RemoteJob.java (遠程要執行的任務,實現了Job接口)

2.RemoteClientLab.java (客戶端程序,遠程告訴Scheduler去執行一個任務)

3.client.properties (客戶端屬性文件)

4.RemoteServerLab.java (服務器程序,監聽端口,接到客戶端的請求後按要求執行任務)

5.server.properties (服務器屬性文件)


第二步:實現


1.RemoteJob.java 

package lab.quartz.lab12;

import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class RemoteJob implements Job {

    public static final String MESSAGE = "msg";

    private static Log _log = LogFactory.getLog(RemoteJob.class);

    public RemoteJob() {
    }

    public void execute(JobExecutionContext context)
        throws JobExecutionException {

        String jobName = context.getJobDetail().getFullName();
        String message = (String) context.getJobDetail().getJobDataMap().get(MESSAGE);

        _log.info("SimpleJob: " + jobName + " executing at " + new Date());
        _log.info("SimpleJob: msg: " + message);
    }
}


2.RemoteClientLab.java 

package lab.quartz.lab12;

import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class RemoteClientLab{

    public void run() throws Exception {

        Log log = LogFactory.getLog(RemoteClientLab.class);

        // First we must get a reference to a scheduler
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();

        // define the job and ask it to run
        JobDetail job = 
            new JobDetail("remotelyAddedJob", "default", RemoteJob.class);
        JobDataMap map = new JobDataMap();
        map.put("msg", "Your remotely added job has executed!");
        job.setJobDataMap(map);
        CronTrigger trigger = new CronTrigger(
                "remotelyAddedTrigger", "default",
                "remotelyAddedJob", "default", 
                new Date(), 
                null, 
                "/5 * * ? * *");

        // schedule the job
        sched.scheduleJob(job, trigger);

        log.info("Remote job scheduled.");
    }

    public static void main(String[] args) throws Exception {

        RemoteClientLab example = new RemoteClientLab();
        example.run();
    }

}

3.client.properties 
# Configure Main Scheduler Properties  ======================================
org.quartz.scheduler.instanceName = Sched1
org.quartz.scheduler.logger = schedLogger
org.quartz.scheduler.rmi.proxy = true
org.quartz.scheduler.rmi.registryHost = localhost
org.quartz.scheduler.rmi.registryPort = 1099

4.RemoteServerLab.java 
package lab.quartz.lab12;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SchedulerMetaData;
import org.quartz.impl.StdSchedulerFactory;

public class RemoteServerLab {

    public void run() throws Exception {
        Log log = LogFactory.getLog(RemoteServerLab.class);

        // First we must get a reference to a scheduler
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();

        log.info("------- Initialization Complete -----------");

        log.info("------- (Not Scheduling any Jobs - relying on a remote client to schedule jobs --");

        log.info("------- Starting Scheduler ----------------");

        // start the schedule
        sched.start();

        log.info("------- Started Scheduler -----------------");

        log.info("------- Waiting ten minutes... ------------");

        // wait five minutes to give our jobs a chance to run
        try {
            Thread.sleep(600L * 1000L);
        } catch (Exception e) {
        }

        // shut down the scheduler
        log.info("------- Shutting Down ---------------------");
        sched.shutdown(true);
        log.info("------- Shutdown Complete -----------------");

        SchedulerMetaData metaData = sched.getMetaData();
        log.info("Executed " + metaData.numJobsExecuted() + " jobs.");
    }

    public static void main(String[] args) throws Exception {

        RemoteServerLab example = new RemoteServerLab();
        example.run();
    }

}

5.server.properties 
#============================================================================
# Configure Main Scheduler Properties  
#============================================================================

org.quartz.scheduler.instanceName = Sched1
org.quartz.scheduler.rmi.export = true
org.quartz.scheduler.rmi.registryHost = localhost
org.quartz.scheduler.rmi.registryPort = 1099
org.quartz.scheduler.rmi.createRegistry = true

#============================================================================
# Configure ThreadPool  
#============================================================================

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5

#============================================================================
# Configure JobStore  
#============================================================================

org.quartz.jobStore.misfireThreshold = 60000

org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

第三步:編譯運行

將屬性文件放在classes的根目錄。classpath中增加需要的jar包。

1.啓動服務器

java -Dorg.quartz.properties=server.properties lab.quartz.lab12.RemoteServerRemote

2.啓動客戶端

java -Dorg.quartz.properties=client.properties lab.quartz.lab12.RemoteClientRemote  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章