There are components in many software projects that can be difficult to monitor. Information about bottlenecks that might obstruct a production launch of your project is crucial during the creation phase.
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
JavaMelody is an open-source project that offers tools and a user interface for monitoring Java applications. Statistics and logs are provided to analyze Java applications in production and during quality assurance, not to simulate the load on your system. To monitor SQL statements, you must set up a JNDI-based data source in Tomcat.
Magnolia Project Setup
Since Magnolia projects use Maven, we will add the necessary libraries to our Magnolia webapp’s POM.
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>
The Magnolia bundle has been broken by a filter/configuration change in the 2.0.x series of JavaMelody. I suggest using the 1.x version until the problem is addressed.
Basic web.xml setup
To provide the most basic configuration, you can 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…).
Look at this issue on GitHub for more details. With this configuration, you have successfully configured JavaMelody to be available with your Magnolia project. After you have started your Magnolia instance, navigate to
http://server:port/your-webapp-context/monitoring
to access JavaMelody. Please note that „monitoring“ is the default name for accessing the web interface.
Advanced configuration
You can 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 further information about configuring JavaMelody. Adding this tool to your Magnolia CMS project will give you a quick way to track and analyze your application’s performance.
Summary
The screenshot shows SQL statements that were run against the JCR repository while using Magnolia CMS AdminCentral. This is just one of the reports you will receive automatically when integrating JavaMelody into your Magnolia CMS or Java web application.
With just a few lines of configuration, we were able to get performance metrics on our Java-based application.