1

I've been doing some research about cancelling ajax requests, and I've read several threads here about this topic. I found this very informative topic:

Abort Ajax requests using jQuery

According to it, when you use abort() method, the request is cancelled on the client side, but not necessarily on server side. Thing is my request demands some heavy calculations, and I would like to be able to stop on both client and server side, if it starts taking too long. Is there any way I could do that?

ps: timeouts on the request wouldn't help me here. I'm using JSF for calling a bean that calls an optimization algorithm, and I'd like to stop the algorithm on demand, if possible.

Community
  • 1
  • 1
Tinadm
  • 359
  • 3
  • 16

1 Answers1

0

To have this happen reliably you need to make the code with heavy calculations act like a long-running task that processes in the background. Your request then would issue the task to run with a tie back to that user's session. To cancel execution, then, would require a different request. This could be done with a timer on the client side after the initiating request comes back, if it is to occur after a set period of time, or it could be user-initiated with a button or whatever.

Another option is to have the task itself keep track of its time, and abort it on its own after a set period of time. You could pass in the required time as a parameter or whatever works best, and throw an exception if more time has elapsed. Then, your server-side code would try/catch the routine and respond accordingly to each scenario.

DMac the Destroyer
  • 5,240
  • 6
  • 36
  • 56
  • That's a good idea. I'll try to make the first request just create a new thread and start the calculations. The second request, as you said, could be triggered by a button or whatever, setting a flag to stop the thread. As soon as I finish, I post here the results. – Tinadm Dec 05 '12 at 15:53
  • It seems that creating another task to run the calculations as I said in the previously comment kinda works. But I've run into another issue: if the user does not cancel the task and it ends properly, how can I make it notify my application that it has ended, since my FacesContext instance is no longer valid? I mean, how can I tie my task to the user's session as you suggested? – Tinadm Dec 05 '12 at 18:20
  • That's an entirely different project altogether... we're a .net shop, but we use a C# port of [Quartz](http://quartz-scheduler.org/) to programmatically schedule long-running tasks and store progess info in the database when appropriate. You'll ultimately end up with three endpoints: one for initiating the task, another for monitoring progress of said task, and a third to optionally cancel the task. It's not an "easy" solution, but I've found in just about all web application projects I've started, the ability to fire off long-running background tasks has far-reaching benefits. – DMac the Destroyer Dec 05 '12 at 18:27
  • If all of that sounds a bit overwhelming right now, you should think about my second suggestion. It's not as elegant, but it's much simpler and should work for your specific set of current requirements – DMac the Destroyer Dec 07 '12 at 22:15
  • I think I'll do as you said about creating separate tasks for calculations and tasks for monitoring the process. That indeed is not that elegant, but should do the job. Thanks for the help! – Tinadm Dec 08 '12 at 22:30