A. java quartz 怎麼讀

擴兒詞
發音順序:四聲,輕聲,三聲

B. java quartz 定時需要的包

1.需要的jar包

實現定時任務 需要quartz-1.5.1.jar和commons-logging-1.1.jar。

2.定義定時任務配置類
該類主要進行定時任務時間的設置和設置對應的定時任務類。
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheler;
import org.quartz.impl.StdSchelerFactory;

public class Test{
/**
* 定時任務
*/
public static void cleanUpJob() {
try {
JobDetail jobDetail = new JobDetail();
jobDetail.setName("cleanup");
jobDetail.setJobClass(PrintJob.class);
CronTrigger trigger = new CronTrigger();
trigger.setName("cleanupTrigger");
trigger.setJobName("cleanup");
trigger.setCronExpression("0/5 * * * * ?");

Scheler sch = StdSchelerFactory.getDefaultScheler();
sch.scheleJob(jobDetail, trigger);
sch.start();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 修改定時任務時間
*/
public static void modifyJobTime(String triggerName, String time) {
System.out.println("modify~~~~~~~~~~~~~~~~");
try {
Scheler sched = StdSchelerFactory.getDefaultScheler();
System.out.println("triggerName " + triggerName);
CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerName, Scheler.DEFAULT_GROUP);
if(trigger == null) {
return;
}
String oldTime = trigger.getCronExpression();
System.out.println("oldTime " + oldTime);
if (!oldTime.equalsIgnoreCase(time)) {
System.out.println("time " + time);
// 修改時間
trigger.setCronExpression(time);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}

public static void main(String[] args) {
cleanUpJob();
modifyJobTime("cleanupTrigger", "0/2 * * * * ?");
}
}
3.定時任務類
該類主要定義定時任務執行的內容。
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class PrintJob implements Job{

@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String time = sdf.format(new Date());
System.out.println("print job" + time);
}

}
4.輸出結果

modify~~~~~~~~~~~~~~~~
triggerName cleanupTrigger
oldTime 0/5 * * * * ?
time 0/2 * * * * ?
print job2014-12-03 17:06:40.031
print job2014-12-03 17:06:42.015
print job2014-12-03 17:06:44.016
print job2014-12-03 17:06:46.019
print job2014-12-03 17:06:48.019
print job2014-12-03 17:06:50.020

從輸出的結果中可以看到,原本的定時任務為每5秒執行一次,但是由於後面對定時任務表達式做了修改,所以後來安照修改後的每2秒執行一次。

C. 用quartzi給一個java程序做定時任務,怎樣傳遞參數

java定時任務Timer
關於定時任務,似乎跟時間操作的聯系並不是很大

D. java中jdk1.4.2版本支持那個quartz版本啊!!急

怎麼不升級?可以查看quartz的文檔,裡面有說明。建議使用1.8之前的版本試一下。

E. java Quartz時間調度

<?xmlversion="1.1"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
default-autowire="byName"default-lazy-init="false">
<!--定時器工作類-->
<beanclass="com.wits.schele.BusinessJob"id="businessJob"/>
<!--定義調用對象和調用對象的方法-->
<beanclass="org.springframework.scheling.quartz."id="interfaceTrigger">
<!--調用的類-->
<propertyname="targetObject">
<refbean="businessJob"/>
</property>
<!--調用方法-->
<propertyname="targetMethod">
<value>generateBusinessInfo</value>
</property>
</bean>
<beanclass="org.springframework.scheling.quartz."id="killProcessTrigger">
<!--調用的類-->
<propertyname="targetObject">
<refbean="businessJob"/>
</property>
<!--調用方法-->
<propertyname="targetMethod">
<value>killProcess</value>
</property>
</bean>
<!--定義觸發時間-->
<beanclass="org.springframework.scheling.quartz.CronTriggerBean"id="excuteTrigger">
<propertyname="jobDetail">
<refbean="interfaceTrigger"/>
</property>
<!--cron表達式-->
<propertyname="cronExpression">
<value>0022**?</value>
</property>
</bean>
<beanclass="org.springframework.scheling.quartz.CronTriggerBean"id="excuteKillProcessTrigger">
<propertyname="jobDetail">
<refbean="killProcessTrigger"/>
</property>
<!--cron表達式-->
<propertyname="cronExpression">
<value>0023**?</value>
</property>
</bean>
<beanid="startQuertz"lazy-init="false"autowire="no"class="org.springframework.scheling.quartz.SchelerFactoryBean">
<propertyname="triggers">
<list>
<refbean="excuteTrigger"/>
<refbean="excuteKillProcessTrigger"/>
</list>
</property>
</bean>
</beans>

F. 如何在java中使用quartz

在java中使用quartz
/** *//**
* 添加一個定時任務,使用默認的任務組名,觸發器名,觸發器組名
* @param jobName 任務名
* @param job 任務
* @param time 時間設置,參考quartz說明文檔
* @throws SchelerException
* @throws ParseException
*/
public static void addJob(String jobName,Job job,String time)
throws SchelerException, ParseException{
Scheler sched = sf.getScheler();
JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, job.getClass());//任務名,任務組,任務執行類
//觸發器
CronTrigger trigger =
new CronTrigger(jobName, TRIGGER_GROUP_NAME);//觸發器名,觸發器組
trigger.setCronExpression(time);//觸發器時間設定
sched.scheleJob(jobDetail,trigger);
//啟動
if(!sched.isShutdown())
sched.start();
}

/** *//**
* 添加一個定時任務
* @param jobName 任務名
* @param jobGroupName 任務組名
* @param triggerName 觸發器名
* @param triggerGroupName 觸發器組名
* @param job 任務
* @param time 時間設置,參考quartz說明文檔
* @throws SchelerException
* @throws ParseException
*/
public static void addJob(String jobName,String jobGroupName,
String triggerName,String triggerGroupName,
Job job,String time)
throws SchelerException, ParseException{
Scheler sched = sf.getScheler();
JobDetail jobDetail = new JobDetail(jobName, jobGroupName, job.getClass());//任務名,任務組,任務執行類
//觸發器
CronTrigger trigger =
new CronTrigger(triggerName, triggerGroupName);//觸發器名,觸發器組
trigger.setCronExpression(time);//觸發器時間設定
sched.scheleJob(jobDetail,trigger);
if(!sched.isShutdown())
sched.start();
}

/** *//**
* 修改一個任務的觸發時間(使用默認的任務組名,觸發器名,觸發器組名)
* @param jobName
* @param time
* @throws SchelerException
* @throws ParseException
*/
public static void modifyJobTime(String jobName,String time)
throws SchelerException, ParseException{
Scheler sched = sf.getScheler();
Trigger trigger = sched.getTrigger(jobName,TRIGGER_GROUP_NAME);
if(trigger != null){
CronTrigger ct = (CronTrigger)trigger;
ct.setCronExpression(time);
sched.resumeTrigger(jobName,TRIGGER_GROUP_NAME);
}
}

/** *//**
* 修改一個任務的觸發時間
* @param triggerName
* @param triggerGroupName
* @param time
* @throws SchelerException
* @throws ParseException
*/
public static void modifyJobTime(String triggerName,String triggerGroupName,
String time)
throws SchelerException, ParseException{
Scheler sched = sf.getScheler();
Trigger trigger = sched.getTrigger(triggerName,triggerGroupName);
if(trigger != null){
CronTrigger ct = (CronTrigger)trigger;
//修改時間
ct.setCronExpression(time);
//重啟觸發器
sched.resumeTrigger(triggerName,triggerGroupName);
}
}

/** *//**
* 移除一個任務(使用默認的任務組名,觸發器名,觸發器組名)
* @param jobName
* @throws SchelerException
*/
public static void removeJob(String jobName)
throws SchelerException{
Scheler sched = sf.getScheler();
sched.pauseTrigger(jobName,TRIGGER_GROUP_NAME);//停止觸發器
sched.unscheleJob(jobName,TRIGGER_GROUP_NAME);//移除觸發器
sched.deleteJob(jobName,JOB_GROUP_NAME);//刪除任務
}

/** *//**
* 移除一個任務
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
* @throws SchelerException
*/
public static void removeJob(String jobName,String jobGroupName,
String triggerName,String triggerGroupName)
throws SchelerException{
Scheler sched = sf.getScheler();
sched.pauseTrigger(triggerName,triggerGroupName);//停止觸發器
sched.unscheleJob(triggerName,triggerGroupName);//移除觸發器
sched.deleteJob(jobName,jobGroupName);//刪除任務
}

G. java:如何使用 quartz定時執行任務,例如定時執行System.out.pintln("aa");看清楚用quartz

}
importstaticorg.quartz.CronScheleBuilder.cronSchele;
importstaticorg.quartz.JobBuilder.newJob;
importstaticorg.quartz.TriggerBuilder.newTrigger;

