java.util.concurrent.ArrayBlockingQueue Scala Examples

The following examples show how to use java.util.concurrent.ArrayBlockingQueue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: PublishToBlockingResource.scala    From akka_streams_tutorial   with MIT License 5 votes vote down vote up
package sample.stream

import java.util.concurrent.{ArrayBlockingQueue, BlockingQueue}

import akka.NotUsed
import akka.actor.ActorSystem
import akka.stream.DelayOverflowStrategy
import akka.stream.scaladsl.{Flow, Sink, Source}

import scala.concurrent.duration._
import scala.util.Failure



object PublishToBlockingResource extends App {
  implicit val system = ActorSystem("PublishToBlockingResource")
  implicit val ec = system.dispatcher

  val slowSink: Sink[Seq[Int], NotUsed] =
    Flow[Seq[Int]]
      .delay(1.seconds, DelayOverflowStrategy.backpressure)
      .to(Sink.foreach(e => println(s"Reached sink: $e")))

  val blockingResource: BlockingQueue[Int] = new ArrayBlockingQueue[Int](100)

  //Start a new `Source` from some (third party) blocking resource which can be opened, read and closed
  val source: Source[Int, NotUsed] =
    Source.unfoldResource[Int, BlockingQueue[Int]](
      () => blockingResource,                   //open
      (q: BlockingQueue[Int]) => Some(q.take()),//read
      (_: BlockingQueue[Int]) => {})            //close

  val done = source
    .groupedWithin(10, 2.seconds)
    .watchTermination()((_, done) => done.onComplete {
      case Failure(err) =>
        println(s"Flow failed: $err")
      case each => println(s"Server flow terminated: $each")
    })
    .runWith(slowSink)

  //simulate n process that publish in blocking fashion to the queue
  (1 to 1000).par.foreach(value => blockingResource.put(value))
} 
Example 2
Source File: WIPRegulator.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.util.concurrent

import java.util.concurrent.{ArrayBlockingQueue, TimeUnit}
import scala.concurrent.Future
import scala.util.{Failure, Success, Try}
import com.typesafe.scalalogging.Logger
import scala.concurrent.ExecutionContext.Implicits.global
import cmwell.util.jmx._


case class WIPRegulator(private var numOfWorkers: Int, noWorkerAlertInterval: Long = 30000) extends WIPRegulatorMBean {

  jmxRegister(this, "cmwell.indexer:type=WIPRegulator")

  private val wipQueue = new ArrayBlockingQueue[String](50)

  // Set intitial number of concurrent requests
  for (i <- 1 to numOfWorkers)(wipQueue.add("WIP Worker " + i))

  def doWithWorkerAsync[T](f: => Future[T])(implicit logger: Logger): Future[T] = {
    var notFinished = true
    var reply: Future[T] = Future.failed(FailedToExecuteException())

    while (notFinished) {
      Try {
        wipQueue.poll(noWorkerAlertInterval, TimeUnit.MILLISECONDS)
      } match {
        case Success(null) =>
          logger.error(s"waited for $noWorkerAlertInterval miliseconds and did not get worker, something is wrong")
        case Success(worker) => reply = f; reply.onComplete(_ => wipQueue.add(worker)); notFinished = false
        case Failure(execption) =>
          logger.error("InteruptedException while trying to poll a worker from queue"); reply = Future.failed(execption);
          notFinished = false
      }
    }

    reply
  }

  def getNumOfWorkers(): Int = wipQueue.size()

  def addWorker(): Unit = this.synchronized { numOfWorkers += 1; wipQueue.add(s"WIP Worker $numOfWorkers") }

  def removeWorker(): Unit = this.synchronized { wipQueue.remove(s"WIP Worker $numOfWorkers"); numOfWorkers -= 1 }

}

trait WIPRegulatorMBean {
  def getNumOfWorkers(): Int
  def addWorker()
  def removeWorker()
}

case class FailedToExecuteException(msg: String = "Undifiened reason") extends Exception(msg) 
Example 3
Source File: ToolTestUtilities.scala    From scala-debugger   with Apache License 2.0 5 votes vote down vote up
package test

import java.util.concurrent.ArrayBlockingQueue

