aspose file tools*
The moose likes Servlets and the fly likes Can we define a batch job to be called from a servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can we define a batch job to be called from a servlet?" Watch "Can we define a batch job to be called from a servlet?" New topic
Author

Can we define a batch job to be called from a servlet?

Deepak Shankar
Greenhorn

Joined: Dec 14, 2005
Posts: 12
Hi,

I have an requirement where I want to execute some job every 1 hr. My application is a web project deployed in Websphere App server. I tried getting information on scheduler service but was not able to get enough information.

Now as a quick fix, I am making the call from a servlet (loaded at server startup). This servlet will call a thread which will execute the job every 1 hr. Though I have done it as a quick fix I am not sure if it could cause some problem.

Servlet class
.................

public class BootstrapServlet extends HttpServlet {
public void init() throws ServletException {
EJBCallScheduler ejbScheduler = new EJBCallScheduler(5);
// EJBCallScheduler will call Timer thread.
// The thread will do the job everz 1 hour
}
}

The issue I feel is BootstrapServlet's init method will never be completed and will be hanging through out the application lifetime. Please provide your suggestion and help me.
[ December 16, 2005: Message edited by: Deepak Shankar ]
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Another option being that you can use the Timer class to take care of scheduling and you can create a task which will actually do the work which should be done when 1 hr has been timed out.

Although by creating thread you will have more control, Timer class is best suited for scheduling the tasks. Obvious advantage being that, with one Thread you can schedule multiple tasks. However be noted that when a task is executed, it should complete it as soon as possible otherwise there is chance that subsequent (timedout) task's notification may get delayed.

In your posting, I didn't quite catch "The issue I feel is BootstrapServlet's init method will never be completed and will be hanging through out the application lifetime". How can this happen? Because when you call thread's start() method, you will be returned back the execution control immediately, which implies init() method execution would be completed.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Deepak Shankar:
The issue I feel is BootstrapServlet's init method will never be completed and will be hanging through out the application lifetime. Please provide your suggestion and help me.


No no. Just invoke a thread, for that batch processing, and let your main thread continue to process in a usual manner.
Deepak Shankar
Greenhorn

Joined: Dec 14, 2005
Posts: 12
Santhosh Kumar/Adeel Ansari,
Thanks for your reply. It really helped me.

For some reason I thought that if I call a thread from init method, the execution will not return to the method. This is why I assumed that the main thread will be hanging. I guess I was wrong. Thanks for clarification.

Regards,
Deepak
[ December 16, 2005: Message edited by: Deepak Shankar ]
Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 196
Hi Santosh,

You recommended the Timer class, but is that really a good choice for a J2EE application server? The threads that the Timer class creates are not managed by the application server, can this lead to any issues?

Thanks,
Jehan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we define a batch job to be called from a servlet?
 
Similar Threads
doubt in servlet
Kron Job using Servlets
put Vector in run(), and update him
How to schedule a recurring job in Websphere?
Custom Authentication & Authorization