java.util.concurrent.CopyOnWriteArrayList Scala Examples

The following examples show how to use java.util.concurrent.CopyOnWriteArrayList. 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: MultiFuture.scala    From sbt-dependency-updates   with Apache License 2.0 5 votes vote down vote up
package org.jmotor.sbt.concurrent

import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.atomic.AtomicInteger

import org.jmotor.sbt.exception.MultiException

import scala.concurrent.Promise


class MultiFuture[T](p: Promise[T], count: Int, default: T) {
  private[this] val counter = new AtomicInteger(0)
  private[this] val errors = new CopyOnWriteArrayList[Throwable]()

  def tryComplete(): Unit = {
    if (counter.incrementAndGet() == count) {
      if (errors.isEmpty) {
        p success default
      } else {
        import scala.collection.JavaConverters._
        p failure MultiException(errors.asScala: _*)
      }
    }
  }

  def tryComplete(throwable: Throwable): Unit = {
    errors.add(throwable)
    tryComplete()
  }

} 
Example 2
Source File: EventBus.scala    From aloha   with Apache License 2.0 5 votes vote down vote up
package me.jrwang.aloha.scheduler.bus

import java.util.concurrent.CopyOnWriteArrayList

import scala.collection.JavaConverters._
import scala.util.control.NonFatal

import me.jrwang.aloha.common.Logging
import me.jrwang.aloha.common.util.Utils


trait EventBus[L <: AnyRef, E] extends Logging {
  private[bus] val listeners = new CopyOnWriteArrayList[L]()

  
  def removeListenerOnError(listener: L): Unit = {
    removeListener(listener)
  }

  protected def doPostEvent(listener: L, event: E):Unit

  def postToAll(event: E): Unit = {
    val iter = listeners.iterator()
    while (iter.hasNext) {
      val listener = iter.next()
      try {
        doPostEvent(listener,event)
        if (Thread.interrupted()) {
          // We want to throw the InterruptedException right away so we can associate the interrupt
          // with this listener, as opposed to waiting for a queue.take() etc. to detect it.
          throw new InterruptedException()
        }
      } catch {
        case ie: InterruptedException =>
          logError(s"Interrupted while posting to ${Utils.getFormattedClassName(listener)}.  " +
            s"Removing that listener.", ie)
          removeListenerOnError(listener)
        case NonFatal(e) =>
          logError(s"Listener ${Utils.getFormattedClassName(listener)} throws an Exception.", e)
      }
    }
  }
} 
Example 3
Source File: RabbitmqClusterManage.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.cluster.rabbitmq

import java.util.concurrent.CopyOnWriteArrayList

import com.ecfront.common.Resp
import com.ecfront.ez.framework.core.cluster.ClusterManage
import com.fasterxml.jackson.databind.JsonNode
import com.rabbitmq.client.{Channel, Connection, ConnectionFactory}

import scala.collection.JavaConversions._

object RabbitmqClusterManage extends ClusterManage {

  private var conn: Connection = _
  private var factory: ConnectionFactory = _
  private val channels: CopyOnWriteArrayList[Channel] = new CopyOnWriteArrayList[Channel]()

  private[rabbitmq] var defaultTopicExchangeName: String = _
  private[rabbitmq] var defaultRPCExchangeName: String = _
  private[rabbitmq] var defaultQueueExchangeName: String = _

  override def init(config: JsonNode): Resp[Void] = {
    factory = new ConnectionFactory()
    if (config.has("userName")) {
      factory.setUsername(config.get("userName").asText())
      factory.setPassword(config.get("password").asText())
    }
    if (config.has("virtualHost")) {
      factory.setVirtualHost(config.get("virtualHost").asText())
    }
    factory.setHost(config.get("host").asText())
    factory.setPort(config.get("port").asInt())
    if (config.has("defaultTopicExchangeName")) {
      defaultTopicExchangeName = config.get("defaultTopicExchangeName").asText()
    }
    if (config.has("defaultRPCExchangeName")) {
      defaultRPCExchangeName = config.get("defaultRPCExchangeName").asText()
    }
    if (config.has("defaultQueueExchangeName")) {
      defaultQueueExchangeName = config.get("defaultQueueExchangeName").asText()
    }
    conn = factory.newConnection()
    Resp.success(null)
  }

  override def close(): Unit = {
    closeChannel()
    conn.close()
  }

  private[rabbitmq] def getChannel(): Channel = {
    val channel = conn.createChannel()
    channels += channel
    channel
  }

  private[rabbitmq] def closeChannel(): Unit = {
    channels.foreach {
      channel =>
        if (channel.isOpen) {
          channel.close()
        }
    }
  }

} 
Example 4
Source File: Cluster.scala    From zipkin-mesos-framework   with Apache License 2.0 5 votes vote down vote up
package net.elodina.mesos.zipkin.storage

import java.util.concurrent.CopyOnWriteArrayList

import net.elodina.mesos.zipkin.Config
import net.elodina.mesos.zipkin.components.{WebService, QueryService, Collector}
import play.api.libs.json._
import play.api.libs.functional.syntax._

import scala.collection.mutable
import scala.collection.JavaConversions._

case class Cluster(_collectors: List[Collector] = Nil,
                   _webServices: List[WebService] = Nil,
                   _queryServices: List[QueryService] = Nil) {

  private[zipkin] var frameworkId: Option[String] = None

  private val storage = Cluster.newStorage(Config.storage)

  private[zipkin] val collectors: mutable.Buffer[Collector] = new CopyOnWriteArrayList[Collector]()
  private[zipkin] val queryServices: mutable.Buffer[QueryService] = new CopyOnWriteArrayList[QueryService]()
  private[zipkin] val webServices: mutable.Buffer[WebService] = new CopyOnWriteArrayList[WebService]()

  collectors ++= _collectors
  queryServices ++= _queryServices
  webServices ++= _webServices

  def clear(): Unit = {
    collectors.clear()
    queryServices.clear()
    webServices.clear()
  }

  def save() = storage.save(this)(Cluster.writer)

  def load() {
    storage.load(Cluster.reader).foreach { cluster =>
      this.frameworkId = cluster.frameworkId
      collectors ++= cluster.collectors
      webServices ++= cluster.webServices
      queryServices ++= cluster.queryServices
    }
  }

  def fetchAllComponents = {
    collectors ++ queryServices ++ webServices
  }

  def isReconciling:Boolean = {
    collectors.exists(_.isReconciling) || queryServices.exists(_.isReconciling) || webServices.exists(_.isReconciling)
  }
}

object Cluster {

  private def newStorage(storage: String): Storage[Cluster] = {
    storage.split(":", 2) match {
      case Array("file", fileName) => FileStorage(fileName)
      case Array("zk", zk) => ZkStorage(zk)
      case _ => throw new IllegalArgumentException(s"Unsupported storage: $storage")
    }
  }

  implicit val writer = new Writes[Cluster] {
    override def writes(o: Cluster): JsValue = {
      Json.obj(
        "frameworkId" -> o.frameworkId,
        "collectors" -> o.collectors.toList,
        "webServices" -> o.webServices.toList,
        "queryServices" -> o.queryServices.toList
      )
    }
  }

  implicit val reader = (
    (__ \ 'frameworkId).readNullable[String] and
    (__ \ 'collectors).read[List[Collector]] and
    (__ \ 'webServices).read[List[WebService]] and
    (__ \ 'queryServices).read[List[QueryService]]
    )((frameworkId, collectors, webServices, queryServices) => {
    val cluster = Cluster(collectors, webServices, queryServices)
    cluster.frameworkId = frameworkId
    cluster
  })
}