服务器端代码在当前线程忙时将数据推送到客户端浏览器Comet(编程)

时间:2021-11-27 16:32:28

I am writing one simple web page with bunch of textboxes and a button control. Now when user finished editing the values on this text boxes user has to click the button and this button invoke heavily process intensive algorithm on server side code based on the data received from client (Textboxes)

我正在编写一个带有大量文本框和按钮控件的简单网页。现在,当用户完成编辑此文本框上的值时,用户必须单击按钮,此按钮根据从客户端(文本框)接收的数据调用服务器端代码上的过程密集型算法

And it could some time takes up to 30 to 45 minutes to complete the whole operation so the current thread is still inside the button click event handler function.

并且它可能需要30到45分钟来完成整个操作,因此当前线程仍然在按钮单击事件处理函数内。

That background task only provides one event, and the web page subscribes to it to get some text data after each stage of processing

该后台任务仅提供一个事件,并且网页订阅它以在每个处理阶段之后获得一些文本数据

I was wandering if there is any way I can keep user up-to-date with what is the current progress on that background task. I have div element to print the current status information

如果有任何方法可以让用户及时了解该后台任务的当前进度,我就会徘徊。我有div元素来打印当前的状态信息

So I am looking for some sort of reverse mechanism then "get" and "post".

所以我正在寻找某种反向机制,然后“获取”和“发布”。

I have read some articles on the Comet (programming) but I can't find any easy or definitive answer

我已经阅读了一些有关Comet(编程)的文章,但我找不到任何简单或明确的答案

Thanks in advance

提前致谢

4 个解决方案

#1


2  

Perhaps the simplest way is to submit the job, and get an id back via a straightforward page POST. That id corresponds to the long running job on the server side.

也许最简单的方法是提交作业,并通过简单的POST页面获取id。该id对应于服务器端长时间运行的作业。

The returned page can then automatically refresh via the HTTP meta refresh mechanism, and the server can return status etc. in the refreshed page. The page will continually refresh with the id every (say) 30s until the job is complete.

然后,返回的页面可以通过HTTP元刷新机制自动刷新,服务器可以在刷新的页面中返回状态等。每隔(比如说)30秒,页面将不断刷新id,直到作业完成。

It's a brute-force mechanism, but it's straightforward. Plus it has the advantage of allowing the user to bookmark the page and go away/come back. Given that your job is running for 30/45 mins, that could be important.

这是一种蛮力机制,但它很简单。此外,它还具有允许用户为页面添加书签并离开/返回的优点。鉴于你的工作正在运行30/45分钟,这可能很重要。

#2


2  

You are on the right track with 'Comet' (aka Ajax-Push or reverse-Ajax) - Comet is essentially an umbrella term for the ability for the server to 'push' something back to the client without the client triggering the event. Some Ajax frameworks are starting to build this in, and it is not something i would suggest trying to implement yourself close to the metal, because there are a lot of different things to consider (different webapp servers, threading models, etc). You can see that * has some of this functionality, when it notifies you of a new answer coming in if you are writing one yourself for a given question.

你正在使用'Comet'(又名Ajax-Push或反向Ajax)走上正轨 - Comet本质上是一个能够让服务器在没有客户端触发事件的情况下将某些东西“推”回客户端的能力的术语。一些Ajax框架开始构建这个,并且我不建议尝试自己实现接近金属,因为有很多不同的东西需要考虑(不同的webapp服务器,线程模型等)。你可以看到*具有一些这样的功能,如果你自己为一个给定的问题写一个新的答案,它会通知你。

Now all that being said, in your case, given the length of time of the server side processing, I would agree with Brian's answer that you should give the running job an id and use a more simple refresh mechanism to check it. If you wanted to add the look and feel of a server side push, you could query the server via standard ajax on an interval to check if the job is done rather than a simple refresh, and change the page when it is done through that Ajax call. If your job is able to report progress, then standard ajax could refresh your div with that progress, and there is no need for a server side push.

现在所说的一切,在你的情况下,考虑到服务器端处理的时间长度,我同意Brian的回答,你应该给正在运行的作业一个id并使用更简单的刷新机制来检查它。如果您想添加服务器端推送的外观,您可以在一定时间间隔内通过标准ajax查询服务器以检查作业是否已完成而不是简单刷新,并在通过该Ajax完成时更改页面呼叫。如果你的工作能够报告进度,那么标准的ajax可以用这个进程刷新你的div,并且不需要服务器端推送。

#3


1  

I agree completely with Brian Agnew's suggestion, with one possible improvement. Since you're essentially doing server-push operations, it might be worth considering a comet server. Now, I say that with a caveat - if you're really only managing jobs that complete every 30-45 minutes, then it may well be overkill. However, it has the advantage that you can push the results to the user when they've completed, and if the user is not longer connected, you can do something different (such as send them a notification email).

我完全赞同Brian Agnew的建议,并有一个可能的改进。由于您实际上是在进行服务器推送操作,因此考虑使用彗星服务器可能是值得的。现在,我有一点需要注意 - 如果你真的只管理每30-45分钟完成的工作,那么它可能会有点过分。但是,它的优势在于,您可以在完成后将结果推送给用户,如果用户不再连接,您可以执行不同的操作(例如向他们发送通知电子邮件)。

