Monitoring spring beans with JavaMelody in Spring-Boot project
Asked Answered
K

2

8

I'm trying to monitor a REST application based on the Spring tutorial Building a RESTful Web Service but in the Java Melody documentation page the configuration depends of the web.xml file, but the spring project does not have such file. I tried by using java melody annotations and setting contextConfigLocation in the WebInitializer but when I enter to Java Melody page I can't see Spring section.

I Have my WebInitializar like this:

public class WebInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class).properties();
}

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    servletContext.setInitParameter("contextConfigLocation", "classpath:net/bull/javamelody/monitoring-spring.xml");
    super.onStartup(servletContext);
}
}

I have set the contextConfigLocation as the Java Melody documentation said.

And my controller:

@RestController
@MonitoredWithSpring
public class GreetingController {

private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();


@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
    return new Greeting(counter.incrementAndGet(),
                        String.format(template, name));
}
}

Any advice to make it work?

Khichabia answered 8/1, 2015 at 19:43 Comment(1)
Just add @ImportResource("classpath:net/bull/javamelody/monitoring-spring.xml") to your Application.class.Coven
L
8

There is now a documentation to monitor Spring-boot app with javamelody, including Spring beans: https://github.com/javamelody/javamelody/wiki/SpringBootStarter

Longawa answered 12/7, 2015 at 21:13 Comment(0)
H
6

You only need the javamelody dependency jar in the web application, and register two beans in spring application context:

@Bean
public HttpSessionListener javaMelodyListener(){
    return new net.bull.javamelody.SessionListener();
}

@Bean
public Filter javaMelodyFilter(){
    return new net.bull.javamelody.MonitoringFilter();
}
Herniorrhaphy answered 28/5, 2015 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.