`

Semi-synchronicity: Deferred downloads based on time

阅读更多
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.

代码下载
分享到:
评论

相关推荐

    i-ching:易经或易经

    "synchronicity"(同步性)是心理学家荣格提出的一个概念,指的是一种非因果性的巧合事件,这些事件看似偶然,却似乎蕴含着深层的心理意义。荣格认为,同步性可能与集体无意识有关,与《易经》的某些观念有所契合,...

    synchronicity:分布式构建系统通过拜占庭容错(BFT)共识提供加密的可复制性证明

    同步性 与看不见的事物联系在一起的连接原理。 几乎不可察觉。 无法表达的东西。 —警察,同步性I 分布式构建系统通过拜占庭容错(BFT)共识提供... 通过使用基于可复制构建的系统, Synchronicity还可以使用易于

    PyPI 官网下载 | synchronicity-0.0.1-py3-none-any.whl

    资源来自pypi官网。 资源全名:synchronicity-0.0.1-py3-none-any.whl

    Create Synchronicity - Backup & Sync:适用于Windows的简单,快速,轻量级备份和同步应用程序-开源

    创建同步性是一个简单,快速且功能强大的备份应用程序。 它可以同步文件和文件夹,具有简洁的界面,并且可以安排备份以确保数据安全。 另外,它是开源,便携式,多语言且非常轻巧(210kB)。

    iPhone越狱后必装的插件(超多)

    * Synchronicity:同步时也能玩 iphone,当然啦,iOS5 就不需要了,系统自带这个功能了,iOS4 的用户需要这个。 * Fakecarrier:能自由修改左上角的运营商文字,仅仅能修改文字或者加上 Emoji 表情哦,不能改成图标...

    [3planesoft屏幕保护程序合集].Premium.3D.Screensavers.iso

    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 ...

    synchronicity

    Python 3对异步编程提供了惊人的支持,但是可以说它使开发库变得有些困难。 您是否厌倦了实现同步和异步方法做基本相同的事情? 对于您来说,这可能是一个简单的解决方案。 背景:为什么需要这样的东西 ...

    [Mastering.Node.js(2013.11) 精通Node.js

    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 ...

    matrixone/ematrix开发手册

    - Adaplet®、ComplianceConnect®、DesignSync®、ENOVIA®、MatrixOne®、ProjectSync®、Synchronicity® 和 TeamCentral® 是达索系统的注册商标。 - ENOVIA Live Collaboration、ENOVIA Live Collaboration ...

    powerpoint模版气球与天空主题synchroPPT模板.ppt

    "Synchronicity Commerce"可能是一个设计概念,强调模板的商业应用中,视觉效果与商业策略的同步一致性。 【部分内容】提到了"SynchronicityCommerce SF Font",这可能是一种特定的字体,用于增强模板的专业性和...

    使用微型原子钟可以使依赖GPS的设备按比例缩小.pdf

    同步性(synchronicity)是另一个关键指标,特别是在需要精确时间同步的应用中,如雷达系统,确保接收器在正确的时间接收到脉冲信号以获得清晰图像。 文章中提到了几种不同类型的原子钟技术,包括铯芯片级原子钟...

Global site tag (gtag.js) - Google Analytics