Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run tasks at scheduled time java,apache

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.

like image 794
i_raqz Avatar asked Feb 13 '11 07:02

i_raqz


2 Answers

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

    }
}
like image 78
ayush Avatar answered Oct 13 '22 22:10

ayush


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.

like image 35
cherouvim Avatar answered Oct 13 '22 20:10

cherouvim