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.

 

b) following are the maven dependency
 

net.bull.javamelody
javamelody-spring-boot-starter
1.70.0
          
        
 
 
// If proxy issues comes then add 

org.springframework.boot
spring-boot-starter-aop
    
        
 
c)
Following are bean class for java melody configuration
 
@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;
    }
}
    
        
 
4)
Following are the properties enable java melody to your application( application.properties )
 
javamelody.enabled=true 
javamelody.excluded-datasources=
javamelody.spring-monitoring-enabled=true 
javamelody.init-parameters.log=true
        
 
 
5) if want to enable every scheduler monitoring, then add following properties
 
javamelody.advisor-auto-proxy-creator-enabled= false
javamelody.scheduled-monitoring-enabled= true
        
 
 
6) If your application is configured with spring security then add following entry to main class of spring security.
 
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
public void configure(WebSecurity web) throws Exception {

web.ignoring().antMatchers("/monitoring/**");
}

        
 
7) now , run following command 
mvn spring-boot:run
 
http://localhost:port/monitoring
 
Thanks, I hope this will be helpful.

 

About Author

Author Image
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.

Request for Proposal

Name is required

Comment is required

Sending message..