Hmm, another blog entry on the Play framework. You must be sooo surprised!
Play allows you to suspend requests while doing heavy computation – this frees up your application to continue processing new requests, regardless of how long the computation takes. However, you may be deploying to an environment with a fixed request timeout – in the case that prompted me to come up with this code, I was deploying to a server that required requests to return within 120 seconds.
When generating PDFs, Excel files, images or whatever, that may take longer than the available window I needed to be able to push the computation into the background using a job. This solves the immediate problem, but leads to another issue – what if the job actually completes in a couple of seconds? I would then be forcing the user to then go to another link to download the generated content. Not so good for the user experience.
A third way was clearly needed, and this is the solution I came up with:
1. All file generation computation runs in a job. This job is responsible for saving the content into the database.
2. A promise is requested from the job, and given a certain amount of time to complete in:
F.Promise promise = downloadJob.now();
UserContent userContent = promise.get(5,TimeUnit.SECONDS);
3. If the object returned from the promise is not null, it’s sent to the browser. If it is null, a message is returned to the browser indicating the generation is on-going.
if (userContent != null)
{
...
renderBinary(new ByteArrayInputStream(userContent.content.byteContent),
userContent.fileName,
userContent.content.byteContent.length,
MimeTypes.getMimeType(userContent.fileName),
false);
}
else
{
flash.success("Content is being generated in the background");
index();
}
4. A download manager is provided to download any generated content generated (even content that returned a binary response).
5. A job returns every night to remove any generated content older than a week
The result is an application that will guarantee to return within the alloted timeframe, and also allows users to keep working while their content is generated asynchronously. It also reduces concurrent load on the server because the generation job is executed from within the jobs pool, which can be limited using the application.conf.
An example implementation can be found here: variable-downloads. The only thing missing (apart from comments – it’s pretty self-explanatory) is polling of the server to update the downloads table when a new download is available.
代码下载
分享到:
相关推荐
"synchronicity"(同步性)是心理学家荣格提出的一个概念,指的是一种非因果性的巧合事件,这些事件看似偶然,却似乎蕴含着深层的心理意义。荣格认为,同步性可能与集体无意识有关,与《易经》的某些观念有所契合,...
同步性 与看不见的事物联系在一起的连接原理。 几乎不可察觉。 无法表达的东西。 —警察,同步性I 分布式构建系统通过拜占庭容错(BFT)共识提供... 通过使用基于可复制构建的系统, Synchronicity还可以使用易于
资源来自pypi官网。 资源全名:synchronicity-0.0.1-py3-none-any.whl
创建同步性是一个简单,快速且功能强大的备份应用程序。 它可以同步文件和文件夹,具有简洁的界面,并且可以安排备份以确保数据安全。 另外,它是开源,便携式,多语言且非常轻巧(210kB)。
* Synchronicity:同步时也能玩 iphone,当然啦,iOS5 就不需要了,系统自带这个功能了,iOS4 的用户需要这个。 * Fakecarrier:能自由修改左上角的运营商文字,仅仅能修改文字或者加上 Emoji 表情哦,不能改成图标...
Keep track of time directly from the computer screen on this great, magical hours and listen to the cuckoo bird's song every half hour and hour. Enjoy excellent picture and sound and feel the magic ...
Python 3对异步编程提供了惊人的支持,但是可以说它使开发库变得有些困难。 您是否厌倦了实现同步和异步方法做基本相同的事情? 对于您来说,这可能是一个简单的解决方案。 背景:为什么需要这样的东西 ...
Deferred execution 35 process.nextTick 36 Timers 38 setTimeout 38 setInterval 39 unref and ref 40Table of Contents [ ii ] Understanding the event loop 41 Four sources of truth 42 Callbacks and errors ...
- Adaplet®、ComplianceConnect®、DesignSync®、ENOVIA®、MatrixOne®、ProjectSync®、Synchronicity® 和 TeamCentral® 是达索系统的注册商标。 - ENOVIA Live Collaboration、ENOVIA Live Collaboration ...
"Synchronicity Commerce"可能是一个设计概念,强调模板的商业应用中,视觉效果与商业策略的同步一致性。 【部分内容】提到了"SynchronicityCommerce SF Font",这可能是一种特定的字体,用于增强模板的专业性和...
同步性(synchronicity)是另一个关键指标,特别是在需要精确时间同步的应用中,如雷达系统,确保接收器在正确的时间接收到脉冲信号以获得清晰图像。 文章中提到了几种不同类型的原子钟技术,包括铯芯片级原子钟...