本文整理汇总了Scala中sun.misc.SignalHandler类的典型用法代码示例。如果您正苦于以下问题:Scala SignalHandler类的具体用法?Scala SignalHandler怎么用?Scala SignalHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SignalHandler类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: UnixSignalManager
//设置package包名称以及导入依赖的类
package de.frosner.broccoli.signal
import javax.inject.Singleton
import org.apache.commons.lang3.SystemUtils
import sun.misc.{Signal, SignalHandler}
import scala.collection.mutable
@Singleton
class UnixSignalManager extends SignalManager {
private val signals = mutable.HashMap.empty[Signal, SignalHandler]
def register(signal: Signal, handler: SignalHandler): Unit =
if (SystemUtils.IS_OS_UNIX) {
if (signals.contains(signal)) {
throw new IllegalArgumentException(s"Signal $signal is already registered")
}
Signal.handle(signal, handler)
signals.put(signal, handler)
} else {
throw new UnsupportedOperationException("Signal handling is only supported on UNIX")
}
def unregister(signal: Signal): Unit =
if (signals.contains(signal)) {
Signal.handle(signal, new SignalHandler {
override def handle(signal: Signal): Unit = {}
})
signals.remove(signal)
}
}
示例2: UnixSignalManagerSpec
//设置package包名称以及导入依赖的类
package de.frosner.broccoli.signal
import org.specs2.mock.Mockito
import org.specs2.mutable.Specification
import sun.misc.{Signal, SignalHandler}
class UnixSignalManagerSpec extends Specification with Mockito {
"Registering new signal" should {
"fail if the signal is reserved by the JVM" in {
val manager = new UnixSignalManager()
manager.register(new Signal("USR1"), mock[SignalHandler]) must throwA(
new IllegalArgumentException("Signal already used by VM or OS: SIGUSR1"))
}
"fail if the signal is already registered" in {
val manager = new UnixSignalManager()
val handler = mock[SignalHandler]
manager.register(new Signal("USR2"), handler)
manager.register(new Signal("USR2"), handler) must throwA(
new IllegalArgumentException(s"Signal ${new Signal("USR2")} is already registered"))
}
}
}
示例3: SignalRefreshedTemplateSourceSpec
//设置package包名称以及导入依赖的类
package de.frosner.broccoli.templates
import de.frosner.broccoli.signal.SignalManager
import org.mockito.Mockito.{times, verify}
import org.mockito.{ArgumentCaptor, Matchers}
import org.specs2.mock.Mockito
import org.specs2.mutable.Specification
import sun.misc.{Signal, SignalHandler}
class SignalRefreshedTemplateSourceSpec extends Specification with Mockito {
"Receiving a SIGUSR2 signal" should {
"update the cache" in {
val signalManager = mock[SignalManager]
val testTemplateSource = mock[CachedTemplateSource]
val signalRefreshedTemplateSource = new SignalRefreshedTemplateSource(testTemplateSource, signalManager)
val handler = ArgumentCaptor.forClass(classOf[SignalHandler])
there was one(signalManager).register(Matchers.eq(new Signal("USR2")), handler.capture())
there was no(testTemplateSource).refresh()
there was no(testTemplateSource).loadTemplates()
signalRefreshedTemplateSource.loadTemplates()
there was no(testTemplateSource).refresh()
there was one(testTemplateSource).loadTemplates()
verify(testTemplateSource, times(1)).loadTemplates()
handler.getValue.handle(new Signal("USR2"))
there was one(testTemplateSource).refresh()
there was one(testTemplateSource).loadTemplates()
}
}
}
示例4: UnixSignalManagerIntegrationSpec
//设置package包名称以及导入依赖的类
package de.frosner.broccoli.signal
import org.specs2.mock.Mockito
import org.specs2.mutable.Specification
import sun.misc.{Signal, SignalHandler}
class UnixSignalManagerIntegrationSpec extends Specification with Mockito {
"Registering new signal" should {
"trigger the handler when the signal is raised" in {
val manager = new UnixSignalManager()
val signal = new Signal("USR2")
val handler = mock[SignalHandler]
manager.register(signal, handler)
Signal.raise(signal)
Thread.sleep(1000)
there was one(handler).handle(signal)
}
}
}