| Author |
Call action class from timertask's run method
|
deepika mane
Greenhorn
Joined: Jul 15, 2009
Posts: 5
|
|
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
|
 |
Ivan Krizsan
Bartender
Joined: Oct 04, 2006
Posts: 2193
|
|
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
Joined: Jul 15, 2009
Posts: 5
|
|
|
Hi thanks for reply.But can you please elaborate on how can i call action class using ScheduledThreadPoolExecutor.
|
 |
Ivan Krizsan
Bartender
Joined: Oct 04, 2006
Posts: 2193
|
|
Hi!
If you never have used ScheduledThreadPoolExecutor, I would suggest Googling for "ScheduledThreadPoolExecutor" and "tutorial".
Best wishes!
|
 |
deepika mane
Greenhorn
Joined: Jul 15, 2009
Posts: 5
|
|
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?
|
 |
 |
|
|
subject: Call action class from timertask's run method
|
|
|