Add throws clause to run() method of TimeTask object

I am creating a struts application.

I have an action class that creates an object of Timer class and calls its schedule() method with a parameter of the TimerTask object. This method call executes the code written in the run() method of the TimerTask object.

My problem is that I want to return a user defined exception from this run method to the calling class i.e. the action class. But the run() method of the TimeTask object does not support any "throws" clause.

Any suggestions?

This is the code:

//Action Class:::::::::::::::::::::::::

public class Schedule extends LookupDispatchAction

{

public ActionForward startSchedule (ActionMapping am, ActionForm af,

HttpServletRequest req, HttpServletResponse res) throws Exception

{

Timer objTimer = new Timer();

OspTimerTask objOspTimerTask = new OspTimerTask();

Calendar cal = Calendar.getInstance();

objTimer.schedule(objOspTimerTask,cal.getTime(),interMilliSec);

// some code..

return...

}

}

//TimerTask implementation:::::::::::::::::::::::::::::::::::::::::::::

public class OspTimerTask extends TimerTask

{

public void run() <I want "throws" clause here>

{

try

{

//some code...

}

catch(SQLExecption e)

{

throw new UserDefinedException("My Message");

}

}

}

[1427 byte] By [Swapnil_Bandawara] at [2007-10-2 11:12:07]
# 1

Hi,

The code in the run() method of the TimerTask will be executed in its own thread of execution according to the schedule you have defined. Even if it were possible to force the signature of the OspTimerTask.run() method to declare that it throws a checked exception, an exception thrown in the thread of OspTimerTask will not get propagated to the "calling" thread of the Action class.

Try to consume and handle the exception in the OspTimerTask itself.

Cheers!

unmesh_malvankara at 2007-7-13 3:55:52 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
unmesh,thanks for ur reply.In that case, can u suggest of some other method (other than timer + timertask) wherein a task can be periodically performed and the exception in the code of the periodic activity can still be propagated to the calling function?
Swapnil_Bandawara at 2007-7-13 3:55:52 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Hi Swapnil,

If the handling of the exception can be done in a different "callback" method of the calling class, then I can suggest using this basic Event notification mechanism:

class Schedule extends LookupDispatchAction implements java.awt.event.ActionListener {

public ActionForward startSchedule (ActionMapping am, ActionForm af, HttpServletRequest req, HttpServletResponse res) throws Exception {

Timer objTimer = new Timer();

OspTimerTask objOspTimerTask = new OspTimerTask(this);

Calendar cal = Calendar.getInstance();

objTimer.schedule(objOspTimerTask,cal.getTime(), interMilliSec);

return;

}

public void actionPerformed(java.awt.event.ActionEvent e) {

Object obj = e.getSource();

Exception ex = (Exception) obj;

// work with the Exception object

}

}

class OspTimerTask extends TimerTask {

java.awt.event.ActionListener m_exceptionListener = null;

public OspTimerTask(java.awt.event.ActionListener exceptionListener) {

m_exceptionListener = exceptionListener;

}

public void run() {

try {

//some code...

} catch(Exception e) {

java.awt.event.ActionEvent exceptionEvent = new java.awt.event.ActionEvent(e, 1, "My message");

m_exceptionListener.actionPerformed(exceptionEvent);

}

}

}

unmesh_malvankara at 2007-7-13 3:55:52 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...