This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Services and the fly likes Call action class from timertask's run method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Call action class from timertask Watch "Call action class from timertask New topic
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
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
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
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Call action class from timertask's run method
 
Similar Threads
TimerTask and Actions (Servlets)
How to i invoke a method every 24 hours
Web Page to Excel Sheet
Killing current TimerTask
java.util.Timer inside Runnable?