Guava: Service
java guava    2018-09-01 00:09:34    0    0    0
cqc   java guava

Service代表一个对象的可运行状态和提供的启动和关闭方法。这是一个管理服务生命周期的框架对象

如何使用

Service的生命周期

Service提供的其他方法
  • addListener()用于监听服务状态变化
  • awaitRunning()阻塞直到服务的状态变为Running
  • awaitTerminated()阻塞直到服务的状态变为Terminated或Failed
Guava不建议直接实现Service接口,其提供了其他的一些基类:
  • AbstractIdleService
  • AbstractExecutionThreadService
  • AbstractScheduledService
  • AbstractService

ServiceManager

当有多个Service需要统一管理时,可以使用ServiceManager。其提供的方法有:
  • startAsync() will start all the services under management. Much like Service#startAsync() you can only call this method once, if all services are NEW.
  • stopAsync() will stop all the services under management.
  • addListener() will add a ServiceManager.Listener that will be called on major state transitions.
  • awaitHealthy() will wait for all services to reach the RUNNING state.
  • awaitStopped() will wait for all services to reach a terminal state.
  • isHealthy() returns true if all services are RUNNING.
  • servicesByState() returns a consistent snapshot of all the services indexed by their state.
  • startupTimes() returns a map from Service under management to how long it took for that service to start in milliseconds. The returned map is guaranteed to be ordered by startup time.

上一篇: Guava:Multimaps

下一篇: Guava:Immutable collections(不可变集合)

文档导航