serving the solutions day and night

Pages

Sunday, February 13, 2011

Java Servlet CRON job through Tomcat

Apache Tomcat server won't support to run servelt automatically (Resin will support through <run-at> tag). So i created time scheduler job to perform cron job through Tomcat.

1) Create Schedular Servelt
package com.dns.blog;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* Parser Schedular to run every one hour to parse the file.
*
* @author Daynight
* @version 1.0 02/23/2011
*/

public class ParseSchedular extends HttpServlet {
/**
* init method - when tomcat server starts the servlet init.
*
* @param config- ServletConfig
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
CronJob pp = new CronJob ();
pp.getJobInfo();
}
catch (Exception e) {
}
}

/**
* Get method.
*
* @param request - HttpServletRequest
* @param response - HttpServletResponse
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}

/**
* Post method.
*
* @param request - HttpServletRequest
* @param response - HttpServletResponse
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}

2) Configuring the Tomcat webapps WEB-INF/web.xml
<!-- defines a time based to run servlet -->
<env-entry>
<description>Minutes to Parse - 1440 minutes = 1 day</description>
<env-entry-name>Minutes</env-entry-name>
<env-entry-value>1440</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>

<!-- defines a servlet -->
<servlet>
<servlet-name>ParseSchedular</servlet-name>
<display-name>Schedular run every one hour to parse</display-name>
<servlet-class>com.dns.blog.ParseSchedular</servlet-class>
<!-- load-on-startup starts the servlet when the server starts. -->
<load-on-startup>1</load-on-startup>
</servlet>


3) Create CronJob Class
package com.dns.blog;

import java.io.*;
import java.util.*;
import javax.naming.*;
import java.util.Timer;
import java.util.TimerTask;
/**
* Cron Job.
*
* @author Daynight
* @version 1.0 02/22/2011
*/
public class CronJob {
private final Timer timer = new Timer();
private int minutes = 15;

/**
* Web Call method.
*
*/
public void getJobInfo() {
try {
//Properties from web.xml enviornment file
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
String ctxminutes = (String) envCtx.lookup("Minutes");
this.minutes = Integer.parseInt(ctxminutes);
startScheduler();
}
catch (Exception e) {
}
}

/**
* start schedular.
*
*/
private void startScheduler() {
try {
timer.schedule(new TimerTask() {
public void run() {
scheduleParse();
//timer.cancel();
}
private void scheduleParse() {
//write your business logic here
}
}, 0, this.minutes * 60 * 1000);
}
catch (Exception e) {
}
}
}

4) Restart Tomcat server or Application through manager, service will execute every one day.

6 comments:

David Cassidy said...

Hey Mohideen
Why not use something like quartz which will run jobs at set times etc ?

Zaki Ahmad said...

I like this

Pravin Jain said...

I like this

Daynight said...

Thanks. Yes, Quartz is a good open source for scheduler, but i wrote it for compare to resin and tomcat server, resin has feature tag plus,some hosting company doesn't have/support quartz.jar.

Pepo (per gli amici) said...

It's a great bit of software... thanks for share :-)

Anonymous said...

This is very much useful information about cron job.
Really its very fruitful.

I also liked the following links regarding java job scheduling

Java Job Scheduling with java.util.Timer
http://jksnu.blogspot.com/2011/02/java-job-scheduling.html

Quartz Scheduling with JSP-Servlet
http://jksnu.blogspot.com/2011/03/quartz-framework-implementation-with.html

Quartz Scheduling with Spring Framework
http://jksnu.blogspot.com/