java.util.concurrent.ScheduledThreadPoolExecutor Scala Examples

The following examples show how to use java.util.concurrent.ScheduledThreadPoolExecutor. 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: NotifyRMEventPublisher.scala    From Linkis   with Apache License 2.0 5 votes vote down vote up
package com.webank.wedatasphere.linkis.resourcemanager.notify

import java.util.concurrent.ScheduledThreadPoolExecutor

import com.webank.wedatasphere.linkis.common.utils.{Logging, Utils}
import com.webank.wedatasphere.linkis.resourcemanager.event.notify._
import org.apache.zookeeper.ZooDefs.Ids
import org.apache.zookeeper._
import org.json4s._
import org.json4s.jackson.Serialization.{read, write}

import scala.collection.JavaConversions._


class NotifyRMEventPublisher(val topic: String, val zk: ZooKeeper) extends TopicPublisher[NotifyRMEvent] with Logging {

  implicit val formats = DefaultFormats + NotifyRMEventSerializer
  val historyRoot = "/dwc_events_history"
  val historyScheduler = new ScheduledThreadPoolExecutor(1)

  try {
    if (zk.exists("/" + topic, false) == null) zk.create("/" + topic, new Array[Byte](0), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)
    if (zk.exists(historyRoot, false) == null) zk.create(historyRoot, new Array[Byte](0), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)
    if (zk.exists(historyRoot + "/" + topic, false) == null) zk.create(historyRoot + "/" + topic, new Array[Byte](0), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)
  } catch {
    case e: KeeperException => error(s"Failed to create topic[$topic]: ", e)
  }


  def publish(event: NotifyRMEvent): Unit = {
    val moduleScope = event.moduleName + "_" + event.eventScope.toString
    val path = "/" + topic + "/" + moduleScope
    val historyPath = historyRoot + path
    if (zk.exists(path, false) == null) {
      zk.create(path, serialize(event), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)
      zk.create(historyPath, new Array[Byte](0), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)
    } else {
      // should merge with old event
      val oldEvent = deserialize(zk.getData(path, false, null))
      zk.setData(path, serialize(event.merge(oldEvent)), -1)
    }

    recordHistory(event, historyPath)
  }

  def recordHistory(event: NotifyRMEvent, historyPath: String) = {
    info(Thread.currentThread() + "start to record event to history async")
    historyScheduler.submit(new Runnable {
      override def run(): Unit = Utils.tryAndError({
        event match {
          //for special events, don't record
          case moduleUnregisterEvent: ModuleUnregisterEvent =>
            zk.getChildren(historyPath, false).foreach { eventIndex =>
              deserialize(zk.getData(historyPath + "/" + eventIndex, false, null)) match {
                case e: ModuleInstanceEvent if e.moduleInstance.equals(moduleUnregisterEvent.moduleInstance) =>
                  zk.delete(historyPath + "/" + eventIndex, -1)
                case _ =>
              }
            }
          case userReleasedEvent: UserReleasedEvent => deleteByTicketId(userReleasedEvent.ticketId)
          case clearPrdUsedEvent: ClearPrdUsedEvent => deleteByTicketId(clearPrdUsedEvent.ticketId)
          case clearUsedEvent: ClearUsedEvent => deleteByTicketId(clearUsedEvent.ticketId)
          //for normal events, do record
          case ticketIdEvent: TicketIdEvent => zk.create(historyPath + "/" + ticketIdEvent.ticketId, serialize(event), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL)
          case _ => zk.create(historyPath + "/event", serialize(event), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL)
        }
        info(Thread.currentThread() + "finished record event to history async")
      })

      def deleteByTicketId(ticketId: String) = zk.getChildren(historyPath, false).foreach { eventIndex =>
        if (eventIndex.startsWith(ticketId)) zk.delete(historyPath + "/" + eventIndex, -1)
      }
    })
  }

  def serialize(event: NotifyRMEvent): Array[Byte] = {
    val serialized = write(event).getBytes
    info(Thread.currentThread() + "Serialized event, ready to publish: " + serialized)
    serialized
  }

  private def deserialize(bytes: Array[Byte]) = read[NotifyRMEvent](new String(bytes))

  def remove(event: NotifyRMEvent) = ZKUtil.deleteRecursive(zk, "/" + topic + "/" + event.moduleName + "_" + event.eventScope.toString)
}

object NotifyRMEventPublisher {
  def apply(topic: String, zk: ZooKeeper): NotifyRMEventPublisher = new NotifyRMEventPublisher(topic, zk)

