如何处理来自AJAX调用的异步Java调用

时间:2022-12-04 00:19:07

I want to make an AJAX call to my Java webapp. The Java webapp will in turn make an asynchronous return call elsewhere. The result of that call will then be returned as the result of AJAX request.

我想对我的Java webapp进行AJAX调用。 Java webapp将在其他地方进行异步返回调用。然后,作为AJAX请求的结果,将返回该调用的结果。

The crux of my question is what would I do with the HttpRequest whilst I'm waiting for the second call to return?

我的问题的关键是我在等待第二次回电话时我将如何处理HttpRequest?

Do I just block and wait for the call within the AJAX handler method or do I store the request somewhere and wait for a callback? How would I handle errors / timeouts?

我只是在AJAX处理程序方法中阻塞并等待调用,还是将请求存储在某处并等待回调?我该如何处理错误/超时?

For those who care further information as to how I arrived at this situation follows:

对于那些关心我如何到达这种情况的进一步信息的人如下:

This is part of an XMPP based instant messaging system. There is one global support user which is displayed as an icon on every page in our webapp. I also want to display the presence of this user, so, I could just use the IM system to request this users presence on every single page load for every user and eventually DDOS myself. Instead I want to have a single user query the presence from the webapp periodically and cache the result.

这是基于XMPP的即时消息系统的一部分。有一个全局支持用户,它在我们的webapp中的每个页面上显示为一个图标。我还希望显示此用户的存在,因此,我可以使用IM系统在每个用户的每个页面加载上请求此用户,并最终自己请求DDOS。相反,我想让一个用户定期查询webapp的存在并缓存结果。

The AJAX call is therefore to the server which will then either return the cached presence or query the XMPP server asynchronously.

因此,AJAX调用是服务器,然后服务器将返回缓存的存在或异步查询XMPP服务器。

3 个解决方案

#1


0  

You shouldn't have to block and wait for the AJAX call. That is, don't make the call synchronously. What you should do on the Java side is figure out a way to block while you wait for the response to come back from your asynchronous call (i.e., figure out to a way to make the request synchronously. The performance hit will be on the first call for any new data. Subsequent calls will hit the cache, so you should be good). You can maintain a cache for this data, so you can check the cache first to see if the data exists. If it doesn't make the call and store the result in the cache. Otherwise, grab the data from the cache and send it back to the view. Since AJAX is asynchronous, your callback will be called as soon as the data comes back from the server.

你不应该阻止并等待AJAX​​调用。也就是说,不要同步拨打电话。你应该在Java方面做的是找出一种阻止等待响应从异步调用返回的方法(即,找出一种同步发出请求的方法。性能命中将在第一次调用任何新数据。后续调用将进入缓存,所以你应该很好)。您可以为此数据维护缓存,因此可以先检查缓存以查看数据是否存在。如果它没有进行调用并将结果存储在缓存中。否则,从缓存中获取数据并将其发送回视图。由于AJAX是异步的,因此只要数据从服务器返回,就会调用回调。

#2


0  

here is what i would do:

这是我会做的:

  • when the page startup, init an job to retrieve data array you need for that specific page, you need to identify the job and the job result for later usage
  • 当页面启动时,初始化一个作业以检索该特定页面所需的数据数组,您需要识别作业和作业结果以供以后使用
  • use ajax from the page to poll for the job result, once the job is done, the poll finishes and returned with data
  • 使用页面中的ajax轮询作业结果,作业完成后,轮询结束并返回数据
  • cache the entries you have requested as Vivin indicated
  • 缓存您所请求的条目,如Vivin所示
  • cache the job result on your server and give it a time-out option
  • 将作业结果缓存在您的服务器上并为其提供超时选项

#3


0  

HTTP requests, i.e. HttpServletRequest objects are not serializable. Therefore you cannot store them in a persistent store of any sort, for the duration of the call. It doesn't make sense anyway to store the request, for its life is limited to the duration of the HTTP request itself, given the stateless nature of the HTTP protocol.

HTTP请求,即HttpServletRequest对象不可序列化。因此,在调用期间,您无法将它们存储在任何类型的持久存储中。无论如何,存储请求是没有意义的,因为它的生命仅限于HTTP请求本身的持续时间,给定HTTP协议的无状态特性。

This effectively means that you have to hold on to the HttpServletResponse object for the duration of the call. The HttpServletRequest object is no longer needed, once the parsing of the HTTP request is performed, and once all the data is available to your application; it is the response object that is of importance in your context.

