Quartz使用之:远程job的执行

本文介绍如何使用Quartz实现远程任务调度。通过客户端-服务器架构,可在远程客户端触发服务器上的任务执行。示例代码展示了配置与启动过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

quartz提供了远程执行job的功能。本篇文章通过具体的例子来演示这一功能。

第一步:建立以下几个文件:

1.RemoteJob.java

远程要执行的任务,实现了Job接口。

2.RemoteClientLab.java

客户端程序,远程告诉Scheduler去执行一个任务。

3.client.properties

客户端属性文件

4.RemoteServerLab.java

服务器程序,监听端口,接到客户端的请求后按要求执行任务。

5.server.properties

服务器属性文件


第二步:实现


1.RemoteJob.java

[code]
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);
}
}
[/code]

2.RemoteClientLab.java

[code]

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();
}

}
[/code]

3.client.properties

[code]
# 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
[/code]

4.RemoteServerLab.java

[code]
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();
}

}
[/code]

5.server.properties
[code]
#============================================================================
# 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
[/code]

第三步:编译运行

将属性文件放在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

[url=http://www.51.la/?1613417][img]http://img.users.51.la/1613417.asp[/img][/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值