A Bootique Command Demo Application

Using Bootique Commands is a great way to put together tight little JARs to execute scheduled tasks with cron. We'll cover the basics of building a Bootique Command Application in this post.

The purpose of the task we're building with Bootique Commands is to run a MySQL Stored Procedure once a day to update the NixMash Home Page Banner of the Day. I've detailed the Banner of the Day logic here. What our new task does is replace the MySQL Daily Selection Logic previously located in the Post Microservice banner SQL request call.

Application Layout

You'll notice below that the application layout is straightforward. The app compiles to a nice small 8.5 megabyte JAR. And since we're using cron to schedule and run the app there will be nothing staying in memory.

Command Support is built into Bootique so no additional Bootique Modules are required. In this application we are using Bootique JDBC and Bootique Logging along with Bootique Core.

We have two Commands: SetSiteImageCommand and a ClearSiteImageCommand, the latter used only in development. We'll be executing SetSiteImageCommand as a cron job once a day to set the Banner Image of the Day for NixMash.

Adding Commands to Bootique

To make the new command available to Bootique, we'll add it to BQCoreModule’s extender in the Application Launcher config.

A Command Class Example

Here's the complete class for SetSiteImageCommand. Notice when we declare the class we are extending CommandWithMetadata. The core of the Command in our run() method is to set the Banner Image of the Day and write the new image data to a log file. We can add logic to handle if our Command fails if we wish.

Injecting Services in Commands

We can inject services into Bootique Commands but we do so with the Guice Provider instead of using a direct dependency. You see that in the example above with our cmdUI and commandDb services.

Running the Command

Okay, we've got a great little JAR. We specify the command we want to run by its first letter as a command line argument, or "-s" for SetSiteImageCommand. We can change the letter using the .shortname('char') property in the CommandMetadata Builder in the Command Class Constructor.

$ java -jar MyCoolBootiqueApp.jar -s

More Information on Bootique Commands

You'll find good information on using Bootique Commands in the Bootique Documentation. You can see Bootique Commands in action in this Bootique JDBC Demo Application on GitHub.

More Like This Post