Packages

trait App extends ClosableOnce with CloseOnceAwaitably with Lifecycle

A composable application trait that includes flag parsing as well as basic application lifecycle (pre- and post- main). Flag parsing is done via com.twitter.app.Flags, an instance of which is defined in the member flag. Applications should be constructed with modularity in mind, and common functionality should be extracted into mixins.

Flags should only be constructed in the constructor, and should only be read in the premain or later, after they have been parsed.

import com.twitter.app.App

object MyApp extends App {
  val n = flag("n", 100, "Number of items to process")

  def main(): Unit = {
    for (i <- 0 until n()) process(i)
  }
}

Note that a missing main is OK: mixins may provide behavior that does not require defining a custom main method.

Linear Supertypes
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. App
  2. Lifecycle
  3. CloseOnceAwaitably
  4. CloseOnceAwaitably0
  5. Awaitable
  6. ClosableOnce
  7. CloseOnce
  8. Closable
  9. AnyRef
  10. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final val MinGrace: Duration

    Minimum duration to allow for exits to be processed.

  5. def allowUndefinedFlags: Boolean

    Whether or not to accept undefined flags

    Whether or not to accept undefined flags

    Attributes
    protected
  6. def args: Array[String]

    The remaining, unparsed arguments

  7. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  8. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native()
  9. final def close(deadline: Time): Future[Unit]

    Close the resource with the given deadline.

    Close the resource with the given deadline. This deadline is advisory, giving the callee some leeway, for example to drain clients or finish up other tasks.

    Definition Classes
    CloseOnceClosable
  10. def close(after: Duration): Future[Unit]

    Close the resource with the given timeout.

    Close the resource with the given timeout. This timeout is advisory, giving the callee some leeway, for example to drain clients or finish up other tasks.

    Definition Classes
    Closable
  11. final def close(): Future[Unit]

    Close the resource.

    Close the resource. The returned Future is completed when the resource has been fully relinquished.

    Definition Classes
    Closable
  12. final def closeFuture: Future[Unit]

    The Future satisfied upon completion of close.

    The Future satisfied upon completion of close.

    Attributes
    protected
    Definition Classes
    CloseOnce
    Note

    we do not expose direct access to the underlying closePromise, because the Promise state is mutable - we only want mutation to occur within this CloseOnce trait.

  13. final def closeOnExit(closable: Closable): Unit

    Close closable when shutdown is requested.

    Close closable when shutdown is requested. Closables are closed in parallel.

  14. final def closeOnExitLast(closable: Closable): Unit

    Register a closable to be closed on application shutdown after those registered via closeOnExit.

    Register a closable to be closed on application shutdown after those registered via closeOnExit.

    Note

    Application shutdown occurs in two sequential phases to allow explicit encoding of resource lifecycle relationships. Concretely this is useful for encoding that a monitoring resource should outlive a monitored resource. In all cases, the close deadline is enforced.

  15. def closeOnce(deadline: Time): Future[Unit]

    Close the resource with the given deadline exactly once.

    Close the resource with the given deadline exactly once. This deadline is advisory, giving the callee some leeway, for example to drain clients or finish up other tasks.

    Attributes
    protected
    Definition Classes
    AppCloseOnce
    Note

    if this method throws a synchronous exception, that exception will be wrapped in a failed future. If a fatal error is thrown synchronously, the error will be wrapped in a failed Future AND thrown directly.

  16. def defaultCloseGracePeriod: Duration

    Default amount of time to wait for shutdown.

    Default amount of time to wait for shutdown. This value is not used as a default if close() is called without parameters. It simply provides a default value to be passed as close(grace).

  17. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  18. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  19. def exitOnError(reason: String, details: => String): Unit

    The details Fn may be an expensive operation (which could fail).

    The details Fn may be an expensive operation (which could fail). We want to ensure that we've at least written the reason field to System.err before attempting to write the detail field so that users will at a minimum see the reason for the exit regardless of any extra details.

    Attributes
    protected
  20. def exitOnError(reason: String): Unit

    Exit on error with the given reason String

    Exit on error with the given reason String

    Attributes
    protected
  21. def exitOnError(throwable: Throwable): Unit

    Exit on error with the given Throwable

    Exit on error with the given Throwable

    Attributes
    protected
  22. def failfastOnFlagsNotParsed: Boolean

    Users of this code should override this to true so that you fail-fast instead of being surprised at runtime by code that is reading from flags before they have been parsed.

    Users of this code should override this to true so that you fail-fast instead of being surprised at runtime by code that is reading from flags before they have been parsed.

    Ideally this would default to true, however, in order to avoid breaking existing users, it was introduced using false.

    Attributes
    protected
  23. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
  24. final def flag: Flags

    The com.twitter.app.Flags instance associated with this application

  25. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  26. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  27. def includeGlobalFlags: Boolean

    Whether or not to include GlobalFlag's when Flags are parsed

    Whether or not to include GlobalFlag's when Flags are parsed

    Attributes
    protected
  28. final def init(f: => Unit): Unit

    Invoke f before anything else (including flag parsing).

    Invoke f before anything else (including flag parsing).

    Attributes
    protected
  29. final def isClosed: Boolean

    Signals whether or not this Closable has been closed.

    Signals whether or not this Closable has been closed.

    returns

    return true if close has been initiated, false otherwise

    Definition Classes
    CloseOnce
  30. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  31. def isReady(implicit permit: CanAwait): Boolean

    Is this Awaitable ready? In other words: would calling Awaitable.ready block?

    Is this Awaitable ready? In other words: would calling Awaitable.ready block?

    Definition Classes
    CloseOnceAwaitably0 → Awaitable
  32. def loadServiceBindings: Seq[Binding[_]]

    Programmatically specify which implementations to use in

    Programmatically specify which implementations to use in

    LoadService.apply for the given interfaces. This allows applications to circumvent the standard service loading mechanism when needed. It may be useful if the application has a broad and/or rapidly changing set of dependencies.

    For example, to require SuperCoolMetrics be used as the com.twitter.finagle.stats.StatsReceiver implementation:

    import com.initech.SuperCoolMetrics
    import com.twitter.app.App
    import com.twitter.app.LoadService.Binding
    import com.twitter.finagle.stats.StatsReceiver
    
    class MyApp extends App {
      val implementationToUse = new SuperCoolMetrics()
    
      override protected[this] val loadServiceBindings: Seq[Binding[_]] = {
        Seq(new Binding(classOf[StatsReceiver], implementationToUse))
      }
    
      def main(): Unit = {
        val loaded = LoadService[StatsReceiver]()
        assert(Seq(implementationToUse) == loaded)
      }
    }

    If this is called for a Class[T] after LoadService.apply has been called for that same interface, an IllegalStateException will be thrown. For this reason, bindings are done as early as possible in the application lifecycle, before even inits and flag parsing.

    returns

    a mapping from Class to the 1 or more implementations to be used by LoadService.apply for that interface.

    Attributes
    protected[this]
    Note

    this should not generally be used by "libraries" as it forces their user's implementation choice.

    See also

    LoadService.apply

  33. final def main(args: Array[String]): Unit
  34. def name: String

    The name of the application, based on the classname

  35. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  36. final def nonExitingMain(args: Array[String]): Unit
  37. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  38. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  39. final def observe(event: Event)(f: => Unit): Unit

    Notifies Observers of Events.

    Notifies Observers of Events.

    Attributes
    protected
    Definition Classes
    Lifecycle
  40. final def observeFuture(event: Event)(f: Future[Unit]): Future[Unit]

    Notifies Observers of com.twitter.util.Future Events.

    Notifies Observers of com.twitter.util.Future Events.

    Attributes
    protected
    Definition Classes
    Lifecycle
  41. final def onExit(f: => Unit): Unit

    Invoke f when shutdown is requested.

    Invoke f when shutdown is requested. Exit hooks run in parallel and are executed after all postmains complete. The thread resumes when all exit hooks complete or closeDeadline expires.

    Attributes
    protected
    See also

    runOnExit(Runnable) for a variant that is more suitable for Java.

  42. final def onExitLast(f: => Unit): Unit

    Invoke f when shutdown is requested.

    Invoke f when shutdown is requested. Exit hooks run in parallel and are executed after all closeOnExit functions complete. The thread resumes when all exit hooks complete or closeDeadline expires.

    Attributes
    protected
    See also

    runOnExitLast(Runnable) for a variant that is more suitable for Java.

  43. def parseArgs(args: Array[String]): Unit

    Parse the command line arguments as an Array of Strings.

    Parse the command line arguments as an Array of Strings. The default implementation parses the given String[] as Flag input values.

    Users may override this method to specify different functionality.

    args

    String Array which represents the command line input given to the application.

    Attributes
    protected[this]
  44. final def postmain(f: => Unit): Unit

    Invoke f after the user's main has exited.

    Invoke f after the user's main has exited.

    Attributes
    protected
  45. final def premain(f: => Unit): Unit

    Invoke f right before the user's main is invoked.

    Invoke f right before the user's main is invoked.

    Attributes
    protected
  46. def ready(timeout: Duration)(implicit permit: CanAwait): App.this.type

    Support for Await.ready.

    Support for Await.ready. The use of the implicit permit is an access control mechanism: only Await.ready may call this method.

    Definition Classes
    CloseOnceAwaitably0 → Awaitable
  47. def result(timeout: Duration)(implicit permit: CanAwait): Unit

    Support for Await.result.

    Support for Await.result. The use of the implicit permit is an access control mechanism: only Await.result may call this method.

    Definition Classes
    CloseOnceAwaitably0 → Awaitable
  48. final def runOnExit(runnable: Runnable): Unit

    Invoke runnable.run() when shutdown is requested.

    Invoke runnable.run() when shutdown is requested. Exit hooks run in parallel and are executed after all postmains complete. The thread resumes when all exit hooks complete or closeDeadline expires.

    This is a Java friendly API to allow Java 8 users to invoke onExit with lambda expressions.

    For example (in Java):

    runOnExit(() -> {
     clientA.close();
     clientB.close();
    });
    Attributes
    protected
    See also

    => Unit) for a variant that is more suitable for Scala.

  49. final def runOnExitLast(runnable: Runnable): Unit

    Invoke runnable.run() when shutdown is requested.

    Invoke runnable.run() when shutdown is requested. Exit hooks run in parallel and are executed after all closeOnExit functions complete. The thread resumes when all exit hooks complete or closeDeadline expires.

    This is a Java friendly API to allow Java 8 users to invoke onExitLast with lambda expressions.

    For example (in Java):

    runOnExitLast(() -> {
     clientA.close();
     clientB.close();
    });
    Attributes
    protected
    See also

    => Unit) for a variant that is more suitable for Scala.

  50. lazy val shutdownTimer: Timer
    Attributes
    protected
  51. def suppressGracefulShutdownErrors: Boolean

    By default any failure during the graceful shutdown portion of an App's lifecycle bubbles up and causes non-zero return codes in the process.

    By default any failure during the graceful shutdown portion of an App's lifecycle bubbles up and causes non-zero return codes in the process. Setting this to false allows an application to suppress these errors and express that graceful shutdown logic should not be a determinant of the process exit code.

    Attributes
    protected
  52. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  53. def toString(): String
    Definition Classes
    AnyRef → Any
  54. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  55. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  56. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()

Inherited from Lifecycle

Inherited from CloseOnceAwaitably

Inherited from CloseOnceAwaitably0[Unit]

Inherited from Awaitable[Unit]

Inherited from ClosableOnce

Inherited from CloseOnce

Inherited from Closable

Inherited from AnyRef

Inherited from Any

Ungrouped