  def apply(topic: String): NotifyRMEventPublisher = new NotifyRMEventPublisher(topic, ZookeeperUtils.getOrCreateZookeeper())
} 
Example 2
Source File: ThreadUtil.scala    From coursier   with Apache License 2.0 5 votes vote down vote up
package coursier.cache.internal

import java.util.concurrent.{ExecutorService, LinkedBlockingQueue, ScheduledExecutorService, ScheduledThreadPoolExecutor, ThreadFactory, ThreadPoolExecutor, TimeUnit}
import java.util.concurrent.atomic.AtomicInteger

object ThreadUtil {

  private val poolNumber = new AtomicInteger(1)

  def daemonThreadFactory(): ThreadFactory = {

    val poolNumber0 = poolNumber.getAndIncrement()

    val threadNumber = new AtomicInteger(1)

    new ThreadFactory {
      def newThread(r: Runnable) = {
        val threadNumber0 = threadNumber.getAndIncrement()
        val t = new Thread(r, s"coursier-pool-$poolNumber0-thread-$threadNumber0")
        t.setDaemon(true)
        t.setPriority(Thread.NORM_PRIORITY)
        t
      }
    }
  }

  def fixedThreadPool(size: Int): ExecutorService = {

    val factory = daemonThreadFactory()

    // 1 min keep alive, so that threads get stopped a bit after resolution / downloading is done
    val executor = new ThreadPoolExecutor(
      size, size,
      1L, TimeUnit.MINUTES,
      new LinkedBlockingQueue[Runnable],
      factory
    )
    executor.allowCoreThreadTimeOut(true)
    executor
  }

  def fixedScheduledThreadPool(size: Int): ScheduledExecutorService = {

    val factory = daemonThreadFactory()

    val executor = new ScheduledThreadPoolExecutor(size, factory)
    executor.setKeepAliveTime(1L, TimeUnit.MINUTES)
    executor.allowCoreThreadTimeOut(true)
    executor
  }

  def withFixedThreadPool[T](size: Int)(f: ExecutorService => T): T = {

    var pool: ExecutorService = null
    try {
      pool = fixedThreadPool(size)
      f(pool)
    } finally {
      if (pool != null)
        pool.shutdown()
    }
  }

} 
Example 3
Source File: SimpleScheduler.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.util.concurrent

import java.util.concurrent.{ScheduledExecutorService, ScheduledFuture, ScheduledThreadPoolExecutor}

import com.typesafe.scalalogging.LazyLogging

import scala.concurrent.{ExecutionContext, Future, Promise}
import scala.concurrent.duration.{Duration, FiniteDuration}
import scala.util.Try


object SimpleScheduler extends LazyLogging {
  private[this] lazy val timer = {
    val executor = new ScheduledThreadPoolExecutor(1)
    executor.setRemoveOnCancelPolicy(true)
    executor.asInstanceOf[ScheduledExecutorService]
  }

  //method is private, since we must keep execution on the expense of out timer thread to be as limited as possible.
  //this method can be used if and only if we know `body` is a safe and small job.
  private[util] def scheduleInstant[T](duration: FiniteDuration)(body: => T) = {
    val p = Promise[T]()
    val cancellable = timer.schedule(
      new Runnable {
        override def run(): Unit = {
          // body must not be expensive to compute since it will be run in our only timer thread expense.
          p.complete(Try(body))
        }
      },
      duration.toMillis,
      java.util.concurrent.TimeUnit.MILLISECONDS
    )
    p.future -> Cancellable(cancellable)
  }

  def scheduleAtFixedRate(initialDelay: FiniteDuration, period: FiniteDuration, mayInterruptIfRunning: Boolean = false)(
    task: => Any
  )(implicit executionContext: ExecutionContext): Cancellable = {
    // memoize runnable task
    val runnable: Runnable = new Runnable {
      override def run(): Unit = Try(task).failed.foreach { err =>
        logger.error("schedueled task failed", err)
      }
    }

    val cancellable = timer.scheduleAtFixedRate(new Runnable {
      override def run(): Unit = executionContext.execute(runnable)
    }, initialDelay.toMillis, period.toMillis, java.util.concurrent.TimeUnit.MILLISECONDS)

    Cancellable(cancellable, mayInterruptIfRunning)
  }

  def schedule[T](duration: FiniteDuration)(body: => T)(implicit executionContext: ExecutionContext): Future[T] = {
    val p = Promise[T]()
    timer.schedule(
      new Runnable {
        override def run(): Unit = {
          // body may be expensive to compute, and must not be run in our only timer thread expense,
          // so we compute the task inside a `Future` and make it run on the expense of the given executionContext.
          p.completeWith(Future(body)(executionContext))
        }
      },
      duration.toMillis,
      java.util.concurrent.TimeUnit.MILLISECONDS
    )
    p.future
  }

