Class KiwiDropwizardLifecycles

java.lang.Object
org.kiwiproject.dropwizard.lifecycle.KiwiDropwizardLifecycles

public final class KiwiDropwizardLifecycles extends Object
Provides utilities related to the Dropwizard lifecycle.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    manage(io.dropwizard.lifecycle.setup.LifecycleEnvironment lifecycle, Runnable startAction, Runnable stopAction)
    Creates a Dropwizard Managed whose start action is startAction and whose stop action is stopAction, and attaches it to the given Dropwizard lifecycle.
    static void
    manageOnlyStart(io.dropwizard.lifecycle.setup.LifecycleEnvironment lifecycle, Runnable startAction)
    Creates a Dropwizard Managed whose start action is startAction, and attaches it to the given Dropwizard lifecycle.
    static void
    manageOnlyStop(io.dropwizard.lifecycle.setup.LifecycleEnvironment lifecycle, Runnable stopAction)
    Creates a Dropwizard Managed whose stop action is stopAction, and attaches it to the given Dropwizard lifecycle.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • manage

      public static void manage(io.dropwizard.lifecycle.setup.LifecycleEnvironment lifecycle, Runnable startAction, Runnable stopAction)
      Creates a Dropwizard Managed whose start action is startAction and whose stop action is stopAction, and attaches it to the given Dropwizard lifecycle.

      Useful when you have some external object that has start and stop methods, but you don't want to clutter your code by creating an anonymous inner class just to specify the start and stop actions. For example, if you have an ActiveMQ PooledConnectionFactory (which has start and stop methods) you can simply call this method:

      KiwiDropwizardLifecycles.manage(lifecycle, () -> factory.start(), () -> factory.stop());

      To make the code cleaner, use method references:

      KiwiDropwizardLifecycles.manage(lifecycle, factory::start, factory::stop);

      Parameters:
      lifecycle - the lifecycle to manage
      startAction - the action to run when Dropwizard starts the application
      stopAction - the action to run when Dropwizard stops the application
    • manageOnlyStart

      public static void manageOnlyStart(io.dropwizard.lifecycle.setup.LifecycleEnvironment lifecycle, Runnable startAction)
      Creates a Dropwizard Managed whose start action is startAction, and attaches it to the given Dropwizard lifecycle.

      Useful when you have some external object that has a start method, but you don't want to clutter your code by creating an anonymous inner class just to specify the start action. For example, if you have a monitoring class that needs to start when the application starts, you can ensure that happens using code like:

      KiwiDropwizardLifecycles.manage(lifecycle, () -> monitor.start());

      To make the code cleaner, use method references:

      KiwiDropwizardLifecycles.manage(lifecycle, monitor::start);

      Parameters:
      lifecycle - the lifecycle to manage
      startAction - the action to run when Dropwizard starts the application
    • manageOnlyStop

      public static void manageOnlyStop(io.dropwizard.lifecycle.setup.LifecycleEnvironment lifecycle, Runnable stopAction)
      Creates a Dropwizard Managed whose stop action is stopAction, and attaches it to the given Dropwizard lifecycle.

      Useful when you have some external object that has a stop method, but you don't want to clutter your code by creating an anonymous inner class just to specify the stop action. For example, if you have an HTTP Client class that needs to stop when the application shuts down to ensure resources are properly closed, you can ensure that happens using code like:

      KiwiDropwizardLifecycles.manage(lifecycle, () -> client.close());

      To make the code cleaner, use method references:

      KiwiDropwizardLifecycles.manage(lifecycle, client::close);

      Parameters:
      lifecycle - the lifecycle to manage
      stopAction - the action to run when Dropwizard stops the application