Dropwizard Commands
The Dropwizard Commands are basic actions. Dropwizard executes commands based on the arguments provided on the command line. For example, the built-in server command starts an HTTP (Hypertext Transfer Protocol) server and runs your program. Every Command subclass has a name and a set of command line parameters that Dropwizard will use to parse the command line arguments.
Dropwizard is often used to build background workers or other non-server applications with Dropwizard. When we do, so there are high chances of using the Dropwizard Command pattern. There are many examples where Dropwizard Commands are used, and managing the lifecycle with its help is one of them.
Let us see a very basic Dropwizard command.
public class WaitCommand extends EnvironmentCommand<AppConfiguration> {
public WaitCommand(Application<AppConfiguration> application) {
super(application, "wait", "Wait for a 5 seconds");
}
@Override
protected void run(Environment environment, Namespace namespace, AppConfiguration configuration) throws Exception {
Logger.getInstance(getClass()).info("Starting the command");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Logger.getInstance(getClass()).info("Done running the Command");
}
}

You can also try this code with Online Java Compiler
Run Code
The command given above is fundamental and helps to print some logs sleeps for five seconds in between. Now what we can do is, the above can also be wired to our application as well.
public class LogApplication extends Application<AppConfiguration> {
public static void main(String[] args) throws Exception {
new LogApplication().run(args);
}
@Override
public void run(AppConfiguration configuration, Environment environment) throws Exception {
}
@Override
public void initialize(Bootstrap<AppConfiguration> bootstrap) {
bootstrap.addCommand(new WaitCommand(this));
bootstrap.setConfigurationSourceProvider(new ResourceConfigurationSourceProvider());
}
}

You can also try this code with Online Java Compiler
Run Code
Here, the name of our application is the ‘LogApplication’. Bootstrap is basically like a template for the application environment and contains everything required.
Creating the server command and the check command are the first steps of the Bootstrapping process.
Another important command is db command. The db command allows executing numerous database actions.
After this, we come to ConfiguredCommands. There are some commands that need access to the configuration parameters and should extend the ConfiguredCommand class, for that matter using the Configuration class of your application as its type parameter.
Dropwizard recognizes our commands once we add them in the initializing stage of our application.
CLI class

Given a Dropwizard Application, the CLI (Command Line Interface) is the Command Line runner. Initializing it followed by running it is the very last step of the Bootstrapping process.
Run will help to handle the command line arguments or will run the configured commands. Note the below given two basic commands that are built for a parent and subclass.
class CheckCommand<T extends Configuration> extends ConfiguredCommand<T>
class ServerCommand<T extends Configuration> extends EnvironmentCommand<T>
Configured Commands

As discussed briefly above, some commands require access to configuration parameters and should extend the ConfiguredCommand class, with the Configuration class from your application as its type parameter. Dropwizard will, by default, treat the last parameter on the command line as the path to a YAML(Yet Another Markup Language) configuration file, parse and validate it, and return an instance of the configuration class to your command.
Additional command line parameters can be supplied for a ConfiguredCommand, although the last argument remains the path to the YAML configuration.
See more, ping command in linux
Frequently Asked Questions
What exactly is Dropwizard bootstrap?
It includes Dropwizard framework properties such as the jersey web container. Bootstrap is the class that connects everything in the Environment, including your Configuration and Application.
What is the purpose of Dropwizard?
Dropwizard is an open-source Java framework for creating high-performance RESTful web services quickly. It assembles several popular libraries to form a lightweight package. Its primary libraries are Jetty, Jersey, Jackson, JUnit, and Guava.
Dropwizard runs on which server?
Dropwizard embeds an extremely tuned HTTP server straight into your project using the Jetty HTTP framework.
Is Dropwizard free and open source?
Dropwizard is an open-source Java framework for creating high-performance, ops-friendly RESTful backends.
How do I use Dropwizard to launch the terminal?
On the main tab, give the configuration a name and specify the primary class. Add server configuration.yml to the program arguments text field on the arguments tab. To launch your Dropwizard application, click the run button or press Ctrl+F11.
Conclusion
We hope this blog gave you a deep understanding of how Dropwizard commands work and what their types are. We discussed the different types of commands and the basic working of Dropwizard Commands with the help of a code. Finally, we moved to Frequently Asked Questions. If you want to know more about Dropwizard, refer to the following articles:
Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.
Keep learning, Keep Growing!