Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Building Applications with Scala
Source

Play framework and high CPU usage

If you notice your CPU usage going higher than it should, do not worry; there is a fix for it. Actually, the issue is related to SBT. Just make sure that, when you run Activator, you pass the following parameter:

$ activator -Dsbt.task.forcegc=false.

Going back to ReactiveWebStore, let's create new routes. Open

ReactiveWebStore/conf/routes:

GET /rnd/double

controllers.RndDoubleGeneratorController.rndDouble

GET /rnd/call controllers.RndDoubleGeneratorController.rndCall

GET /rnd/rx controllers.RndDoubleGeneratorController.rxCall

GET /rnd/rxbat

controllers.RndDoubleGeneratorController.rxScalaCallBatch

Once we have the new routes, we need to create the new controller. This controller needs to be located with the other controllers at ReactiveWebStore/app/controllers.

RndDoubleGeneratorController

The RndDoubleGeneratorController class file should look like this:

@Singleton

class RndDoubleGeneratorController @Inject() (service:IRndService) extends Controller {

import play.api.libs.concurrent.Execution.

Implicits.defaultContext

def rndDouble = Action { implicit request =>

Ok( service.next().toString() )

}

def rndCall = Action.async { implicit request => service.call().map { res => Ok(res) }

}

def rxCall = Action { implicit request =>

Ok(service.rxScalaCall().toBlocking.first.toString())

}

def rxScalaCallBatch = Action { implicit request =>

Ok(service.rxScalaCallBatch().toBlocking.first.toString())

}

}

All methods in the preceding controller call the service IRndService. All operations in RndService call ng-microservice. Here we have some flavors of operations, which will be covered in great detail next when we explore the service implementation.

There are some interesting things here: for instance, for the second operation called rndCall, we see Action.async in use, which means our controller will return a Future, and this Future comes from the service. We also execute a Map to transform the result into an Ok.

The last operation called rxScalaCallBatch is the most interesting, and the one we will be using for our UI. However, you can use the other one if you wish, since they all return doubles, and that's good.

 
Source
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Mathematics
Political science
Philosophy
Psychology
Religion
Sociology
Travel