#4


1  

It depends:

  1. If you need to be instantly notified when the process ends Comet (long polling) is for you
  2. 如果您需要在流程结束时立即通知Comet(长轮询)适合您

  3. If some delay is acceptable (for instance the system notifies you 1 minute after the process finishes) then AJAX timers (polling) is your you.
  4. 如果某些延迟是可以接受的(例如系统在程序结束后1分钟通知您),那么AJAX计时器(轮询)就是你的。

Take a look to three examples of both techniques based on ItsNat framework:

看看基于ItsNat框架的两种技术的三个例子:

  1. Comet
  2. AJAX Timers
  3. Asynchronous Tasks (similar to Comet, one shot)
  4. 异步任务(类似于Comet,一次性)

I'm sorry, is Java

对不起,是Java

#1


2  

Perhaps the simplest way is to submit the job, and get an id back via a straightforward page POST. That id corresponds to the long running job on the server side.

也许最简单的方法是提交作业,并通过简单的POST页面获取id。该id对应于服务器端长时间运行的作业。

The returned page can then automatically refresh via the HTTP meta refresh mechanism, and the server can return status etc. in the refreshed page. The page will continually refresh with the id every (say) 30s until the job is complete.

然后,返回的页面可以通过HTTP元刷新机制自动刷新,服务器可以在刷新的页面中返回状态等。每隔(比如说)30秒,页面将不断刷新id,直到作业完成。

It's a brute-force mechanism, but it's straightforward. Plus it has the advantage of allowing the user to bookmark the page and go away/come back. Given that your job is running for 30/45 mins, that could be important.

这是一种蛮力机制,但它很简单。此外,它还具有允许用户为页面添加书签并离开/返回的优点。鉴于你的工作正在运行30/45分钟,这可能很重要。

#2


2  

You are on the right track with 'Comet' (aka Ajax-Push or reverse-Ajax) - Comet is essentially an umbrella term for the ability for the server to 'push' something back to the client without the client triggering the event. Some Ajax frameworks are starting to build this in, and it is not something i would suggest trying to implement yourself close to the metal, because there are a lot of different things to consider (different webapp servers, threading models, etc). You can see that * has some of this functionality, when it notifies you of a new answer coming in if you are writing one yourself for a given question.

你正在使用'Comet'(又名Ajax-Push或反向Ajax)走上正轨 - Comet本质上是一个能够让服务器在没有客户端触发事件的情况下将某些东西“推”回客户端的能力的术语。一些Ajax框架开始构建这个,并且我不建议尝试自己实现接近金属,因为有很多不同的东西需要考虑(不同的webapp服务器,线程模型等)。你可以看到*具有一些这样的功能,如果你自己为一个给定的问题写一个新的答案,它会通知你。

Now all that being said, in your case, given the length of time of the server side processing, I would agree with Brian's answer that you should give the running job an id and use a more simple refresh mechanism to check it. If you wanted to add the look and feel of a server side push, you could query the server via standard ajax on an interval to check if the job is done rather than a simple refresh, and change the page when it is done through that Ajax call. If your job is able to report progress, then standard ajax could refresh your div with that progress, and there is no need for a server side push.

现在所说的一切,在你的情况下,考虑到服务器端处理的时间长度,我同意Brian的回答,你应该给正在运行的作业一个id并使用更简单的刷新机制来检查它。如果您想添加服务器端推送的外观,您可以在一定时间间隔内通过标准ajax查询服务器以检查作业是否已完成而不是简单刷新,并在通过该Ajax完成时更改页面呼叫。如果你的工作能够报告进度,那么标准的ajax可以用这个进程刷新你的div,并且不需要服务器端推送。

#3


1  

I agree completely with Brian Agnew's suggestion, with one possible improvement. Since you're essentially doing server-push operations, it might be worth considering a comet server. Now, I say that with a caveat - if you're really only managing jobs that complete every 30-45 minutes, then it may well be overkill. However, it has the advantage that you can push the results to the user when they've completed, and if the user is not longer connected, you can do something different (such as send them a notification email).

我完全赞同Brian Agnew的建议,并有一个可能的改进。由于您实际上是在进行服务器推送操作,因此考虑使用彗星服务器可能是值得的。现在,我有一点需要注意 - 如果你真的只管理每30-45分钟完成的工作,那么它可能会有点过分。但是,它的优势在于,您可以在完成后将结果推送给用户,如果用户不再连接,您可以执行不同的操作(例如向他们发送通知电子邮件)。

#4


1  

It depends:

  1. If you need to be instantly notified when the process ends Comet (long polling) is for you
  2. 如果您需要在流程结束时立即通知Comet(长轮询)适合您

  3. If some delay is acceptable (for instance the system notifies you 1 minute after the process finishes) then AJAX timers (polling) is your you.
  4. 如果某些延迟是可以接受的(例如系统在程序结束后1分钟通知您),那么AJAX计时器(轮询)就是你的。

Take a look to three examples of both techniques based on ItsNat framework:

看看基于ItsNat框架的两种技术的三个例子:

  1. Comet
  2. AJAX Timers
  3. Asynchronous Tasks (similar to Comet, one shot)
  4. 异步任务(类似于Comet,一次性)

I'm sorry, is Java

对不起,是Java