import org.scaladebugger.api.utils.Logging
import org.scaladebugger.tool.frontend.VirtualTerminal
import org.scalatest.{Assertion, Matchers}


  def validateNextLine(
    vt: VirtualTerminal,
    text: String,
    success: (String, String) => Assertion = (text, line) => text should be (line),
    fail: String => Assertion = fail(_: String),
    lineLogger: String => Unit = logger.debug(_: String)
  ): Assertion = {
    import scala.reflect.runtime.universe._
    val t = Literal(Constant(text)).toString
    nextLine(vt) match {
      case Some(line) =>
        val l = Literal(Constant(line)).toString
        lineLogger(l)
        success(text, line)
      case None =>
        fail(s"Unable to find desired line in time: '$t'")
    }
  }
} 
Example 4
Source File: HttpClientSyncBackend.scala    From sttp   with Apache License 2.0 5 votes vote down vote up
package sttp.client.httpclient

import java.net.http.{HttpClient, HttpRequest}
import java.net.http.HttpResponse.BodyHandlers
import java.util.concurrent.ArrayBlockingQueue

import sttp.client.httpclient.HttpClientBackend.EncodingHandler
import sttp.client.monad.{IdMonad, MonadError}
import sttp.client.testing.SttpBackendStub
import sttp.client.ws.WebSocketResponse
import sttp.client.{
  FollowRedirectsBackend,
  Identity,
  Request,
  Response,
  SttpBackend,
  SttpBackendOptions,
  SttpClientException
}
import sttp.model.Headers

class HttpClientSyncBackend private (
    client: HttpClient,
    closeClient: Boolean,
    customizeRequest: HttpRequest => HttpRequest,
    customEncodingHandler: EncodingHandler
) extends HttpClientBackend[Identity, Nothing](client, closeClient, customEncodingHandler) {
  override def send[T](request: Request[T, Nothing]): Identity[Response[T]] =
    adjustExceptions {
      val jRequest = customizeRequest(convertRequest(request))
      val response = client.send(jRequest, BodyHandlers.ofInputStream())
      readResponse(response, request.response)
    }

  override def responseMonad: MonadError[Identity] = IdMonad

  override def openWebsocket[T, WS_RESULT](
      request: Request[T, Nothing],
      handler: WebSocketHandler[WS_RESULT]
  ): Identity[WebSocketResponse[WS_RESULT]] =
    adjustExceptions {
      val responseCell = new ArrayBlockingQueue[Either[Throwable, WebSocketResponse[WS_RESULT]]](1)
      def fillCellError(t: Throwable): Unit = responseCell.add(Left(t))
      def fillCell(wr: WebSocketResponse[WS_RESULT]): Unit = responseCell.add(Right(wr))
      val listener = new DelegatingWebSocketListener(
        handler.listener,
        webSocket => {
          val wsResponse = sttp.client.ws.WebSocketResponse(Headers.apply(Seq.empty), handler.createResult(webSocket))
          fillCell(wsResponse)
        },
        fillCellError
      )
      client
        .newWebSocketBuilder()
        .buildAsync(request.uri.toJavaUri, listener)
      responseCell.take().fold(throw _, identity)
    }

  private def adjustExceptions[T](t: => T): T =
    SttpClientException.adjustSynchronousExceptions(t)(SttpClientException.defaultExceptionToSttpClientException)
}

object HttpClientSyncBackend {
  private def apply(
      client: HttpClient,
      closeClient: Boolean,
      customizeRequest: HttpRequest => HttpRequest,
      customEncodingHandler: EncodingHandler
  ): SttpBackend[Identity, Nothing, WebSocketHandler] =
    new FollowRedirectsBackend[Identity, Nothing, WebSocketHandler](
      new HttpClientSyncBackend(client, closeClient, customizeRequest, customEncodingHandler)
    )

  def apply(
      options: SttpBackendOptions = SttpBackendOptions.Default,
      customizeRequest: HttpRequest => HttpRequest = identity,
      customEncodingHandler: EncodingHandler = PartialFunction.empty
  ): SttpBackend[Identity, Nothing, WebSocketHandler] =
    HttpClientSyncBackend(
      HttpClientBackend.defaultClient(options),
      closeClient = true,
      customizeRequest,
      customEncodingHandler
    )

  def usingClient(
      client: HttpClient,
      customizeRequest: HttpRequest => HttpRequest = identity,
      customEncodingHandler: EncodingHandler = PartialFunction.empty
  ): SttpBackend[Identity, Nothing, WebSocketHandler] =
    HttpClientSyncBackend(client, closeClient = false, customizeRequest, customEncodingHandler)

  
  def stub: SttpBackendStub[Identity, Nothing, WebSocketHandler] = SttpBackendStub.synchronous
} 
Example 5
Source File: MapLifter.scala    From diffy   with GNU Affero General Public License v3.0 5 votes vote down vote up
package ai.diffy.lifter

