Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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: 43028
76
  • 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: 43028
76
  • 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: 22665
127
Eclipse IDE Spring VI Editor 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