This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Writing scheduled task with start and end time

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 2
  • 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 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
reply
    Bookmark Topic Watch Topic
  • New Topic