  def scheduleFuture[T](duration: Duration)(body: => Future[T]): Future[T] = {
    val p = Promise[T]()
    timer.schedule(new Runnable {
      override def run(): Unit = p.completeWith(body)
    }, duration.toMillis, java.util.concurrent.TimeUnit.MILLISECONDS)
    p.future
  }
}

object Cancellable {
  def apply(scheduledFuture: ScheduledFuture[_], mayInterruptIfRunning: Boolean = false)=
    new Cancellable { override def cancel(): Boolean = scheduledFuture.cancel(mayInterruptIfRunning) }
}

trait Cancellable {
  def cancel(): Boolean
} 
Example 4
Source File: EventManager.scala    From Mycat-spider   with Apache License 2.0 5 votes vote down vote up
package turbo.crawler.power

import java.util.ArrayList
import java.util.Hashtable
import java.util.concurrent.Callable
import java.util.concurrent.FutureTask
import java.util.concurrent.ScheduledThreadPoolExecutor
import turbo.crawler.Lifecycle
import turbo.crawler.Logable
import turbo.crawler.StringAdapter
import java.util.Collections

/**
 * Event manager
 * @author mclaren
 *
 */
object EventManager extends Lifecycle with Logable with StringAdapter with MessageDriven {
  /**
   * 线程池
   */
  private val exec = new ScheduledThreadPoolExecutor(sysprop("fetch.threads", "100").toInt)

  /**
   * 事件处理器
   */
  private val handlers = new Hashtable[String, java.util.List[Evt => Unit]]()

  /**
   * 获取JVM配置参数
   */
  private def sysprop(key: String, default: String) = {
    var matched = System.getProperty(key)
    if (isNotEmpty(matched)) matched else default
  }

  /**
   * 卸载系统
   */
  override def shutdown = {
    try {
      while (true) {
        if (exec.getActiveCount == 0) {
          exec.shutdown()
          throw new RuntimeException()
        }
      }
    } catch {
      case e: Exception => logger.info("Fetch completed and shutdown concurrenty fetchers.")
    }
  }

  /**
   * 向系统注册事件监听
   */
  def attachEvent(eventId: String, handler: Evt => Unit): Unit = {
    handlers.synchronized {
      var hds = handlers.get(eventId)
      if (hds == null) hds = new ArrayList[Evt => Unit]()
      hds.add(handler)
      handlers.put(eventId, hds)
    }
  }

  /**
   * 处理事件分发
   */
  override def fireEvent(evt: Evt): Unit = {
    if (handlers.containsKey(evt.eventId)) new WrapList[Evt => Unit](handlers.get(evt.eventId)).foreach(fd => dispatchEventConcurrently(evt, fd)) else logger.error("No handlers for event" + evt)
  }

  /**
   * 并行分发事件
   */
  private def dispatchEventConcurrently(evt: Evt, f: Evt => Unit) = {
    var task = new FutureTask[Unit](new Callable[Unit]() {
      def call: Unit = f(evt)
    })
    this.exec.submit(task)
  }

  /**
   * 包装Java列表为SCALA风格
   */
  private class WrapList[T](list: java.util.List[T]) {
    def foreach(f: T => Unit) = for (i <- 0 to list.size() - 1) f(list.get(i))
  }
} 
Example 5
Source File: IOTimer.scala    From cats-effect   with Apache License 2.0 5 votes vote down vote up
package cats.effect
package internals

