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  

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

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

(0)
上一篇 2022年7月14日 上午9:00
下一篇 2022年7月14日 上午9:00


相关推荐

  • curl 模拟 GET\POST 请求,以及 curl post 上传文件

    curl 模拟 GET\POST 请求,以及 curl post 上传文件curl 模拟 GET POST 请求 以及 curlpost 上传文件一般情况下 我们调试数据接口 都会使用一个 postman 的工具 但是这个工具还是有点大了 事实上 我们在调试一些小功能的时候 完全没有必要使用它 在命令行中 我们使用 curl 这个工具 完全可以满足我们轻量的调试要求 下面 我们来简单的说一下 curl 的一些常见使用方法 curlGET 请求 cu

    2026年3月19日
    2
  • python编译过程和执行原理

    python编译过程和执行原理python 编译过程和执行原理 1 python 执行原理这里的解释执行是相对于编译执行而言的 我们都知道 使用 C C 之类的编译性语言编写的程序 是需要从源文件转换成计算机使用的机器语言 经过链接器链接之后形成了二进制的可执行文件 运行该程序的时候 就可以把二进制程序从硬盘载入到内存中并运行 但是对于 Python 而言 python 源码不需要编译成二进制代码 它可以直接从

    2026年3月26日
    2
  • Mac 从本地copy项目到服务器

    Mac 从本地copy项目到服务器

    2022年2月11日
    53
  • 分享一款“暗黑系列”Idea主题插件Material Theme「建议收藏」

    分享一款“暗黑系列”Idea主题插件Material Theme「建议收藏」偶然发现一款“暗黑系列”idea主题插件,感觉非常不错,分享一下~github:https://github.com/equinusocio/material-theme安装plugins->browserepositories->search“material-theme”安装完重启idea设置切换主题Editor->ColorSc…

    2022年6月27日
    116
  • Unity学习笔记 之 发射小球碰撞物体的代码记录

    Unity学习笔记 之 发射小球碰撞物体的代码记录

    2022年3月4日
    45
  • QTreeView实现圆角样式

    QTreeView实现圆角样式QTreeView实现圆角样式在QTreeView等继承于QAbstractItemView表格中,定制表格样式通常都是通过设置项目代理(ItemDelegate)来实现。在这种实现方法中,每个项目(Item)基本上是孤立的,无法有效判断己身周遭环境。如果以此种方法来实现圆角样式,行首或许还能通过方法intQModelindex::column()来判断是否属于第一列来断定,然而行尾难道还要通过QAbstractItemModelQModelindex::*model()获取模型(Model)之后再

    2022年6月6日
    29

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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