In Dropwizard how a custom command class to be execute on application startup startup, please suggest. In command I want to create tables on run time

38 views
Skip to first unread message

ramranjan shukla

unread,
Mar 3, 2020, 2:46:35 PM3/3/20
to dropwizard-dev
Friends,
I am new to dropwizard and have to modify and existing application, please suggest me on my below querries.

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:44:13 PM3/4/20
to dropwizard-dev
Answered in the dropwizard-user forum.

ramranjan shukla

unread,
Mar 5, 2020, 1:37:07 PM3/5/20
to dropwizard-dev
Thanks Peter,
I pass the name of custome command as first command line argument followed by name of yaml file as second argument and it worked for me.
Many thanks.

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