Dropwizard custome command class to execute on application startup startup

35 views
Skip to first unread message

rshukla21

unread,
Mar 3, 2020, 2:31:42 PM3/3/20
to dropwizard-user
Dear Friends,
I am new to dropwizard and have to modify something into existing application classes.

1. I want to execute my command class when my main application class gets executed as in my custom command class I have create database tables on run time.
2. While I write Bootstrap.addCommand((new MyCommand("ganesh", "ram")); , this will create instance but run method of MyCommand is not getting executed, am I missing something here to add some code?

Code Examples which I am trying.

public class App extends Application<MyConfig> {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);

@Override
public void initialize(Bootstrap<MyConfig> b) {
System.out.println(">>inside initialize");
b.addCommand(new MyCommand("ganesh", "ram"));
}

@Override
public void run(MyConfig c, Environment e) throws Exception 
{
// here some more statements which gets executed when App class runs.

       }
}

//Command class

public class MyCommand extends ConfiguredCommand<MyConfig>{

protected MyCommand(String name, String description) {
super(name, description);
// TODO Auto-generated constructor stub
}

@Override
protected void run(Bootstrap<MyConfig> bootstrap, Namespace namespace, MyConfig configuration)
throws Exception {
//Here I will write code to creat tables but how this run method will execute automatically?
}

}


Thanks,
Ram


Peter Stackle

unread,
Mar 4, 2020, 4:43:31 PM3/4/20
to dropwizard-user
Can you share an example of the command you are executing from the command line (or IDE program arguments) that you are using?

The getting started guide walks through an example using the out-of-the-box "server" command (https://www.dropwizard.io/en/latest/getting-started.html#running-your-application). In that case you have something like the following:
java -jar target/hello-world-0.0.1-SNAPSHOT.jar server hello-world.yml
In the case of using your custom command, you'd need to pass the command name as the first argument (in place of "server"). For example:

java -jar target/hello-world-0.0.1-SNAPSHOT.jar ganesh

Friso Vrolijken

unread,
Mar 5, 2020, 8:20:02 AM3/5/20
to dropwizard-user
If I understand you correctly you're looking for something like this: https://www.dropwizard.io/en/latest/manual/migrations.html


Ram

unread,
Mar 5, 2020, 1:33:20 PM3/5/20
to dropwizard-user
Hi Peter,
Many thanks for quick response, it worked for me :) . In the case of using your custom command, you'd need to pass the command name as the first argument (in place of "server")
followed by yaml name.

Cheers'
Ram

Ram

unread,
Mar 5, 2020, 1:35:03 PM3/5/20
to dropwizard-user
Hi Friso,

It is resolved now I pass the custom command name as first commandline arg followed by name of yaml file. Thanks for your quick response.

Cheers'
Ram
Reply all
Reply to author
Forward
0 new messages