Quartz使用之:远程job的执行

Quartz使用之:远程job的执行quartz提供了远程执行job的功能。本篇文章通过具体的例子来演示这一功能。第一步:建立以下几个文件:1.RemoteJob.java(远程要执行的任务,实现了Job接口)。2.RemoteClientLab.java(客户端程序,远程告诉Scheduler去执行一个任务)。3.client.properties(客户端属性文件)4.Rem

大家好,又见面了,我是你们的朋友全栈君。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  

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/159218.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号