aspose file tools*
The moose likes Servlets and the fly likes Execute Servlet At Given Time Instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Execute Servlet At Given Time Instance" Watch "Execute Servlet At Given Time Instance" New topic
Author

Execute Servlet At Given Time Instance

carina caoor
Ranch Hand

Joined: Jun 23, 2007
Posts: 300

Hi In my application i want to run a servlet daily at 7:00pm .. how could this be done in JAVA.Is there any other technology that could be implemented ...is servlet a good choice to add my business logic in ?My servlet basically is connecting to database and updating a table.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

A Servlet is a poor choice for this logic. Servlets exist to service requests, so to get the functionality you want you would need to wtire something that sends the request at your set time. Rather than doing this you could just use a Timer and TimerTask to do the same.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
carina caoor
Ranch Hand

Joined: Jun 23, 2007
Posts: 300

Timer and Timer Task are the good choice or do i need to opt Quartz?.. also i dont no anything about Quartz, my main requirement is just to update a table at given instance of time and connecting to database and updations are all possible in servlets then why servlet is not a good choice?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

ruquia tabassum wrote:Timer and Timer Task are the good choice or do i need to opt Quartz?..


Probably not. Quartz is a big beast - use it when your scheduling needs are complex.

my main requirement is just to update a table at given instance of time and connecting to database and updations are all possible in servlets then why servlet is not a good choice?

Because Servlets are request driven, so you have to write something like a Timer and TimerTask to drive the Servlet anyway, its just that these would have to send an HttpRequest over the network to your Servlet. Also your Servlet is open to any request, not just request from this TimerTask, you would have to add extra logic to make sure the update was not fired by any old user.

Also, its not good design to have JDBC operation directly in your Servlet. If you put these in some sort of DAO layer (or even just a simple utility class) both your Servlet and Timer stuff can use it.
Kuldeep Tewari
Ranch Hand

Joined: May 22, 2006
Posts: 35
I don't think it is possible to execute a job at specific time(every day at 7:00 AM in your case) using java timer class. Yes, you can set the timer to cause execution at specified time interval(say every 24 hrs) but this start time will be relative to your app(server) startup time.

I think, using quartz would be a better option for you and its setup should not be that difficult.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You're wrong.

Did you read the API docs of Timer and TimerTask?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

Wouldn't it just be simpler to write a daemon and have it fired off as a cron job?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Maybe not if it needs some state of the webapplication in question. Depends on the functional requirements of which we know nothing about though.

I would also emphasize, extending HttpServlet for this makes no sense. Just implement ServletContextListener and run a TimerTask class during create of the context. That's all. No need to create a servlet.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

K Tewari wrote:I don't think it is possible to execute a job at specific time(every day at 7:00 AM in your case) using java timer class.


schedule(TimerTask task, Date time) is how you do it.
Kuldeep Tewari
Ranch Hand

Joined: May 22, 2006
Posts: 35
Bauke Scholtz wrote:You're wrong.

Did you read the API docs of Timer and TimerTask?


My bad.

Paul Sturrock wrote:

schedule(TimerTask task, Date time) is how you do it.


or should it be public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)?
carina caoor
Ranch Hand

Joined: Jun 23, 2007
Posts: 300

My requirement is user provides the date and time, and based on that date and time provided the servlet should execute and make updates in the database.. how do i trigger a servlet from timer


i tried it this way but how do i get the user input date and time into timer class

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.net.URL;
import java.net.URLConnection;

public class TestTimer extends TimerTask {

public void run() {

try
{
URL aURL = new URL(
"http://localhost:8080/AutomatedIntelligence/charupdate/UploadBillCycle");

URLConnection aConnection = aURL.openConnection();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("Generating report");
//TODO generate report
}





public static void main(String[] args) {
Timer timer = new Timer();
Calendar date = Calendar.getInstance();
date.set(
Calendar.DAY_OF_WEEK,
Calendar.TUESDAY
);
date.set(Calendar.HOUR, 3);
date.set(Calendar.MINUTE, 30);
date.set(Calendar.SECOND, 0);
date.set(Calendar.MILLISECOND, 0);


timer.schedule(
new TestTimer(),
date.getTime());


}}
Amol Katyare
Ranch Hand

Joined: Apr 02, 2007
Posts: 36
Is there any specific reason you want to use Servlet?

As I understand, your task is to update DB on scheduled time. The time is given by user.

Which App Server are you using?

May be, you can have simple batch program that is scheduled by server.


SCJP [1.4]
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
ruquia tabassum wrote:My requirement is user provides the date and time, and based on that date and time provided the servlet should execute and make updates in the database.. how do i trigger a servlet from timer

Why on earth do you want to do this? You shouldn't tight couple business/database logic in a HttpServlet. Refactor the code, modularize your code, so that you can just execute the same code in your timertask class as the servlet class is doing.
Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
Hi,

Is it your query regarding tDo you want to know how to schedule or

Does your question is to know
1. how to schedule or
2. how to invoke the servlet from the schedular class.





Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
1) is already answered.
2) makes no sense. It should be done differently as mentioned before.
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
ruquia,

The best solution to your problem is what Paul Suggested.

A Servlet is a poor choice for this logic. Servlets exist to service requests, so to get the functionality you want you would need to wtire something that sends the request at your set time. Rather than doing this you could just use a Timer and TimerTask to do the same.


and if you want to know more about Timer people in javaranch can help you but implementing it the wrong way. ie using servlets for such a task nobody is going to suggest you.

Thanks,
Rahul


Rahul Juneja
ThoughtClicks - http://techlabs.thoughtclicks.com
Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
1. User data has to be stored in the Database Otherwise there is no way you get the user provided input.
2. Schedular/Timer has to read the data from the database and has to process the required business logic.
3. Why you need to have the schedular to invoke the Servlet - Can you a brief explanation?
 
Don't get me started about those stupid light bulbs.
 
subject: Execute Servlet At Given Time Instance