Scala

Throttling in Akka and Spray

Posted on by  
Tammo Sminia

When you want to limit the amount of messages an actor gets, you can use the throttler in akka-contrib. This will let you limit the max transactions per second(tps). It will queue up the surplus. Here I'll describe another way. I'll reject all the surplus messages. This has the advantage that the requester knows it's sending too much and can act on that. Both methods have their advantages. And both have limits, since they still require resources to queue or reject the messages. In Akka we can create an Actor that sends messages through to the target actor, or rejects them when it exceeds the specified tps.

object ThrottleActor {
  object OneSecondLater
  object Accepted
  object ExceededMaxTps
}
import ThrottleActor._

class ThrottleActor (target: ActorRef, maxTps: Int) extends Actor with ActorLogging {
  implicit val executionContext: ExecutionContext = context.dispatcher
  context.system.scheduler.schedule(1.second, 1.second, self, OneSecondLater)

  var messagesThisSecond: Int = 0

  def receive = {
    case OneSecondLater =>
      log.info(s"OneSecondLater ${DateTime.now} $messagesThisSecond requests.")
      messagesThisSecond = 0
    case message if messagesThisSecond >= maxTps =>
      sender ! ExceededMaxTps
      messagesThisSecond += 1
      log.info(s"ExceededMaxTps ${DateTime.now} $messagesThisSecond requests.")
    case message =>
      sender ! Accepted
      target ! message
      messagesThisSecond += 1
  }
}

Continue reading →

JSON parsing in Scala

Posted on by  
Tammo Sminia

We can use the Spray JSON parser for uses other than a REST API. We add spray-json to our dependencies. Our build.gradle:

apply plugin: 'scala'
version = '1.0'
repositories {
    mavenCentral()
}
dependencies {
    compile group: 'io.spray', name: 'spray-json_2.11', version: '1.3.1'
}

Continue reading →

Building a REST client with Spray

Posted on by  
Tammo Sminia

In a previous blog I wrote how to make an API. See here.
Now we'll make a client to use that API. This can be done with spray-client. First we add dependencies for spray-client and spray-json:

apply plugin: 'scala'

version = '1.0'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.scala-lang', name: 'scala-library', version: '2.11.6'
    compile group: 'com.typesafe.akka', name: 'akka-actor_2.11', version: '2.3.9'
    compile group: 'com.typesafe.akka', name: 'akka-remote_2.11', version: '2.3.9'
    testCompile group: 'org.scalatest', name: 'scalatest_2.11', version: '2.2.4'
    compile group: 'io.spray', name: 'spray-http_2.11', version: '1.3.3'
    compile group: 'io.spray', name: 'spray-httpx_2.11', version: '1.3.3'
    compile group: 'io.spray', name: 'spray-json_2.11', version: '1.3.1'
    compile group: 'io.spray', name: 'spray-client_2.11', version: '1.3.3'
}

Continue reading →

Building a war with spray-servlet

Posted on by  
Tammo Sminia

We will use spray-servlet to build a war file of our API. So we can run it in a java app server. I assume we already have a working REST API. We will need a web.xml, under src/main/webapp/WEB-INF/:

 spray.servlet.Initializer 

    SprayConnectorServlet
        spray.servlet.Servlet30ConnectorServlet
        true 

    SprayConnectorServlet
        /* 

Continue reading →

shadow-left