Object

com.thoughtworks.raii

AsynchronousPool

Related Doc: package raii

Permalink

object AsynchronousPool extends Serializable

The namespace that contains the implementation of the asynchronous resource pool.

Source
AsynchronousPool.scala
Example:
  1. Given a factory that creates resources,

    import scalaz.Tags.Parallel
    import scalaz._
    import scalaz.syntax.tag._
    import scalaz.syntax.all._
    import com.thoughtworks.future._
    import com.thoughtworks.raii.asynchronous._
    import com.thoughtworks.raii.AsynchronousPool
    import java.lang.AutoCloseable
    trait MyResource extends AutoCloseable {
      def inUse(): Unit
    }
    val myResourceStub0 = stub[MyResource]
    val myResourceStub1 = stub[MyResource]
    val myResourceStub2 = stub[MyResource]
    val myResourceFactoryMock = mockFunction[MyResource]
    myResourceFactoryMock.expects().returns(myResourceStub0)
    myResourceFactoryMock.expects().returns(myResourceStub1)
    myResourceFactoryMock.expects().returns(myResourceStub2)

    then it can be converted to a resource pool, which holds some instances of MyResource.

    val myResourcePool: Do[Do[MyResource]] = AsynchronousPool.fixed(
      resourceFactory = Do.autoCloseable(myResourceFactoryMock()),
      poolSize = 3
    )

    When some clients are using the resource pool,

    def client(acquire: Do[MyResource], operationsPerClient: Int): Do[Unit] = {
      Do.nested[Unit](acquire.flatMap { myResource =>
        Do.execute {
            myResource.inUse
          }
          .replicateM_(operationsPerClient)
      })
    }
    def allClients(acquire: Do[MyResource], numberOfClients: Int, operationsPerClient: Int): ParallelDo[Unit] = {
      implicit def keepLastException = new Semigroup[Throwable] {
        override def append(f1: Throwable, f2: => Throwable) = f2
      }
      Applicative[ParallelDo].replicateM_(numberOfClients, Parallel(client(acquire, operationsPerClient)))
    }
    def usingPool(numberOfClients: Int, operationsPerClient: Int) = {
      myResourcePool.flatMap { acquire =>
        allClients(acquire, numberOfClients, operationsPerClient).unwrap
      }
    }

    then the operations from these clients should be distributed on those MyResources, and those MyResources should be closed after being used.

    usingPool(numberOfClients = 10, operationsPerClient = 10).run.map { _: Unit =>
      ((myResourceStub0.inUse _): () => Unit).verify().repeated(30 to 40)
      ((myResourceStub0.close _): () => Unit).verify().once()
      ((myResourceStub1.inUse _): () => Unit).verify().repeated(30 to 40)
      ((myResourceStub1.close _): () => Unit).verify().once()
      ((myResourceStub2.inUse _): () => Unit).verify().repeated(30 to 40)
      ((myResourceStub2.close _): () => Unit).verify().once()
      succeed
    }.toScalaFuture
Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. AsynchronousPool
  2. Serializable
  3. Serializable
  4. AnyRef
  5. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. final class ClosedPoolException extends IllegalStateException

    Permalink

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  6. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  8. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  9. def fixed[A](resourceFactory: Do[A], poolSize: Int): Do[Do[A]]

    Permalink

    Returns a factory of fixed sized resource pool.

  10. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  11. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  12. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  13. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  14. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  15. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  16. def preloaded[A](preloadedResources: Seq[A]): Resource[UnitContinuation, Do[A]]

    Permalink

    Returns a resource pool from preloaded resoures.

  17. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  18. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  19. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from Serializable

Inherited from Serializable

Inherited from AnyRef

Inherited from Any

Ungrouped