import com.twitter.concurrent.NamedPoolThreadFactory
import com.twitter.util.{ExecutorServiceFuturePool, Future, FuturePool}
import java.util.concurrent.{ArrayBlockingQueue, ThreadPoolExecutor, TimeUnit}

case class Message(endpoint: Option[String], result: FieldMap[Any])

trait MapLifter {
  def apply(input: Array[Byte]): Future[Message]
}

object MapLifterPool {
  val QueueSizeDefault = 5

  def apply(mapLifterFactory: => MapLifter) = {
    val executorService =
      new ThreadPoolExecutor(
        3,   // core pool size
        10,  // max pool size
        500, // keep alive time
        TimeUnit.MILLISECONDS,
        new ArrayBlockingQueue[Runnable](10), // work queue
        new NamedPoolThreadFactory("maplifter", makeDaemons = true),
        new ThreadPoolExecutor.AbortPolicy()
      )
    executorService.prestartCoreThread()
    new MapLifterPool(mapLifterFactory, new ExecutorServiceFuturePool(executorService))
  }
}

class MapLifterPool(underlying: MapLifter, futurePool: FuturePool) extends MapLifter {
  override def apply(input: Array[Byte]): Future[Message] =
    (futurePool { underlying(input) }).flatten
} 
Example 6
Source File: TestDeleteTopicsConcurrently.scala    From ohara   with Apache License 2.0 4 votes vote down vote up
package oharastream.ohara.configurator

import java.util.concurrent.atomic.{AtomicBoolean, AtomicInteger}
import java.util.concurrent.{ArrayBlockingQueue, Executors, LinkedBlockingDeque, TimeUnit}

import oharastream.ohara.client.configurator.{BrokerApi, TopicApi}
import oharastream.ohara.common.setting.TopicKey
import oharastream.ohara.common.util.Releasable
import oharastream.ohara.testing.WithBrokerWorker
import org.junit.{After, Test}
import org.scalatest.matchers.should.Matchers._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}
import scala.jdk.CollectionConverters._

class TestDeleteTopicsConcurrently extends WithBrokerWorker {
  private[this] val configurator =
    Configurator.builder.fake(testUtil.brokersConnProps, testUtil().workersConnProps()).build()

  private[this] val topicApi = TopicApi.access.hostname(configurator.hostname).port(configurator.port)

  private[this] def result[T](f: Future[T]): T = Await.result(f, Duration(20, TimeUnit.SECONDS))

  private[this] val brokerClusterInfo = result(
    BrokerApi.access.hostname(configurator.hostname).port(configurator.port).list()
  ).head

  @Test
  def test(): Unit = {
    val loopMax       = 10
    val count         = 3
    val topicKeyQueue = new ArrayBlockingQueue[TopicKey](count)
    (0 until count).foreach(i => topicKeyQueue.put(TopicKey.of("test", i.toString)))
    val executors      = Executors.newFixedThreadPool(count)
    val exceptionQueue = new LinkedBlockingDeque[Throwable]()
    val closed         = new AtomicBoolean(false)
    val loopCount      = new AtomicInteger(0)
    (0 until count).foreach(
      _ =>
        executors.execute { () =>
          while (!closed.get() && loopCount.getAndIncrement() <= loopMax) try {
            val topicKey = topicKeyQueue.take()
            try result(
              topicApi.request
                .group(topicKey.group())
                .name(topicKey.name())
                .brokerClusterKey(brokerClusterInfo.key)
                .numberOfPartitions(1)
                .numberOfReplications(1)
                .create()
                .flatMap(_ => topicApi.start(topicKey))
                .flatMap { _ =>
                  TimeUnit.SECONDS.sleep(1)
                  topicApi.stop(topicKey)
                }
                .flatMap { _ =>
                  TimeUnit.SECONDS.sleep(1)
                  topicApi.delete(topicKey)
                }
            )
            finally topicKeyQueue.put(topicKey)
          } catch {
            case t: Throwable =>
              exceptionQueue.put(t)
              closed.set(true)
          }
        }
    )
    executors.shutdown()
    withClue(s"${exceptionQueue.asScala.map(_.getMessage).mkString(",")}") {
      executors.awaitTermination(60, TimeUnit.SECONDS) shouldBe true
    }
    exceptionQueue.size() shouldBe 0
  }
  @After
  def tearDown(): Unit = Releasable.close(configurator)
}