Nugget Friday - Streamlining Payara Micro Configurations with Pre-boot and Post-boot Scripts
Originally published on 09 Aug 2024
Last updated on 09 Aug 2024
Welcome to another episode of Nugget Friday, where we bring you bite-sized insights to make your development journey smoother and more efficient! This week, we're tackling a common challenge: how to dynamically configure your Payara Micro application after it’s already deployed in a production environment.
The Problem
Imagine you have a Payara Micro application deployed in a production environment. You need to make dynamic configuration changes after the server starts up, such as adjusting logging levels, enabling security, or setting environment-specific properties. Manually connecting to the server and executing asadmin commands each time is not only cumbersome but also prone to errors, especially in a high-availability setup with multiple instances.
The Solution: Payara Micro Pre-boot and Post-boot Scripts
Payara Micro offers a powerful solution to this challenge with pre-boot and post-boot scripts. These scripts allow you to automate configuration tasks at different stages of the server lifecycle, simplifying your deployment process and reducing manual intervention.
How It Works
Pre-boot Scripts
If needed, you can create a pre-boot-commands.txt file containing asadmin commands to be executed before the server starts. These commands are typically used for setting up essential resources or modifying core configurations.
Package with Payara Micro: Bundle your pre-boot script (if applicable) and your application into an Uber JAR using the --outputUberJAR option:
java -jar payara-micro.jar --prebootcommandfile pre-boot-commands.txt --outputUberJAR myapp.jar
Post-boot Script
Create a post-boot-commands.txt file with asadmin commands to be executed after the server has started. This is where you can fine-tune your application's settings dynamically:
# Set logging level for a specific logger
set-log-levels com.example.myapp.logger=FINEST
# Enable security
set configs.config.server-config.security-service.auth-realm.file.file=${com.sun.aas.instanceRoot}/config/keyfile
# Set environment-specific property
create-system-properties environment=production
You can package your post-boot files with the same command as the preboot command, only changing to --prebootcommandfile to --prebootcommandfile.
Deploy and Configure
When you deploy myapp.jar, Payara Micro will first execute the pre-boot script (if present) and then, after the server starts, the post-boot script. Your application will be configured automatically without manual intervention.
Why You Should Care
- Automation: Eliminate repetitive manual tasks, reducing the chance of errors and saving time.
- Consistency: Ensure consistent configurations across multiple instances and environments.
- Flexibility: Easily adapt to changing requirements by modifying your post-boot script.
- Reliability: Reduce downtime caused by manual configuration errors or inconsistencies.
Caveats
- Script Execution Order: Pre-boot scripts run before the server starts, while post-boot scripts run after. Ensure your commands are placed in the correct script accordingly.
- We recommend creating preboot scripts that use simple set commands to change the configuration of existing resources (adding new resources might not work in all cases) like network listeners, standard domain configuration and such.
There's More
There is also a --postdeploycommandfile for executing commands after all deployments have completed.
Conclusions
Payara Micro's pre-boot, post-boot and post-deployment scripts offer a flexible way to manage your application's configuration. By automating these tasks, you can simplify your deployment process, enhance reliability and focus on delivering value to your users. Take a look at the Payara Micro docs on the boot scripts for more usage examples and download Payara Micro here.
Happy Nugget Friday and Happy Coding!
Related Posts
The Payara Monthly Catch - October 2024
Published on 30 Oct 2024
by Chiara Civardi
0 Comments
Can You Futureproof Your Enterprise Java Apps or Are They Doomed to Fall Behind?
Published on 16 Oct 2024
by Chiara Civardi
0 Comments