Migrating Play framework 2.4 chunked response to play 2.5/2.6 akka stream Source

﹥>﹥吖頭↗ 提交于 2020-01-25 08:13:33

问题


My goal is to migrate from java play 2.4 Chunked response to akka source.

Essentially:

public Result getDCcsv() {

    response().setHeader("Content-Type", "text/csv");
    response().setHeader("Content-Disposition", "attachment;filename=users.csv");
    response().setHeader("Cache-control", "private");

    return ok(new Results.Chunks<String>() {
        public void onReady(Results.Chunks.Out<String> out) {
             //blocking calls to read data from DB in manageable chunks
             out.write(data)
        }
    });
}

to

public Result getDCcsv() {

    response().setHeader("Content-Type", "text/csv");
    response().setHeader("Content-Disposition", "attachment;filename=users.csv");
    response().setHeader("Cache-control", "private");

    return new StatusHeader(OK).chunked(Source....)
}

The query returns a large data set, so it was nescessary in the past to use the chunked responses so it wouldn't time out. The callback is pretty simple it calls the same query using ID > iterator and limit X. Remembering the iterator after each step. Also, converting the data to CSV.

Few things of note:

-The object with callback has internal state
-It makes blocking calls

Now, I read the play 2.5 migration guide here:

https://www.playframework.com/documentation/2.6.x/StreamsMigration25 https://www.playframework.com/documentation/2.6.x/StreamsMigration25#Migrating-Enumerators-to-Sources

But I am too dense to make the connection from my current Results.Chunks implementation to enumerators.

I also went thru:

https://doc.akka.io/docs/akka/2.5.3/scala/stream/stream-flows-and-basics.html#core-concepts

But I am not much wiser.

Is there an easy way to have a stateful callback method that creates the akka.Source ?

来源:https://stackoverflow.com/questions/59360044/migrating-play-framework-2-4-chunked-response-to-play-2-5-2-6-akka-stream-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!