Spring-boot & Java Melody

2,166 views
Skip to first unread message

Chad Goltzman

unread,
Mar 13, 2014, 9:59:46 AM3/13/14
to javam...@googlegroups.com
Has anyone been able to get Java Melody to work with a spring-boot application? If so how where you able to configure it.

Message has been deleted

brian...@gmail.com

unread,
Jun 18, 2014, 1:57:57 PM6/18/14
to javam...@googlegroups.com
I have it working with Spring Boot 1.1.1.RELEASE. 
 
I use Maven, so I have the following dependencies for JavaMelody (along with spring boot starter web (jetty), data-jpa, etc.)
 
net.bull.javamelody
javamelody-core
1.49.0
 
(if you want to export monitoring to json or xml...)
com.thoughtworks.xstream
xstream
1.4.2
 
org.jrobin
jrobin
1.5.9
 
Then simply add a bean to one of your @Configuration classes like this:
 
@Configuration
@ImportResource("classpath:monitoring-spring.xml")   <-- this is just the XML given to you by JavaMelody in my src/main/resources folder
......
 
@Bean
public FilterRegistrationBean filterRegistrationBean() {
    Filter javaMelodyFilter = new MonitoringFilter();
    FilterRegistrationBean javaMelodyFilterBean = new FilterRegistrationBean(javaMeloldyFilter);
    javaMelodyFilterBean.addServletNames("monitoring");
    javaMelodyFilterBean.addUrlPatterns("/*");
    return javaMelodyFilterBean;
}
 
I'm using spring starter web for a RESTful service when I use this, so when I start my application with the normal spring boot ....run() method, the internal web server starts up and I have a localhost:8080/monitoring URL I can hit to see the statistics.  It captures Spring Bean calls, Data Source stats like SQL run, and which classes uses the SQL, CPU, Memory use, HTTP hits and errors, and monitors my EHCache also.  It's really a nice tool. 
 
Let me know if this helps, or if I can be of more assistance.  Post some code to let me see where you are at. 

govindaraja...@gmail.com

unread,
Jul 10, 2015, 2:20:39 PM7/10/15
to javam...@googlegroups.com
I am playing around with Java Melody & Springboot App which is a rest service hitting NoSQL database.

I added 

@ImportResource("classpath:monitoring-spring.xml")  to the @configuration class and also added the @Bean section into the bean. I believe this substitutes for web.xml entries in case of non springboot app.

    @Bean

    public FilterRegistrationBean filterRegistrationBean() {

        Filter javaMelodyFilter = new MonitoringFilter();

        FilterRegistrationBean javaMelodyFilterBean = new FilterRegistrationBean(javaMelodyFilter);

        javaMelodyFilterBean.addServletNames("monitoring");

        javaMelodyFilterBean.addUrlPatterns("/*");

        return javaMelodyFilterBean;

    }


Compiles fine and when I run, I get monitoring-spring.xml file not found exception. Do I have to create an empty XML file with name monitoring-spring.xml under src/main/resources folder? 

Vernat Emeric

unread,
Jul 11, 2015, 9:42:56 AM7/11/15
to javam...@googlegroups.com
Hi,

I suggest to use "classpath:net/bull/javamelody/monitoring-spring.xml" instead. And you should also add a SessionListener and, if you want, javamelody parameters.

For example (not tested):
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import net.bull.javamelody.MonitoringFilter;
import net.bull.javamelody.Parameter;
import net.bull.javamelody.SessionListener;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

/**
 * @author speralta, evernat
 */
@Configuration
@ImportResource("classpath:net/bull/javamelody/monitoring-spring.xml")
public class JavaMelodyConfiguration implements ServletContextInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.addListener(new SessionListener());
    }
    @Bean
    public FilterRegistrationBean javaMelody() {
        FilterRegistrationBean javaMelody = new FilterRegistrationBean();
        javaMelody.setFilter(new MonitoringFilter());
        // see list of parameters:
        // https://github.com/javamelody/javamelody/wiki/UserGuide#6-optional-parameters
        javaMelody.addInitParameter(Parameter.LOG.getCode(), Boolean.toString(true));
        javaMelody.addUrlPatterns("/*");
        return javaMelody;
    }
}
Inspiration taken from https://github.com/mycellar/mycellar/blob/master/src/main/java/fr/mycellar/configuration/JavaMelodyConfiguration.java

bye,
Emeric
--

---
You received this message because you are subscribed to the Google Groups "javamelody" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javamelody+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

govindaraja...@gmail.com

unread,
Jul 11, 2015, 11:43:31 AM7/11/15
to javam...@googlegroups.com
Thanks for your quick response. The following are the changes to make it work.

#1 pom.xml

<dependency>

<groupId>net.bull.javamelody</groupId>

<artifactId>javamelody-core</artifactId>

<version>1.54.0</version>

</dependency>


#2 Controller class


@RestController

@RequestMapping("/ri")

@MonitoredWithSpring

public class RequestInstanceController {


#3 Configuration Class


@ImportResource("classpath:monitoring-spring.xml")

@Configuration

public class Database {


...


...


@Bean

public FilterRegistrationBean filterRegistrationBean() {

Filter javaMelodyFilter = new MonitoringFilter();

FilterRegistrationBean javaMelodyFilterBean = new FilterRegistrationBean(

javaMelodyFilter);

javaMelodyFilterBean.addServletNames("monitoring");

javaMelodyFilterBean.addUrlPatterns("/*");

return javaMelodyFilterBean;

}


}


#4 From javamelody jar file, took the sample monitoring-spring.xml and placed it under my project classpath. This is what I was missing originally. From your note, I realize I dont have to move the xml file into my project instead point it to javamelody jar thats in classpath any way.


#5 Accessed springboot application through http://localhost:8080 and Javamelody monitoring application through http://localhost:8080/monitoring


Thanks

Vernat Emeric

unread,
Jul 11, 2015, 7:02:26 PM7/11/15
to javam...@googlegroups.com
I've written a documentation and an example app for Spring-boot.

Thanks,
Emeric
Reply all
Reply to author
Forward
0 new messages