importjava.text.SimpleDateFormat;
importjava.util.Date;

importorg.quartz.CronTrigger;
importorg.quartz.JobDetail;
importorg.quartz.Scheler;
importorg.quartz.SchelerFactory;
importorg.quartz.impl.StdSchelerFactory;

publicclassTest{
publicvoidgo()throwsException{
//首先,必需要取得一個Scheler的引用
SchelerFactorysf=newStdSchelerFactory();
Schelersched=sf.getScheler();
//jobs可以在scheled的sched.start()方法前被調用

//job1將每隔30分鍾執行一次
JobDetailjob=newJob(myJob.class).withIdentity("job1","group1").build();
CronTriggertrigger=newTrigger().withIdentity("trigger1","group1").withSchele(cronSchele("00/30***?")).build();
Dateft=sched.scheleJob(job,trigger);
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ssSSS");
System.out.pintln("aa");


//開始執行,start()方法被調用後,計時器就開始工作,計時調度中允許放入N個Job
sched.start();
try{
//主線程等待一分鍾
Thread.sleep(60L*1000L);
}catch(Exceptione){}
//關閉定時調度,定時器不再工作
sched.shutdown(true);
}

publicstaticvoidmain(String[]args)throwsException{

Testtest=newTest();
test.go();
}

}

H. java quartz job 執行時間配置

第一步:引包
要使用Quartz,必須要引入以下這幾個包:
1、log4j-1.2.16
2、quartz-2.1.7
3、slf4j-api-1.6.1.jar
4、slf4j-log4j12-1.6.1.jar
這些包都在下載的Quartz包裡麵包含著,因此沒有必要為尋找這幾個包而頭疼。
第二步:創建要被定執行的任務類
這一步也很簡單,只需要創建一個實現了org.quartz.Job介面的類,並實現這個介面的唯一一個方法execute(JobExecutionContext arg0) throws JobExecutionException即可。如:
import java.text.SimpleDateFormat;

import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class myJob implements Job {

@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
System.out.println(sdf.format(new Date()));
}

}
import java.text.SimpleDateFormat;

import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class myJob implements Job {

@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
System.out.println(sdf.format(new Date()));
}

}

這個例子很簡單,就不用解說了。

第三步:創建任務調度,並執行
這一步應該算是最難的一步的,但其實是非常簡單的,直接上代碼


import static org.quartz.CronScheleBuilder.cronSchele;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheler;
import org.quartz.SchelerFactory;
import org.quartz.impl.StdSchelerFactory;

public class Test {
public void go() throws Exception {
// 首先,必需要取得一個Scheler的引用
SchelerFactory sf = new StdSchelerFactory();
Scheler sched = sf.getScheler();
//jobs可以在scheled的sched.start()方法前被調用

//job 1將每隔20秒執行一次
JobDetail job = newJob(myJob.class).withIdentity("job1", "group1").build();
CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchele(cronSchele("0/20 * * * * ?")).build();
Date ft = sched.scheleJob(job, trigger);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
System.out.println(job.getKey() + " 已被安排執行於: " + sdf.format(ft) + ",並且以如下重復規則重復執行: " + trigger.getCronExpression());

// job 2將每2分鍾執行一次(在該分鍾的第15秒)
job = newJob(myJob.class).withIdentity("job2", "group1").build();
trigger = newTrigger().withIdentity("trigger2", "group1").withSchele(cronSchele("15 0/2 * * * ?")).build();
ft = sched.scheleJob(job, trigger);
System.out.println(job.getKey() + " 已被安排執行於: " + sdf.format(ft) + ",並且以如下重復規則重復執行: "+ trigger.getCronExpression());

// 開始執行,start()方法被調用後,計時器就開始工作,計時調度中允許放入N個Job
sched.start();
try {
//主線程等待一分鍾
Thread.sleep(60L * 1000L);
} catch (Exception e) {}
//關閉定時調度,定時器不再工作
sched.shutdown(true);
}

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

Test test = new Test();
test.go();
}

}
import static org.quartz.CronScheleBuilder.cronSchele;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheler;
import org.quartz.SchelerFactory;
import org.quartz.impl.StdSchelerFactory;

