• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Execute Servlet At Given Time Instance

 
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're wrong.

Did you read the API docs of Timer and TimerTask?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wouldn't it just be simpler to write a daemon and have it fired off as a cron job?
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());


}}
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) is already answered.
2) makes no sense. It should be done differently as mentioned before.
 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Mohamed Inayath
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic