写GAE数据存储异步 [英] Write to GAE datastore asynchronously

查看:125
本文介绍了写GAE数据存储异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Java应用程序,有时候我做用户需要的数据存储写一些工作,但我不想让用户在数据存储区写入等待。我想立即返回到用户的响应,而数据被存储在背景

In my Java app, sometimes my users do some work that requires a datastore write, but I don't want to keep the user waiting while the datastore is writing. I want to immediately return a response to the user while the data is stored in the background.

这似乎相当清楚,我可以通过使用GAE任务队列,入队来存储的数据的任务,做到这一点。但我也看到有一个异步数据存储API,这似乎像它会比处理任务队列容易得多。

It seems fairly clear that I could do this by using GAE task queues, enqueueing a task to store the data. But I also see that there's an Async datastore API, which seems like it would be much easier than dealing with task queues.

我可以只调用AsyncDatastoreService.put(),然后从我的servlet回报?将在该API存储没有让我的用户在等待我的数据?

Can I just call AsyncDatastoreService.put() and then return from my servlet? Will that API store my data without keeping my users waiting?

推荐答案

我认为你是对的异步调用似乎更容易。但是,文档为AsyncDatastore提一句告诫说,你应该考虑:

I think you are right that the Async calls seem easier. However, the docs for AsyncDatastore mention one caveat that you should consider:

请注意:异常是甩不,直到调用get()方法。调用此方法可以验证异步操作成功了。

Note: Exceptions are not thrown until you call the get() method. Calling this method allows you to verify that the asynchronous operation succeeded.

获取该说明被调用的异步调用返回的未来对象。如果你只是从你的servlet没有返回Future对象上曾经调用get,你可能不知道为确保您的put()方法是否有效。

The "get" in that note is being called on the Future object returned by the async call. If you just return from your servlet without ever calling get on the Future object, you might not know for sure whether your put() worked.

通过排队的任务,可以处理错误的情况下更明确,或者仅仅依靠自动重发。如果你想队列数据存储回放,你应该能够创建(或寻找​​)一个实用工具类,做大部分的工作适合你。

With a queued task, you can handle the error cases more explicitly, or just rely on the automatic retries. If all you want to queue is datastore puts, you should be able to create (or find) a utility class that does most of the work for you.

这篇关于写GAE数据存储异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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