Run tasks at scheduled time java, apache

I have a requirement where in, I need to complete n tasks at certain intervals. I have a database that will contain the values ​​necessary to complete the task - java, and I have an Apache web server configured on the Windows platform.

Can someone help me in this task.

+5
source share
2 answers

You can use Quartz api for this fill.

Quartz is a scheduling API that is easy to use and initializes scheduling.

. Advance Trigger CronTrigger unix cron. CronTrigger , , . , , .

, ,

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

    }
}
+4

:

  • linux cron, URL wget curl
  • Quartz, Java

, apache, , .

+2

All Articles