import java.util.concurrent.{ScheduledExecutorService, ScheduledThreadPoolExecutor, ThreadFactory, TimeUnit}
import cats.effect.internals.Callback.T
import cats.effect.internals.IOShift.Tick
import scala.concurrent.ExecutionContext
import scala.concurrent.duration._
import scala.util.Try


  def apply(ec: ExecutionContext, sc: ScheduledExecutorService): Timer[IO] =
    new IOTimer(ec, sc)

  private[internals] lazy val scheduler: ScheduledExecutorService =
    mkGlobalScheduler(sys.props)

  private[internals] def mkGlobalScheduler(props: collection.Map[String, String]): ScheduledThreadPoolExecutor = {
    val corePoolSize = props
      .get("cats.effect.global_scheduler.threads.core_pool_size")
      .flatMap(s => Try(s.toInt).toOption)
      .filter(_ > 0)
      .getOrElse(2)
    val keepAliveTime = props
      .get("cats.effect.global_scheduler.keep_alive_time_ms")
      .flatMap(s => Try(s.toLong).toOption)
      .filter(_ > 0L)

    val tp = new ScheduledThreadPoolExecutor(corePoolSize, new ThreadFactory {
      def newThread(r: Runnable): Thread = {
        val th = new Thread(r)
        th.setName(s"cats-effect-scheduler-${th.getId}")
        th.setDaemon(true)
        th
      }
    })
    keepAliveTime.foreach { timeout =>
      // Call in this order or it throws!
      tp.setKeepAliveTime(timeout, TimeUnit.MILLISECONDS)
      tp.allowCoreThreadTimeOut(true)
    }
    tp.setRemoveOnCancelPolicy(true)
    tp
  }

  final private class ShiftTick(
    conn: IOConnection,
    cb: Either[Throwable, Unit] => Unit,
    ec: ExecutionContext
  ) extends Runnable {
    def run(): Unit = {
      // Shifts actual execution on our `ExecutionContext`, because
      // the scheduler is in charge only of ticks and the execution
      // needs to shift because the tick might continue with whatever
      // bind continuation is linked to it, keeping the current thread
      // occupied
      conn.pop()
      ec.execute(new Tick(cb))
    }
  }
} 
Example 6
Source File: JvmIOTimerTests.scala    From cats-effect   with Apache License 2.0 5 votes vote down vote up
package cats.effect
package internals

import java.util.concurrent.{ScheduledThreadPoolExecutor, TimeUnit}

import org.scalatest.matchers.should.Matchers
import org.scalatest.funsuite.AnyFunSuite

import scala.util.control.NonFatal

class JvmIOTimerTests extends AnyFunSuite with Matchers {
  private def withScheduler(props: Map[String, String])(f: ScheduledThreadPoolExecutor => Unit): Unit = {
    val s = IOTimer.mkGlobalScheduler(props)
    try f(s)
    finally {
      try s.shutdownNow()
      catch { case NonFatal(e) => e.printStackTrace() }
    }
  }

  test("global scheduler: default core pool size") {
    withScheduler(Map.empty) { s =>
      s.getCorePoolSize shouldBe 2
    }
  }

  test("global scheduler: custom core pool size") {
    withScheduler(Map("cats.effect.global_scheduler.threads.core_pool_size" -> "3")) { s =>
      s.getCorePoolSize shouldBe 3
    }
  }

  test("global scheduler: invalid core pool size") {
    withScheduler(Map("cats.effect.global_scheduler.threads.core_pool_size" -> "-1")) { s =>
      s.getCorePoolSize shouldBe 2
    }
  }

  test("global scheduler: malformed core pool size") {
    withScheduler(Map("cats.effect.global_scheduler.threads.core_pool_size" -> "banana")) { s =>
      s.getCorePoolSize shouldBe 2
    }
  }

  test("global scheduler: default core thread timeout") {
    withScheduler(Map.empty) { s =>
      s.allowsCoreThreadTimeOut shouldBe false
    }
  }

  test("global scheduler: custom core thread timeout") {
    withScheduler(Map("cats.effect.global_scheduler.keep_alive_time_ms" -> "1000")) { s =>
      s.allowsCoreThreadTimeOut shouldBe true
      s.getKeepAliveTime(TimeUnit.MILLISECONDS) shouldBe 1000
    }
  }

  test("global scheduler: invalid core thread timeout") {
    withScheduler(Map("cats.effect.global_scheduler.keep_alive_time_ms" -> "0")) { s =>
      s.allowsCoreThreadTimeOut shouldBe false
    }
  }

  test("global scheduler: malformed core thread timeout") {
    withScheduler(Map("cats.effect.global_scheduler.keep_alive_time_ms" -> "feral hogs")) { s =>
      s.allowsCoreThreadTimeOut shouldBe false
    }
  }
} 
Example 7
Source File: TimerHelper.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.core.helper

import java.lang.Long
import java.util.concurrent.{ScheduledThreadPoolExecutor, TimeUnit}

object TimerHelper {

  private val ex = new ScheduledThreadPoolExecutor(1)

  def periodic(initialDelay: Long, period: Long, fun: => Unit): Unit = {
    val task = new Runnable {
      def run() = fun
    }
    ex.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.SECONDS)
  }

  def periodic(period: Long, fun: => Unit): Unit = {
    periodic(0L, period, fun)
  }

  def timer(delay: Long, fun: => Unit): Unit = {
    val task = new Runnable {
      def run() = fun
    }
    ex.schedule(task, delay, TimeUnit.SECONDS)
  }

}