13

A Bootique Command Demo Application

 4 years ago
source link: https://nixmash.com/java/a-bootique-command-demo-application/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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.

bqcmd1011a.png

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.

bqcmd1011b.png

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.

bqcmd1011c.png

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.

bqcmd1011d.png

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.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK