如何处理长AJAX请求 - 发送一个响应,但继续工作 [英] How to handle long AJAX requests - send a response but continue to work

查看:144
本文介绍了如何处理长AJAX请求 - 发送一个响应,但继续工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们是从浏览器发送的指令集将使用jQuery阿贾克斯多层Web应用程序。第一成分(A成分,用C#编写)验证和持续的指示到数据库中,然后再从相同的检索它们之后调用该操作上的说明,第二部分(B组分,通过HTTP用Java编写的,也被称为)数据库。浏览器客户端只是投票通过成份A中的RDBMS表提交作业,所以它发送的请求,不等待来自A组份的响应后,有效地切断后。

We are sending in instruction sets from a browser into a multi-tier web app using jquery-ajax. The first component (component A, written in C#) validates and persists the instructions into a database, and then calls the second component (component B, written in Java and also called via HTTP) which operates on the instructions after retrieving them from the same database. The browser client just polls the rdbms table via component A after submitting the job, so it is effectively disconnected after sending in the request and doesn't wait for a response from component A.

这是对中间层的最佳方式(A成分),返回成功消息到客户端确认一个成功提交的任务,但随后仍发出请求到任务的处理程序(B成分)和释放所有其资源?返回的响应的页面完成操作,所以我们不得不做一些事情在另一个线程中发送此回浏览器。

What is the best way for the middle-tier (component A) to return a success message to the client acknowledging a successful submission of the task, but then still make the request to the task handler (component B) and releasing all of its resources? Returning a response is a final action for the page so we'd have to do something in another thread before sending this back to the browser.

我们已经考虑另一种选择是要做到这一点的B组分,其中任务处理程序发回给中间层的立即响应确认的要求,但随后继续在后台运行。唯一的区别是,我们产卵额外的线程来完成工作。

Another option we have considered is for this to happen in component B where the task handler sends back an immediate response to the middle tier acknowledging the request, but then continues on working in the background. The only difference would be where we spawn the additional thread to do the work.

在如何处理这有什么好想法?

Any good ideas on how to handle this?

推荐答案

我们所做的情况是这样的:

What we have done in situations like this is:

  • 在浏览器发送指令集,组件A
  • 在A组份验证的指令集
  • 在A组份保存在新的跟踪的ID 状态指令集,DB = PENDING
  • 组件也求B组做工作
  • 在线程是在B组份:
    • 在B组份开始后台线程做的工作,第一件事就是后台线程确实是跟踪ID的更新状态状态=运行
    • 在B组份主线程返回成功成分A
    • Browser send instruction set to component A
    • Component A validates instruction set
    • Component A save instruction set to DB under new "tracking" ID STATUS=PENDING
    • Component make request to component B to do the work
    • "Threading" is in component B:
      • Component B starts background thread to do the work, first thing background thread does is update status of "tracking ID" to STATUS=RUNNING
      • Component B main thread returns to success to component A

      所有的优点是不存在等待的工作,以从视图中,应用程序的点完成。在实际工作由后台线程中完成的,就像你与的nohup dothework和放大器踢它关闭; 在shell。关键是使用DB监视跟踪ID

      The advantage of all that is there is no waiting for the work to finish from you application's point of view. The actual work is done by a "background" thread, just as if you kicked it off with nohup dothework & in a shell. The key is using the DB to monitor the status of the "tracking ID".

      这篇关于如何处理长AJAX请求 - 发送一个响应,但继续工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