简体   繁体   中英

Activate a Java Servlet from another Java Servlet

My problem is this:
Google App Engine allows cron jobs to be active for only 30 seconds before an DeadlineExceededException is thrown. And my app that isn't suited for the google app engine platform from this point of view, needs to call a time-consuming cron job.
One solution that I figured out was to calling another Servlet (Servlet2) and let that Servlet2 do the job for me, Servlet_2 would be a regular Java Servlet.
To achieve that, I was thinking of creating a session from my cron job Servlet_1, call the other Servlet_2, test the session and then let the server do the jobs required and in the end invalidate the session.
The call from Servlet_1 should not be redirecting to Servlet_2, because that will put me back in square one again.
Now to my question: Do you think this will work? And if yes and an DeadlineExceededException acure, would the Servlet_2 stop from working as well, even if I put all the code in the destroy method of the Servlet_2?
my code:


//Servlet_1
try {
   HttpSession session = request.getSession(true);
   session.setAttribute("referingPage", "server is calling");
   request.getRequestDispatcher("/Servlet_2.do").forward(request, response);
}catch(DeadlineExceededException e) {
   e.printStackTrace();
}
 //Servlet_2 @Override public void destroy() { HttpSession session = request.getSession(true); String value = (String)session.getAttribute("referringPage"); if(value.equals("server is calling")) { // Do the time demanding stuff } session.invalidate(); } 

Would be grateful for an answer!

Why not use a task queue. You put a task on the queue - it works for 29 seconds and then stops but, before it stops, it puts another task on the queue. As long as the payload has a marker to indicate where to restart then you have a chained set of tasks that can run for as long as you want to consume (and pay for) CPU.

You only have 30 seconds to produce finish execution and there's no way around that. Doing a forward doesn't spawn a new thread or anything, it's still executing within the 30 second time limit.

I'd try to figure out some way to serialize what you're doing, or pause it and stick your state in memcache. When you start processing again, check memcache to see if you need to pick up form where you left off.

Google App Engine is working on long running background processes, and I hope then come out with a solution soon. I'm in the same boat.

Mark

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM