Tutorial describing how to integrate JavaMelody with Spring or Spring Boot 1 application. If we are developing the Spring Boot 2 application, we can use the javamelody-spring-boot-starter
as described here. In other case we have to provide own confguration as follows.
Technologies used:
- Spring Boot 1.5.22.RELEASE
- JDK 1.8
- Maven 3
- JavaMelody
Project setup
Project dependencies managed by the Maven’s pom.xml configuration file:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.22.RELEASE</version> <relativePath/> </parent> <groupId>com.devcases.spring</groupId> <artifactId>spring-javamelody</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>net.bull.javamelody </groupId> <artifactId>javamelody-core</artifactId> <version>1.80.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
The explanation of above dependencies:
spring-boot-starter-web
– packages all the necessary dependencies together with the auto configuration for running simple web applicationjavamelody-core
– provides dependencies for JavaMelody monitoring toolsspring-boot-maven-plugin
– plugin prividing commands to work with a Spring Boot application (we will be usine thespring-boot:run
command)
JavaMelody Configuration
@Configuration
public class JavaMelodyConfiguration {
@Bean
public SessionListener monitoringSessionListener() {
return new SessionListener();
}
@Bean
public FilterRegistrationBean monitoringFilter() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
MonitoringFilter filter = new MonitoringFilter();
registrationBean.setFilter(filter);
registrationBean.setAsyncSupported(true);
registrationBean.setName("javamelody");
registrationBean.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC);
registrationBean.addInitParameter(Parameter.LOG.getCode(), Boolean.toString(true));
registrationBean.addUrlPatterns("/*");
return registrationBean;
}
}
The minimal configuration for enabling JavaMelody defines two beans:
- monitoringSessionListener – defines JavaMelody session listener
- monitoringFilter – defines monitoring filter. Thanks to using
FilterRegistrationBean
we have better control over the custom initialization of theMonitoringFilter
.
Application Initializer
@SpringBootApplication
public class SpringBootWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
Controller
@RestController
public class HelloWorldController {
@GetMapping("/hello")
public String hello() {
return "Hello World";
}
}
Running
The project can be started by the running the following command:
mvn clean spring-boot:run
By visiting the localhost:8080/hello
url, we should see the page with “Hello World” text.
By visiting the localhost:8080/monitoring
url, we can see JavaMelody reports.
The example code you can download from here