java,apache按计划时间运行任务

发布于 2024-10-17 07:27:57 字数 121 浏览 7 评论 0原文

我有一个要求,我需要以一定的时间间隔执行 n 个任务。我有一个数据库,其中包含执行任务所需的值 - java,并且我在 Windows 平台上配置了 Apache Web 服务器。

有人可以指导我完成这项任务吗?

I have a requirement where in, I need to execute n tasks at certain intervals of times. I have a database which would contain the values needed to execute the task - java and I have an Apache webserver configured on a Windows platform.

Could somebody please guide me in accomplishing this task.

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

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

发布评论

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

评论(2

骄兵必败 2024-10-24 07:27:57

您可以使用 Quartz api 来实现此目的。

Quartz是调度API,它易于使用并且可以进行调度的初始化。

您可以将简单触发器与毫秒和重复作业结合使用,并设置重复间隔。 Advance Trigger CronTrigger 的工作原理与 unix cron 完全相同。在 CronTrigger 中,我们可以定义选定的日期,例如每周、每月和每年的周三、周五。

这是一个示例教程 解释了如何使用它

Quartz with Simple Servlet

web.xml

<web-app>
 <display-name>timer</display-name>

    <servlet>
     <servlet-name>InitializeServlet</servlet-name>
     <servlet-class>com.cron.InitializeServlet</servlet-class>
     <load-on-startup>1</load-on-startup>
    </servlet>

</web-app>

InitializeServlet.java

package com.cron;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class InitializeServlet extends HttpServlet {

 public void init() throws ServletException {

    try {
        System.out.println("Initializing NewsLetter PlugIn");

        CronScheluder objPlugin = new CronScheluder();

    }
    catch (Exception ex) {
      ex.printStackTrace();
    }

  }

}

CronScheduder.java

package com.cron;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class CronScheluder {

    public CronScheluder() throws Exception {

        SchedulerFactory sf = new StdSchedulerFactory();

        Scheduler sche = sf.getScheduler();

        sche.start();

        JobDetail jDetail = new JobDetail("Newsletter", "NJob", MyJob.class);

        //"0 0 12 * * ?" Fire at 12pm (noon) every day
        //"0/2 * * * * ?" Fire at every 2 seconds every day

 CronTrigger crTrigger = new CronTrigger("cronTrigger", "NJob", "0/2 * * * * ?");

        sche.scheduleJob(jDetail, crTrigger);
    }
}

MyJob.java

package com.cron;

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

public class MyJob implements Job {

    public void execute(JobExecutionContext context)
     throws JobExecutionException {

      System.out.println("Cron executing ");

    }
}

You can use Quartz api for this pourpose.

Quartz is scheduling API its easy to use and does initialization of scheduling.

You can use simple trigger with millisecond and repeat jobs and set repeat intervals. Advance Trigger CronTrigger works exactly same unix cron. In CronTrigger we can define, selected days e.g. Wednesday, Friday weekly, monthly and yearly.

Here is a sample tutorial that explains how to use it

Quartz with Simple Servlet

web.xml

<web-app>
 <display-name>timer</display-name>

    <servlet>
     <servlet-name>InitializeServlet</servlet-name>
     <servlet-class>com.cron.InitializeServlet</servlet-class>
     <load-on-startup>1</load-on-startup>
    </servlet>

</web-app>

InitializeServlet.java

package com.cron;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class InitializeServlet extends HttpServlet {

 public void init() throws ServletException {

    try {
        System.out.println("Initializing NewsLetter PlugIn");

        CronScheluder objPlugin = new CronScheluder();

    }
    catch (Exception ex) {
      ex.printStackTrace();
    }

  }

}

CronScheluder.java

package com.cron;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class CronScheluder {

    public CronScheluder() throws Exception {

        SchedulerFactory sf = new StdSchedulerFactory();

        Scheduler sche = sf.getScheduler();

        sche.start();

        JobDetail jDetail = new JobDetail("Newsletter", "NJob", MyJob.class);

        //"0 0 12 * * ?" Fire at 12pm (noon) every day
        //"0/2 * * * * ?" Fire at every 2 seconds every day

 CronTrigger crTrigger = new CronTrigger("cronTrigger", "NJob", "0/2 * * * * ?");

        sche.scheduleJob(jDetail, crTrigger);
    }
}

MyJob.java

package com.cron;

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

public class MyJob implements Job {

    public void execute(JobExecutionContext context)
     throws JobExecutionException {

      System.out.println("Cron executing ");

    }
}
夜夜流光相皎洁 2024-10-24 07:27:57

您可以通过以下方式执行此操作:

  • linux cron 作业,它将通过 wget 请求应用程序的特定 URL 或curl
  • Quartz,一个用于调度的 Java 库

ALso,apache 似乎与您的要求没有任何关系。

You can either do this via:

  • a linux cron job which will be requesting a specific URL of your app via wget or curl
  • Quartz, a Java library for scheduling

ALso, apache doesn't seem to have any relation with your requirement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文