Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Deepak Shankar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic