| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Akka/Scala example source code file (ReadBackPressure.scala)

This example Akka source code file (ReadBackPressure.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Akka and Scala source code examples by using tags.

All credit for the original source code belongs to; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Akka tags/keywords

ack, actor, actorref, akka, event, io, resumeaccepting, resumereading, test, testing, testkit, testprobe, unit, util, write

The ReadBackPressure.scala Akka example source code

 * Copyright (C) 2009-2014 Typesafe Inc. <>

import{ ActorRef, ActorLogging, Props, Actor, ActorSystem }
import{ Tcp, IO }
import akka.testkit.{ ImplicitSender, TestProbe, AkkaSpec }
import akka.util.ByteString

object PullReadingExample {

  class Listener(monitor: ActorRef) extends Actor {

    import context.system

    override def preStart: Unit =
      IO(Tcp) ! Bind(self, new InetSocketAddress("localhost", 0), pullMode = true)

    def receive = {
      case Bound(localAddress) =>
        // Accept connections one by one
        sender() ! ResumeAccepting(batchSize = 1)
        monitor ! localAddress

    def listening(listener: ActorRef): Receive = {
      case Connected(remote, local) =>
        val handler = context.actorOf(Props(classOf[PullEcho], sender()))
        sender() ! Register(handler, keepOpenOnPeerClosed = true)
        listener ! ResumeAccepting(batchSize = 1)


  case object Ack extends Event

  class PullEcho(connection: ActorRef) extends Actor {

    override def preStart: Unit = connection ! ResumeReading

    def receive = {
      case Received(data) => connection ! Write(data, Ack)
      case Ack            => connection ! ResumeReading


class PullReadingSpec extends AkkaSpec with ImplicitSender {

  "demonstrate pull reading" in {
    val probe = TestProbe()
    system.actorOf(Props(classOf[PullReadingExample.Listener], probe.ref), "server")
    val listenAddress = probe.expectMsgType[InetSocketAddress]

    IO(Tcp) ! Connect(listenAddress, pullMode = true)
    val connection = lastSender

    val client = TestProbe()
    client.send(connection, Register(client.ref))
    client.send(connection, Write(ByteString("hello")))
    client.send(connection, ResumeReading)


Other Akka source code examples

Here is a short list of links related to this Akka ReadBackPressure.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.