这实际上意味着您必须在调用期间保持HttpServletResponse对象。一旦执行了HTTP请求的解析,并且一旦所有数据都可用于您的应用程序,就不再需要HttpServletRequest对象;它是您的上下文中重要的响应对象。

The response could be populated with the cached copy of the user status. If the copy in the cache is stale, you might want to refresh it synchronously from the XMPP server (after all, it affects the performance of just one page load). You could query asynchronously from within the application server, but some result must be returned to the browser (so there might be a few edges cases that need to be taken care of).

可以使用用户状态的缓存副本填充响应。如果缓存中的副本是陈旧的,您可能希望从XMPP服务器同步刷新它(毕竟,它会影响一个页面加载的性能)。您可以从应用程序服务器中异步查询,但必须将某些结果返回给浏览器(因此可能需要处理一些边缘情况)。

#1


0  

You shouldn't have to block and wait for the AJAX call. That is, don't make the call synchronously. What you should do on the Java side is figure out a way to block while you wait for the response to come back from your asynchronous call (i.e., figure out to a way to make the request synchronously. The performance hit will be on the first call for any new data. Subsequent calls will hit the cache, so you should be good). You can maintain a cache for this data, so you can check the cache first to see if the data exists. If it doesn't make the call and store the result in the cache. Otherwise, grab the data from the cache and send it back to the view. Since AJAX is asynchronous, your callback will be called as soon as the data comes back from the server.

你不应该阻止并等待AJAX​​调用。也就是说,不要同步拨打电话。你应该在Java方面做的是找出一种阻止等待响应从异步调用返回的方法(即,找出一种同步发出请求的方法。性能命中将在第一次调用任何新数据。后续调用将进入缓存,所以你应该很好)。您可以为此数据维护缓存,因此可以先检查缓存以查看数据是否存在。如果它没有进行调用并将结果存储在缓存中。否则,从缓存中获取数据并将其发送回视图。由于AJAX是异步的,因此只要数据从服务器返回,就会调用回调。

#2


0  

here is what i would do:

这是我会做的:

  • when the page startup, init an job to retrieve data array you need for that specific page, you need to identify the job and the job result for later usage
  • 当页面启动时,初始化一个作业以检索该特定页面所需的数据数组,您需要识别作业和作业结果以供以后使用
  • use ajax from the page to poll for the job result, once the job is done, the poll finishes and returned with data
  • 使用页面中的ajax轮询作业结果,作业完成后,轮询结束并返回数据
  • cache the entries you have requested as Vivin indicated
  • 缓存您所请求的条目,如Vivin所示
  • cache the job result on your server and give it a time-out option
  • 将作业结果缓存在您的服务器上并为其提供超时选项

#3


0  

HTTP requests, i.e. HttpServletRequest objects are not serializable. Therefore you cannot store them in a persistent store of any sort, for the duration of the call. It doesn't make sense anyway to store the request, for its life is limited to the duration of the HTTP request itself, given the stateless nature of the HTTP protocol.

HTTP请求,即HttpServletRequest对象不可序列化。因此,在调用期间,您无法将它们存储在任何类型的持久存储中。无论如何,存储请求是没有意义的,因为它的生命仅限于HTTP请求本身的持续时间,给定HTTP协议的无状态特性。

This effectively means that you have to hold on to the HttpServletResponse object for the duration of the call. The HttpServletRequest object is no longer needed, once the parsing of the HTTP request is performed, and once all the data is available to your application; it is the response object that is of importance in your context.

这实际上意味着您必须在调用期间保持HttpServletResponse对象。一旦执行了HTTP请求的解析,并且一旦所有数据都可用于您的应用程序,就不再需要HttpServletRequest对象;它是您的上下文中重要的响应对象。

The response could be populated with the cached copy of the user status. If the copy in the cache is stale, you might want to refresh it synchronously from the XMPP server (after all, it affects the performance of just one page load). You could query asynchronously from within the application server, but some result must be returned to the browser (so there might be a few edges cases that need to be taken care of).

可以使用用户状态的缓存副本填充响应。如果缓存中的副本是陈旧的,您可能希望从XMPP服务器同步刷新它(毕竟,它会影响一个页面加载的性能)。您可以从应用程序服务器中异步查询,但必须将某些结果返回给浏览器(因此可能需要处理一些边缘情况)。