java.util.concurrent.RejectedExecutionException Scala Examples
The following examples show how to use java.util.concurrent.RejectedExecutionException.
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
package com.wavesplatform.dex.time import java.net.{InetAddress, SocketTimeoutException} import java.util.concurrent.RejectedExecutionException import java.util.concurrent.atomic.AtomicBoolean import com.wavesplatform.dex.domain.utils.ScorexLogging import monix.eval.Task import monix.execution.schedulers.SchedulerService import monix.execution.{ExecutionModel, Scheduler} import org.apache.commons.net.ntp.NTPUDPClient import scala.concurrent.duration.DurationInt class NTP(ntpServer: String) extends Time with ScorexLogging with AutoCloseable { private val offsetPanicThreshold = 1000000L private val ExpirationTimeout = 60.seconds private val RetryDelay = 10.seconds private val ResponseTimeout = 10.seconds private val duringShutdown = new AtomicBoolean(false) private implicit val scheduler: SchedulerService = Scheduler.singleThread( name = "time-impl", daemonic = false, executionModel = ExecutionModel.AlwaysAsyncExecution, reporter = { case _: RejectedExecutionException if duringShutdown.get() => // ignore case e: Throwable => log.error("An uncaught error", e) } ) private val client = new NTPUDPClient() client.setDefaultTimeout(ResponseTimeout.toMillis.toInt) @volatile private var offset = 0L private val updateTask: Task[Unit] = { def newOffsetTask: Task[Option[(InetAddress, java.lang.Long)]] = Task { try { val info = client.getTime(InetAddress.getByName(ntpServer)) info.computeDetails() Option(info.getOffset).map { offset => val r = if (Math.abs(offset) > offsetPanicThreshold) throw new Exception("Offset is suspiciously large") else offset (info.getAddress, r) } } catch { case _: SocketTimeoutException => None case t: Throwable => log.warn("Problems with NTP: ", t) None } } newOffsetTask.flatMap { case None if !scheduler.isShutdown => updateTask.delayExecution(RetryDelay) case Some((server, newOffset)) if !scheduler.isShutdown => log.trace(s"Adjusting time with $newOffset milliseconds, source: ${server.getHostAddress}.") offset = newOffset updateTask.delayExecution(ExpirationTimeout) case _ => Task.unit } } def correctedTime(): Long = System.currentTimeMillis() + offset private var txTime: Long = 0 def getTimestamp(): Long = { txTime = Math.max(correctedTime(), txTime + 1) txTime } private val taskHandle = updateTask.runAsync { case Left(e) => log.error(s"Error executing task", e) case _ => } override def close(): Unit = if (duringShutdown.compareAndSet(false, true)) { log.info("Shutting down Time") taskHandle.cancel() if (client.isOpen) client.close() scheduler.shutdown() } }
Example 2
Source File: TaskInstancesSpecs.scala From shims with Apache License 2.0 | 5 votes |
package shims.effect import cats.Eq import cats.laws.discipline.{ApplicativeTests, ParallelTests} import cats.effect.{Async, IO} import cats.effect.laws.discipline.{arbitrary, EffectTests}, arbitrary._ import cats.effect.laws.util.{TestContext, TestInstances}, TestInstances._ import cats.instances.either._ import cats.instances.int._ import cats.instances.tuple._ import cats.instances.unit._ import scalaz.Tag import scalaz.concurrent.Task import scalaz.concurrent.Task.ParallelTask import org.specs2.Specification import org.specs2.scalacheck.Parameters import org.specs2.specification.core.Fragments import org.typelevel.discipline.Laws import org.typelevel.discipline.specs2.Discipline import java.util.concurrent.RejectedExecutionException import scala.concurrent.ExecutionContext object TaskInstancesSpecs extends Specification with Discipline { import TaskArbitrary._ import Task.taskParallelApplicativeInstance def is = br ^ taskEff ^ br ^ taskPar ^ br ^ parTaskApp ^ br ^ asyncShiftTask def taskEff = checkAllAsync("Effect[Task]", implicit ctx => EffectTests[Task].effect[Int, Int, Int]) def taskPar = checkAllAsync("Parallel[Task]", implicit ctx => ParallelTests[Task].parallel[Int, Int]) def parTaskApp = checkAllAsync("Parallel[Task]", { implicit ctx => val tests = ApplicativeTests[ParallelTask] tests.applicative[Int, Int, Int] }) def asyncShiftTask = { implicit val context: TestContext = TestContext() val boom = new RejectedExecutionException("Boom") val rejectingEc = new ExecutionContext { def execute(runnable: Runnable): Unit = throw boom def reportFailure(cause: Throwable): Unit = () } "async.shift on rejecting execution context" ! { Eq[Task[Unit]].eqv(Async.shift[Task](rejectingEc), Task.fail(boom)) must beTrue } } def checkAllAsync(name: String, f: TestContext => Laws#RuleSet)(implicit p: Parameters) = { val context = TestContext() val ruleSet = f(context) Fragments.foreach(ruleSet.all.properties.toList) { case (id, prop) => id ! check(prop, p, defaultFreqMapPretty) ^ br } } implicit def taskEq[A: Eq](implicit ctx: TestContext): Eq[Task[A]] = Eq.by(ta => IO.async[A](k => ta.unsafePerformAsync(e => k(e.toEither)))) implicit def parallelTaskEq[A: Eq](implicit ctx: TestContext): Eq[ParallelTask[A]] = Tag.subst(taskEq[A]) }
Example 3
Source File: ExecutorSpec.scala From zio with Apache License 2.0 | 5 votes |
package zio.internal import java.util.concurrent.RejectedExecutionException import scala.concurrent.ExecutionContext import zio.ZIOBaseSpec import zio.test.Assertion._ import zio.test._ final class TestExecutor(val submitResult: Boolean) extends Executor { val here: Boolean = true def shutdown(): Unit = () def submit(runnable: Runnable): Boolean = submitResult def yieldOpCount: Int = 1 def metrics: None.type = None } final class CheckPrintThrowable extends Throwable { var printed = false override def printStackTrace(): Unit = printed = true } object TestExecutor { val failing = new TestExecutor(false) val y = new TestExecutor(true) val u = new TestExecutor(true) val badEC = new ExecutionContext { override def execute(r: Runnable): Unit = throw new RejectedExecutionException("Rejected: " + r.toString) override def reportFailure(cause: Throwable): Unit = () } val ec = new ExecutionContext { override def execute(r: Runnable): Unit = () override def reportFailure(cause: Throwable): Unit = () } // backward compatibility for scala 2.11.12 val runnable = new Runnable { override def run(): Unit = () } } object ExecutorSpec extends ZIOBaseSpec { def spec = suite("ExecutorSpec")( suite("Create the default unyielding executor and check that:")( test("When converted to an EC, it reports Throwables to stdout") { val t = new CheckPrintThrowable TestExecutor.failing.asEC.reportFailure(t) assert(t.printed)(isTrue) } ), suite("Create an executor that cannot have tasks submitted to and check that:")( test("It throws an exception upon submission") { assert(TestExecutor.failing.submitOrThrow(TestExecutor.runnable))(throwsA[RejectedExecutionException]) }, test("When converted to Java, it throws an exception upon calling execute") { assert(TestExecutor.failing.asJava.execute(TestExecutor.runnable))(throwsA[RejectedExecutionException]) } ), suite("Create a yielding executor and check that:")( test("Runnables can be submitted ") { assert(TestExecutor.y.submitOrThrow(TestExecutor.runnable))(not(throwsA[RejectedExecutionException])) }, test("When converted to an ExecutionContext, it accepts Runnables") { assert(TestExecutor.y.asEC.execute(TestExecutor.runnable))(not(throwsA[RejectedExecutionException])) }, test("When created from an EC, must not throw when fed an effect ") { assert(Executor.fromExecutionContext(1)(TestExecutor.ec).submit(TestExecutor.runnable))( not(throwsA[RejectedExecutionException]) ) }, test("When converted to Java, it accepts Runnables") { assert(TestExecutor.y.asJava.execute(TestExecutor.runnable))(not(throwsA[RejectedExecutionException])) } ), suite("Create an unyielding executor and check that:")( test("Runnables can be submitted") { assert(TestExecutor.u.submitOrThrow(TestExecutor.runnable))(not(throwsA[RejectedExecutionException])) }, test("When converted to an ExecutionContext, it accepts Runnables") { assert(TestExecutor.u.asEC.execute(TestExecutor.runnable))(not(throwsA[RejectedExecutionException])) }, test("When converted to Java, it accepts Runnables") { assert(TestExecutor.u.asJava.execute(TestExecutor.runnable))(not(throwsA[RejectedExecutionException])) } ) ) }
Example 4
Source File: DefaultExecutors.scala From zio with Apache License 2.0 | 5 votes |
package zio.internal import java.util.concurrent.{ LinkedBlockingQueue, RejectedExecutionException, ThreadPoolExecutor, TimeUnit } private[internal] abstract class DefaultExecutors { final def makeDefault(yieldOpCount: Int): Executor = fromThreadPoolExecutor(_ => yieldOpCount) { val corePoolSize = Runtime.getRuntime.availableProcessors() * 2 val maxPoolSize = corePoolSize val keepAliveTime = 60000L val timeUnit = TimeUnit.MILLISECONDS val workQueue = new LinkedBlockingQueue[Runnable]() val threadFactory = new NamedThreadFactory("zio-default-async", true) val threadPool = new ThreadPoolExecutor( corePoolSize, maxPoolSize, keepAliveTime, timeUnit, workQueue, threadFactory ) threadPool.allowCoreThreadTimeOut(true) threadPool } final def fromThreadPoolExecutor(yieldOpCount0: ExecutionMetrics => Int)( es: ThreadPoolExecutor ): Executor = new Executor { private[this] def metrics0 = new ExecutionMetrics { def concurrency: Int = es.getMaximumPoolSize() def capacity: Int = { val queue = es.getQueue() val remaining = queue.remainingCapacity() if (remaining == Int.MaxValue) remaining else remaining + queue.size } def size: Int = es.getQueue().size def workersCount: Int = es.getPoolSize() def enqueuedCount: Long = es.getTaskCount() def dequeuedCount: Long = enqueuedCount - size.toLong } def metrics = Some(metrics0) def yieldOpCount = yieldOpCount0(metrics0) def submit(runnable: Runnable): Boolean = try { es.execute(runnable) true } catch { case _: RejectedExecutionException => false } def here = false } }
Example 5
Source File: ExecuteStatementOperation.scala From kyuubi with Apache License 2.0 | 5 votes |
package yaooqinn.kyuubi.operation.statement import java.security.PrivilegedExceptionAction import java.util.UUID import java.util.concurrent.RejectedExecutionException import yaooqinn.kyuubi.KyuubiSQLException import yaooqinn.kyuubi.operation._ import yaooqinn.kyuubi.session.KyuubiSession abstract class ExecuteStatementOperation( session: KyuubiSession, statement: String, runAsync: Boolean) extends AbstractOperation(session, EXECUTE_STATEMENT) { protected val statementId: String = UUID.randomUUID().toString protected def execute(): Unit protected def onStatementError(id: String, message: String, trace: String): Unit = { error( s""" |Error executing query as ${session.getUserName}, |$statement |Current operation state ${getStatus.getState}, |$trace """.stripMargin) setState(ERROR) } protected def cleanup(state: OperationState): Unit = { setState(state) if (shouldRunAsync) { val backgroundHandle = getBackgroundHandle if (backgroundHandle != null) { backgroundHandle.cancel(true) } } } override protected def runInternal(): Unit = { setState(PENDING) setHasResultSet(true) val task = new Runnable() { override def run(): Unit = { try { session.ugi.doAs(new PrivilegedExceptionAction[Unit]() { registerCurrentOperationLog() override def run(): Unit = { try { execute() } catch { case e: KyuubiSQLException => setOperationException(e) } } }) } catch { case e: Exception => setOperationException(new KyuubiSQLException(e)) } } } if (shouldRunAsync) { try { // This submit blocks if no background threads are available to run this operation val backgroundHandle = session.getSessionMgr.submitBackgroundOperation(task) setBackgroundHandle(backgroundHandle) } catch { case rejected: RejectedExecutionException => setState(ERROR) throw new KyuubiSQLException("The background threadpool cannot accept" + " new task for execution, please retry the operation", rejected) } } else { task.run() } } private def registerCurrentOperationLog(): Unit = { if (isOperationLogEnabled) { if (operationLog == null) { warn("Failed to get current OperationLog object of Operation: " + getHandle.getHandleIdentifier) isOperationLogEnabled = false } else { session.getSessionMgr.getOperationMgr.setOperationLog(operationLog) } } } override def shouldRunAsync: Boolean = runAsync }