Latest test with Magnolia CMS 6.2.44 Community Edition, PostgreSQL 16.2 and JavaMelody 1.98.0 running on JDK 17 and Tomcat 9.0.88.
Monitoring your Java Web Application
Many software projects are made up of components that can be difficult to monitor. During the development phase of an application, it can be crucial to get information about bottlenecks that might prevent a production deployment of your project.
One performance metric can be the number of unnecessary SQL statements executed during the use of your application - this is a use case I had in a Magnolia CMS project to increase performance.
Adding JavaMelody to an instance of Magnolia CMS
JavaMelody is an open source project that provides tools and a user interface for monitoring Java applications. It is not used to simulate the load on your system, but to provide statistics and logs to analyze Java applications in production and during QA. To monitor SQL statements as in the scenario above, you must set up a JNDI-based data source in Tomcat.
Magnolia Project Setup
Since Magnolia projects are based on Maven, we will include the required library in the POM of our Magnolia webapp.
Magnolia webapp POM
<!-- https://mvnrepository.com/artifact/net.bull.javamelody/javamelody-core -->
<dependency>
<groupId>net.bull.javamelody</groupId>
<artifactId>javamelody-core</artifactId>
<version>1.98.0</version>
</dependency>
Warning: There is a filter/configuration change in the 2.0.x series of JavaMelody that breaks the Magnolia bundle. I recommend staying with the 1.x branch until this issue is resolved!
Basic web.xml setup
To provide the most basic configuration, add the following configuration to web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:/java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<description>Magnolia</description>
<display-name>magnolia</display-name>
<distributable />
<filter>
<filter-name>javamelody</filter-name>
<filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>javamelody</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>net.bull.javamelody.SessionListener</listener-class>
</listener>
<filter>
<display-name>Magnolia global filters</display-name>
<filter-name>magnoliaFilterChain</filter-name>
<filter-class>info.magnolia.cms.filters.MgnlMainFilter</filter-class>
</filter>
… more configuration …
</web-app>
Note that I used „javamelody“ as the name of the filter, not „monitoring“ as in most examples on the web. This is because some of the options shown below won't work if you're using Java 8 with your project (you shouldn't, but...).
See this issue on GitHub for more information. With this configuration, you have configured JavaMelody to be available with your Magnolia project. After starting your Magnolia instance, go to
http://server:port/your-webapp-context/monitoring
to open JavaMelody. Note that „monitoring“ is the default name for accessing the web interface.
Setup with advanced configuration
This blog entry written by Siegfried Goeschl gives an introduction on how to configure some of the options for using JavaMelody within your Java-based web application.
Check out the filter configuration below:
<filter>
<filter-name>javamelody</filter-name>
<filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
<init-param>
<param-name>authorized-users</param-name>
<param-value>superuser:superuser</param-value>
</init-param>
<init-param>
<param-name>monitoring-path</param-name>
<param-value>/mypath/javamelody</param-value>
</init-param>
<init-param>
<param-name>storage-directory</param-name>
<param-value>/Users/exampleuser/temp/javamelody</param-value>
</init-param>
</filter>
The advanced filter configuration shows you how to
- add basic authentication credentials
- adjust the path where JavaMelody can be accessed
- change the directory where historical data is stored
Further information
Please refer to the official project documentation on GitHub for more information on configuring JavaMelody. By adding this tool to your Magnolia CMS project, you will quickly have a tool for monitoring and collecting statistics about the performance of your application.
Summary
The screenshot shows an example of SQL statements executed against the JCR repository while using Magnolia CMS AdminCentral. This is just one of the reports you can get automatically when integrating JavaMelody into your Magnolia CMS or Java web application.
With a few lines of configuration, we were able to get performance metrics on our Java-based application.