Implementing web consoles in the jvm process using the SonarQube example
By analogy with the example from this article, you can embed the hawtio console into your jvm application without recompiling and recompiling it. And also in the case of DEV / QA hosts, embed the SQL web console process from the H2 project into java
')
Before turning to practice, a small theoretical digression.
In the jvm ecosystem, a huge number of libraries, frameworks and projects are available in the maven repositories: maven central and jcenter. In this publication, we will be interested only in io.hawt: hawtio-app and com.h2database: h2.
hawtio is a universal web console for monitoring and managing a java process with multiple plug-ins and the ability to extend user plugins.
What is contained inside h2 is difficult to enumerate in the framework of even one article. But we are now interested in the fact that this is an embedded database that can also work in server mode and contains inside the web application for working with SQL databases that have a jdbc driver. This is a fairly convenient console with a database schema browser, autocompletion of SQL queries when they are written and localized for the Russian language.
Both consoles work without a separate server, running the embedded web server. But for your projects, you can use almost any suitable maven artifact from the repositories.
And we will work on the SonarQube using the AspectJ-Scripting java agent:
The SonarQube in this publication will be our experimental process - it is one of the leading systems for inspecting the quality of project codes with many plug-ins for different languages and technologies.
AspectJ-Scripting is a modified aspectj agent that instruments classes during loading from jvm, and also downloads artifacts from maven repositories and allows you to describe aspects in the form of MVEL2 scripts and execute code from maven dependencies.
The agent is activated by two additional parameters at the start of jvm and performs all the "magic" of the configuration. It is strange that in their daily work, developers rarely use the java agent technology, although many proprietary tracing solutions and DevOps actively use javaagent: for example, takipi, plumbr, new relic.
So, we return to modifying java processes only by configuring jvm parameters using the example of SonarQube and implementing web consoles.
Download the zip archive from the project site and unpack it into a local directory.
In order for the aspectj-scripting agent to work, it must be passed as the -javaagent: aspectj-scripting-1.0-agent.jar parameter when starting jvm, and the configuration is through the -Dorg.aspectj.weaver.loadtime.configuration = config: file property: PATH_K_CONFIGURATION . Through the system property parameters, you can pass the maven proxy repository and the path to the local maven repository.
All this for the SonarQube is configured in the conf / sonar.properties file in the sonar.web.javaAdditionalOpts property
The video shows how SonarQube is configured to run in conjunction with hawtio_console.xml and hawt.io. The console starts.
The following screencast shows how the agent is launched with the parameter database_console.xml and starts the SQL console
Finally, both consoles start in the SonarQube process by configuring h2_hawtio.xml , for example, we import the mvel project metrics into Sonar and use the console to look at the tables inside the sonar database
Making queries to the SonarQube database from the H2 console
For metrics in SonarQube for the MVEL library
Access to the statistics of the H2 database in the JMX hawtio tab can be obtained by adding "JMX = TRUE" to the sonar.jdbc.url property in the conf / sonar.properties file : sonar.jdbc.url = jdbc: h2: tcp: // localhost: 9092 / sonar; JMX = TRUE
JMX java.lang.OperatingSystem charts overview:
So, we were able to modify the existing SonarQube application so that when starting, the hawtio and h2 from the maven repository and the web console were launched at the right time when the sonar started.
In the next post, I’ll show how to embed an aspectj-scripting interactive ssh console into an existing java application. And also consider a couple of options for how to log jdbc requests and their parameters in the application, during the development of which did not think about this functionality.
For those who wish to visit on September 9 a report on the topic “Aspect-oriented programming in distributed systems for java developers and QA” register
Good luck in analyzing and monitoring java applications