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.
- Alphabetic
- By Inheritance
- App
- Lifecycle
- CloseOnceAwaitably
- CloseOnceAwaitably0
- Awaitable
- ClosableOnce
- CloseOnce
- Closable
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
val
MinGrace: Duration
Minimum duration to allow for exits to be processed.
-
def
allowUndefinedFlags: Boolean
Whether or not to accept undefined flags
Whether or not to accept undefined flags
- Attributes
- protected
-
def
args: Array[String]
The remaining, unparsed arguments
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
final
def
close(deadline: Time): Future[Unit]
- Definition Classes
- CloseOnce
-
def
close(after: Duration): Future[Unit]
- Definition Classes
- Closable
-
final
def
close(): Future[Unit]
- Definition Classes
- Closable
-
final
def
closeFuture: Future[Unit]
- Attributes
- protected
- Definition Classes
- CloseOnce
-
final
def
closeOnExit(closable: Closable): Unit
Close
closablewhen shutdown is requested.Close
closablewhen shutdown is requested. Closables are closed in parallel. -
final
def
closeOnExitLast(closable: Closable): Unit
Register a
closableto be closed on application shutdown after those registered viacloseOnExit.Register a
closableto be closed on application shutdown after those registered viacloseOnExit.- 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.
-
def
closeOnce(deadline: Time): Future[Unit]
- Attributes
- protected
- Definition Classes
- App → CloseOnce
-
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 asclose(grace). -
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
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
reasonfield to System.err before attempting to write thedetailfield so that users will at a minimum see thereasonfor the exit regardless of any extra details.- Attributes
- protected
-
def
exitOnError(reason: String): Unit
Exit on error with the given
reasonStringExit on error with the given
reasonString- Attributes
- protected
-
def
exitOnError(throwable: Throwable): Unit
Exit on error with the given Throwable
Exit on error with the given Throwable
- Attributes
- protected
-
def
failfastOnFlagsNotParsed: Boolean
Users of this code should override this to
trueso 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
trueso 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 usingfalse.- Attributes
- protected
-
def
finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
val
flag: Flags
The com.twitter.app.Flags instance associated with this application
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
init(f: ⇒ Unit): Unit
Invoke
fbefore anything else (including flag parsing).Invoke
fbefore anything else (including flag parsing).- Attributes
- protected
-
final
def
isClosed: Boolean
- Definition Classes
- CloseOnce
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
isReady(implicit permit: CanAwait): Boolean
- Definition Classes
- CloseOnceAwaitably0 → Awaitable
-
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
SuperCoolMetricsbe used as thecom.twitter.finagle.stats.StatsReceiverimplementation: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, anIllegalStateExceptionwill be thrown. For this reason, bindings are done as early as possible in the application lifecycle, before eveninitsand flag parsing.- returns
a mapping from
Classto 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
- final def main(args: Array[String]): Unit
-
val
name: String
The name of the application, based on the classname
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def nonExitingMain(args: Array[String]): Unit
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
observe(event: Event)(f: ⇒ Unit): Unit
Notifies
Observers ofEvents.Notifies
Observers ofEvents.- Attributes
- protected
- Definition Classes
- Lifecycle
-
final
def
observeFuture(event: Event)(f: Future[Unit]): Future[Unit]
Notifies
Observers of com.twitter.util.FutureEvents.Notifies
Observers of com.twitter.util.FutureEvents.- Attributes
- protected
- Definition Classes
- Lifecycle
-
final
def
onExit(f: ⇒ Unit): Unit
Invoke
fwhen shutdown is requested.Invoke
fwhen shutdown is requested. Exit hooks run in parallel and are executed after all postmains complete. The thread resumes when all exit hooks complete orcloseDeadlineexpires.- Attributes
- protected
- See also
runOnExit(Runnable) for a variant that is more suitable for Java.
-
final
def
onExitLast(f: ⇒ Unit): Unit
Invoke
fwhen shutdown is requested.Invoke
fwhen shutdown is requested. Exit hooks run in parallel and are executed after all closeOnExit functions complete. The thread resumes when all exit hooks complete orcloseDeadlineexpires.- Attributes
- protected
- See also
runOnExitLast(Runnable) for a variant that is more suitable for Java.
-
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]
-
final
def
postmain(f: ⇒ Unit): Unit
Invoke
fafter the user's main has exited.Invoke
fafter the user's main has exited.- Attributes
- protected
-
final
def
premain(f: ⇒ Unit): Unit
Invoke
fright before the user's main is invoked.Invoke
fright before the user's main is invoked.- Attributes
- protected
-
def
ready(timeout: Duration)(implicit permit: CanAwait): App.this.type
- Definition Classes
- CloseOnceAwaitably0 → Awaitable
-
def
result(timeout: Duration)(implicit permit: CanAwait): Unit
- Definition Classes
- CloseOnceAwaitably0 → Awaitable
-
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 orcloseDeadlineexpires.This is a Java friendly API to allow Java 8 users to invoke
onExitwith 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.
-
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 orcloseDeadlineexpires.This is a Java friendly API to allow Java 8 users to invoke
onExitLastwith 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.
-
lazy val
shutdownTimer: Timer
- Attributes
- protected
-
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
falseallows an application to suppress these errors and express that graceful shutdown logic should not be a determinant of the process exit code.- Attributes
- protected
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()