问题
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