How should a Play application be set up to handle requests sequentially at a time?

d-_-b

Suppose I have an application that executes an external command, which cannot be ran in parallel, as follows.

Future("command_to_run".run().exitValue())

The requirement for the application is that it should accepts requests and handle them sequentially by executing the command. The order that the requests are processed is not very important.

What would be the simplest or recommended way to achieve this requirement?

Levi Ramsey

The simplest approach is probably, expanding on Feyyaz's answer, to define a single-thread ExecutionContext:

val singlethread = ExecutionContext.fromExecutorService(Executors.newSingleThreadExecutor())

I recommend not making this context implicit, as you want only the execution of the command to happen in that context.

val execution = Future("command_to_run".run())(singleThread)
execution.map(_.exitValue)  // will execute on the regular execution context in scope

The single thread context will then be in use for no longer than required to run the command and will then be available to serve the next request. In general, it's not a great idea to have

import scala.concurrent.ExecutionContext.Implicits.global

in production code, unless you really know what you're doing (IMO that the compiler suggests this is a horrible misfeature).

It's also worth considering using an Akka actor to synchronize running the command: this will simplify failure handling/cleanup and not require anything special in terms of ExecutionContexts, at the expense of needing to work with the ActorSystem Play supplies.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How should unit tests set up data sources when not running in an application server?

how to play an ObjectAnimator sequentially with a playTogether

How to process the results of a series of AJAX requests sequentially?

How to set up application menu in electron?

How should I Handle duplicate times in time series data with pandas?

Set up a proxy with python requests

Best way to set up account before first time application startup

How should I handle revoking permission in application runtime?

How to Speed up the First time loading time Angular 6 Application

Handle Express requests one at a time

How to handle multiple requests to purchase at same time

How to set up robust Windows time synchronization?

QTP: How to handle a pop up window that shows up at an uncertain time

How to set up a application-wide Timer?

How to handle erros and show up only one time

how to run two requests sequentially in jmeter

How to sincronize requests application android to webservice(REST) in real time?

AS3 How to Set Up A Max Time Count Up

How to set up a nodejs application server in ubuntu

How should I calculate Ramp-up time in Jmeter

How to set up dns cache time?

how to handle "Launch Application" pop up window in firefox using autoIT

Should WebSocket server only handle GET requests?

How should I set up the python packages for this

How to set up a pause for AWS EC2 spot requests

How can I use set up an MKMapView to only zoom in once? Then every time after it should just add annotations without zooming

How would you set up a database to handle comments for a blogging site?

How can I handle multiple requests at the same time in Django?

401 on POST requests, how to set up properly http service?

TOP Ranking

HotTag

Archive