Java melody configuration in maven spring boot with security
Posted By : Md Imroz Alam | 27-Dec-2017
Java melody is a monitoring tool for analyzing our application performance.
We can see actual db connection, CPU utilization, how many user session active for our application.
Where is bottleneck area application going down? what exact time db query takes. Where are the area optimization required? These are above detail we can fetch from java melody configuration.
we can also configure java melody to sonarQube, Jenkins automation tool. It is easy to use and time-saving monitoring tool for production environment application.
net.bull.javamelody javamelody-spring-boot-starter 1.70.0
org.springframework.boot spring-boot-starter-aop
@Bean class
package com.javamelody.service;
import net.bull.javamelody.*;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.DispatcherType;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
@Configuration
@ImportResource("classpath:net/bull/javamelody/monitoring-spring-aspectj.xml")
public class JavaMelodyConfiguration implements ServletContextInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.addListener(new SessionListener());
}
// Main function of java melody configuration, to enable monitoring url
@Bean
public FilterRegistrationBean javaMelody() {
final FilterRegistrationBean javaMelody = new FilterRegistrationBean();
javaMelody.setFilter(new MonitoringFilter());
javaMelody.setAsyncSupported(true);
javaMelody.setName("javamelody");
javaMelody.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC);
javaMelody.addInitParameter(Parameter.LOG.getCode(), Boolean.toString(true));
javaMelody.addUrlPatterns("/*");
return javaMelody;
}
// jdbc connection monitoring
@Bean
public SpringDataSourceBeanPostProcessor monitoringDataSourceBeanPostProcessor() {
SpringDataSourceBeanPostProcessor processor = new SpringDataSourceBeanPostProcessor();
processor.setExcludedDatasources(null);
return processor;
}
// Monitoring those classes whose added @MonitoringWithSpring
@Bean
public MonitoringSpringAdvisor monitoringAdvisor() {
final MonitoringSpringAdvisor interceptor = new MonitoringSpringAdvisor();
interceptor.setPointcut(new MonitoredWithAnnotationPointcut());
return interceptor;
}
// Monitoring all function and classes those without @MonitoringWithSpring annotation
@Bean
public MonitoringSpringAdvisor springServiceMonitoringAdvisor() {
final MonitoringSpringAdvisor interceptor = new MonitoringSpringAdvisor();
interceptor.setPointcut(new AnnotationMatchingPointcut(Service.class));
return interceptor;
}
// monitoring all services and controller of application
@Bean
public MonitoringSpringAdvisor springRestControllerMonitoringAdvisor() {
final MonitoringSpringAdvisor interceptor = new MonitoringSpringAdvisor();
interceptor.setPointcut(new AnnotationMatchingPointcut(RestController.class));
return interceptor;
}
}
javamelody.enabled=true
javamelody.excluded-datasources=
javamelody.spring-monitoring-enabled=true
javamelody.init-parameters.log=true
javamelody.advisor-auto-proxy-creator-enabled= false
javamelody.scheduled-monitoring-enabled= true
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/monitoring/**");
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Md Imroz Alam
Md. Imroz Alam is a bright Web App Developer, he has good knowledge of Java, J2SE, Jsp, Servlet, jdbc. His hobbies are watching movie, playing carom.