在特定时间运行Java程序

发布于 2024-11-10 17:31:33 字数 510 浏览 12 评论 0原文

我需要帮助在特定时间(例如下午 2 点)在服务器上运行我的 Java 程序(以索引新文件)。

有人告诉我 Java 有一种叫做工作的东西,但我不知道如何使用它。我尝试了这个:

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

但是程序仍然在运行。

如何在指定时间运行我的程序?

i need help to run my Java program on the server at a specific time like 2 pm (to index the new files).

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房的律动 2024-11-17 17:31:34

有一个名为 Quartz 的 API,您的程序可以在其中安排“作业”并在该处运行它时间。

在我给出示例之前,请尝试此链接

编辑:
首先,您必须创建一个实现 org.quartz.Job 的类。当您实现该方法时,您必须实现方法execute(JobExecutionContext jobExecution),该方法将在“触发器”被触发时运行。

要设置时间表:

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

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.

Edit:
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.

To set up the Schedule:

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();
归属感 2024-11-17 17:31:34

循环中没有 Thread.sleep() 调用,因此它会以 100% CPU 的速度“旋转”(不好),但无论如何,这是一个糟糕的设计。一个很大的改进是简单地计算“现在”和您希望它运行之间的毫秒数,然后调用 Thread.sleep(n)。

然而,更好的解决方案是使用 JDK 已经提供的解决方案。

看一下这段代码,它使用 JDK 并发库中的类。
这是一个非常简单的例子,可以工作:

import java.util.concurrent.*;

public static void main(String[] args)
{
    ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
    Runnable runnable = new Runnable() {
        public void run()
        {
            // do something;
        }
    };

    // Run it in 8 hours - you would have to calculate how long to wait from "now"
    service.schedule(runnable, 8, TimeUnit.HOURS); // You can 
}

There's no Thread.sleep() call in the loop, so it will "spin" at 100% CPU (not good), but it's a poor design anyway. A big improvement would be to simply calculate the number of milliseconds between "now" and when you want it to run, then call Thread.sleep(n).

However, a better solution is to use what the JDK already provides.

Have a look at this code which uses classes from the JDK concurrent library.
This is a very simple example that would work:

import java.util.concurrent.*;

public static void main(String[] args)
{
    ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
    Runnable runnable = new Runnable() {
        public void run()
        {
            // do something;
        }
    };

    // Run it in 8 hours - you would have to calculate how long to wait from "now"
    service.schedule(runnable, 8, TimeUnit.HOURS); // You can 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文