• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing scheduled task with start and end time  RSS feed

 
Ravi Lunia
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I need to run a task to generate report on daily basis for every 2 hours starting 8 AM and ending at 6 PM. The code needs to be deployed with an existing application within a EAR on WAS 6.1 server.

What can be the best approach?

Thanks in advance.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use EJBs, you might use the Timer Service.

Otherwise, check out the java.util.Timer and TimerTask classes; they allow you to schedule code execution on a regular basis.
 
Ravi Lunia
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply!

I used Timertask and Timer. But when I am using scheduleAtFixedRate(TimerTask task, long delay, long period) and implemented run method in Task class. I call this method in init of my servlet. But after some time the task starts running in more than one threads. Is there any way to avoid that.

Thanks,
Ravi
 
kavuri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it will happen.Bec init method will be called once in aservlet.how wil it create more than one thread for that task
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet container is free to create and remove servlets as it pleases. So if you start any threads in a servlet's init method then you should terminate those in the destroy method.

A better way to create background threads would be in a ServletContextListener, which is guaranteed to run only once at the beginning (and end) of a web app's lifetime.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"vamsi",

please read your private messages regarding an important announcement.

Thank you,

Rob
 
Ravi Lunia
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Everyone!

I implemented it using ServletContextListener and it works just fine. I cancel the thread in the the contextDestroyed method.

Thanks for your help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!