在特定时间运行Java程序 [英] run a Java program in specific time
问题描述
我需要帮助在特定时间(例如索引新文件)在服务器上运行我的Java程序。
i need help to run my Java program on the server at a specific time like 2 pm (to index the new files).
有人告诉我Java已经有些东西叫做工作,但我不知道如何使用它。我试过这个:
Someone told me that Java has some thing called jobs but I don't know how to work with that. I tried this:
boolean cond=true;
while(cond){
@SuppressWarnings("deprecation")
int heur = new Date().getHours();
@SuppressWarnings("deprecation")
int minute= new Date().getMinutes();
if(heur==16 && minute==02){
indexer.close();
end = new Date().getTime();
File f;
cond=false;
}
但是这个程序仍在运行。
But with this the program is still running.
我如何在指定时间运行我的程序?
How could I run my program at a specified time?
推荐答案
Theres a API名为 Quartz ,这是您的程序可以安排作业的地方,它将在那时运行它。
Theres a API called Quartz, It's where your program can schedule "Jobs" and it will run it at that time.
在我举一个例子之前,试试此链接。
Until I can give an example, try this link.
编辑:
首先,您必须创建一个实现org.quartz.Job的类。当你实现它时,你将不得不实现方法 execute(JobExecutionContext jobExecution)
,这是在触发触发器时将运行的方法。
First you have to create a class that implements org.quartz.Job. When you implement that you will have to implement the method execute(JobExecutionContext jobExecution)
, which is the method that will run when the "trigger" is fired.
设置时间表:
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
// Retrieve a scheduler from schedule factory
Scheduler scheduler = null;
try {
scheduler = schedulerFactory.getScheduler();
}
catch (SchedulerException e) {
e.printStackTrace();
}
//Set up detail about the job
JobDetail jobDetail = new JobDetail("jobDetail", "jobDetailGroup", ImplementedJob.class);
SimpleTrigger simpleTrigger = new SimpleTrigger("Trigger Name","defaultGroup", DATE);
// schedule a job with JobDetail and Trigger
scheduler.scheduleJob(jobDetail, simpleTrigger);
// start the scheduler
scheduler.start();
这篇关于在特定时间运行Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!