• 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

Call action class from timertask's run method

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I want to schedule a task which will be executed say every day or weekly.I have used struts2 action class in which i am doing all required tasks which include some database activities. I am using hibernate for database actions.
I am using Timer and TimerTask to schedule this task. But i am not able to call the action class from run method of the TimerTask.Does anyone have any idea regarding how to do this?

For example

/*Action class that will start the schedular*/
StartSchedular.java
public class Scheduler extends ActionSupport implements SessionAware
{

@Override
public String execute() throws Exception
{

TimerTask task = new foo(ac,request,response);
Calendar date = Calendar.getInstance();
Timer timer = new Timer();
timer.schedule(task ,5*1000,24*60*60*1000 );

return SUCCESS;
}
}


/*Scheduler class which will schedule the above action class to execute at specified time*/
class schedular extends TimerTask
{
public void run()
{
}
}


/*Scheduler class which will schedule the above action class to execute at specified time*/
class schedular extends TimerTask
{
public void run()
{
}
}

/*Action class that will be executed at scheduled time*/
foo.java

class foo extends ActionSupport implements sessionAware
{
/*
Logic goes here
*/

}



I want to call foo.java from run method. Can anyone help me please.
Reply With Quote
 
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
1. Refactor out the task to be performed.
2. I suggest using the Quartz scheduler instead of Timer, or at least ScheduledThreadPoolExecutor.
3. The task can now be invoked both by the scheduler or by the user initiated action.
Best wishes!
 
deepika mane
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi thanks for reply.But can you please elaborate on how can i call action class using ScheduledThreadPoolExecutor.
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
If you never have used ScheduledThreadPoolExecutor, I would suggest Googling for "ScheduledThreadPoolExecutor" and "tutorial".
Best wishes!
 
deepika mane
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for your reply. I tried it using quartz scheduler but i was not able to call an action class through quartz also.
My basic problem my job contains some database activities and i am using hibernate to handle database activities .
Struts2 sets hibernate environment before invoking action class by itself.
Can you tell me how to call a struts action class using quartz?
Or any other way to schedule my task which uses hibernate?
 
reply
    Bookmark Topic Watch Topic
  • New Topic