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.
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 ");
}
}
You can either do this via:
ALso, apache doesn't seem to have any relation with your requirement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With