

A Bootique Command Demo Application
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.

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.
Recommend
-
186
Symfony Demo Application The "Symfony Demo Application" is a reference application created to show how to develop applications following the Symfony Best Practi...
-
116
Symfony Demo Application The "Symfony Demo Application" is a reference application created to show how to develop applications following the Symfony Best Practi...
-
53
bootique - Bootique is a minimally opinionated platform for modern runnable Java apps.
-
8
Flink SQL Demo: Building an End-to-End Streaming Application 28 Jul 2020 Jark Wu (@JarkWu) Apache Flink 1.11 has released many exciting new features, including many d...
-
18
Bearer Token Design in Bootique Shiro for REST Authentication My interest in using Bearer Authentication Tokens in Microservices began after reading
-
39
Single and Multiple File Uploads with Jersey and Bootique You'll find information on uploading single and multiple files with Jersey in many places, but there are a few details I'll emphasize in this post which may prove help...
-
14
A Bootique Logback Configuration File Example Probably every Bootique Application uses the Bootique Logback Module. All of my Bootique Apps do, yet I've always used a logback.xml file instead of a bootique.yml configuration. One of t...
-
9
Generic Type Injection Examples in Bootique and Guice Back when I was working in Spring I built a Jsoup Annotation-based parsing library which I blogged about here. It...
-
18
NixMash.com is built with the Bootique Java Framework. Modules include Bootique Shiro, Jersey, RabbitMQ Client, MVC, Logback and Jetty. Bootique 0.25 was just released so I didn't waste any time upgrading....
-
9
In this post I'm going to demonstrate using RabbitMQ RPC with the Bootique RabbitMQ Module. You can find RabbitMQ RPC examples elsewhere like in the RabbitMQ Java Examp...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK