Origins of this project: some best practices
- Minimize the payload in the server side session and use a non in-memory session storage backend to provide non-disruptive deployments
For a single server scenario this can be achieved with combining it with Tomcat's parallel deployment feature. [Example]
In a multi server + load balancer set up, this also provides fully transparant fail-over in case of any failure on the hosts.
- Put your (web-)application config files into /etc, make it reloadable, make it possible to overwrite entries via System.setProperty to support config changes in unit-tests. AbstractProperties implements everything you need.
- Put your logging config (like logback.xml) into /etc. Consider making it reloadable and write log files to /var/log. BaseConfigurator makes this very simple.
- Put the git commit hash, build time and build tag during build time into your artifacts and show it somewhere. See VersionFromManifest how this library can help.
- Start right away with secure connections. Use letsencrypt, set up your database connections securely. A script to use letsencrypt with Tomcat.
- Consider jmxtrans to monitor your Tomcat instance. A template config file, using is just adding this to your tomcat start script: JAVA_OPTS="${JAVA_OPTS} -javaagent:/var/lib/tomcat7/jmxtrans/jmxtrans-agent.jar=/var/lib/tomcat7/jmxtrans/jmxtrans.xml" and putting adding the jar.
Project: de.oglimmer.utils
Description
The library helps to prevent boilerplate code for random strings and names, SPI-based slf4j configuration, sophisticated property files, human readable representation of date differences and getting attributes from MANIFEST.FM files.
Source / JavaDoc / Binaries
The sources can be found at github. The javadoc can be found at here. Download/use the latest binaries from maven central.
Detailed information with examples
Class: de.oglimmer.utils.VersionFromManifest
Helps to read attributes from a Manifest file and provides a uniform "version" string which has version, git-commit, creation date and a html-link to git
An example project is on github.
1.) add this to your pom.xml, to add the information into the MANIFEST.FM file: 2.) add this to your pom.xml to retrieve the git commit hash: 3.) use a ServletContextListener.contextInitialized(ServletContextEvent) to get a proper version stringClass: de.oglimmer.utils.BaseConfigurator
Implements SPI-based slf4j configuration. It changes the order and places where slf4j looks for configuration files. The "APP_NAME" is given as a parameter in the constructor. Checks first the system parameter "APP_NAME"-logback for a filename, if this parameter is not given or the file doesn't exist it looks in /etc/logback-custom.xml. If all this doesn't exist it uses /logback-custom.xml in the $CLASSPATH. It also sets the slf4j variable "application-name" with the "APP_NAME".
- Create a class in your project and extend it from de.oglimmer.utils.BaseConfigurator
- Have a parameter less constructor and set your application name in super("....");
- Create META-INF/services/ch.qos.logback.classic.spi.Configurator under src/main/resources
- Put the full qualified filename of your class from (1) into that file
- make sure you have a logback-custom.xml in your project (and not logback.xml)
Class: de.oglimmer.utils.AbstractProperties
A base class for application property classes. Supports json-based config files, automatic reloads when the file changes, merging in-classpath and out-of-classpath files and can take the whole configuration from a string to support unit test configurations.
- Create a (singleton) class in your project and extends it from de.oglimmer.utils.AbstractProperties
- Have a parameter less constructor and pass the name of the system parameter to your production config file to super("...."), e.g. super("my_app")
- create as many getters as you need like
- put a default.properties into src/main/resources. this needs to be a json file and should hold all default/development values for all properties
- On your production host start the server with -Dmy_app=/etc/my_app.properties