public class Test {
public void go() throws Exception {
// 首先,必需要取得一個Scheler的引用
SchelerFactory sf = new StdSchelerFactory();
Scheler sched = sf.getScheler();
//jobs可以在scheled的sched.start()方法前被調用

//job 1將每隔20秒執行一次
JobDetail job = newJob(myJob.class).withIdentity("job1", "group1").build();
CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchele(cronSchele("0/20 * * * * ?")).build();
Date ft = sched.scheleJob(job, trigger);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
System.out.println(job.getKey() + " 已被安排執行於: " + sdf.format(ft) + ",並且以如下重復規則重復執行: " + trigger.getCronExpression());

// job 2將每2分鍾執行一次(在該分鍾的第15秒)
job = newJob(myJob.class).withIdentity("job2", "group1").build();
trigger = newTrigger().withIdentity("trigger2", "group1").withSchele(cronSchele("15 0/2 * * * ?")).build();
ft = sched.scheleJob(job, trigger);
System.out.println(job.getKey() + " 已被安排執行於: " + sdf.format(ft) + ",並且以如下重復規則重復執行: "+ trigger.getCronExpression());

// 開始執行,start()方法被調用後,計時器就開始工作,計時調度中允許放入N個Job
sched.start();
try {
//主線程等待一分鍾
Thread.sleep(60L * 1000L);
} catch (Exception e) {}
//關閉定時調度,定時器不再工作
sched.shutdown(true);
}

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

Test test = new Test();
test.go();
}

}

OK了,Job1和Job2就會被安排為定時執行了。此時程序是可以執行的了,但是可能會輸出WARN級別日誌,這是因為沒有加log4j的配置文件,加上配置文件,就OK了。這里需要說明的地方只有一個,其它的可以直接Copy到您的項目裡面。看代碼:
CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchele(cronSchele("0/20 * * * * ?")).build();
CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchele(cronSchele("0/20 * * * * ?")).build();

I. 我可以用java的quartz來定時執行shell腳本嗎

可以,其實你的問題就是java程序怎麼調用shell腳本。


ProcessBuilderpb=newProcessBuilder("xx.sh");
try{
Processp=pb.start();
}catch(IOExceptione){
e.printStackTrace();
}

J. 關於 java quartz 執行並發性 大蝦趕快來幫忙啊

<prop key="org.quartz.threadPool.threadCount">5</prop>
quartz可以設置線程並發數,同一個job不存在並發(處於線程等待狀態)。