spring-boot: Jolokia Actuator Endpoint is not available when using Reactive/WebFlux Web Application

When adding a jolokia-core dependency to a WebFlux (not MVC) Web Application, the Jolokia actuator endpoint is not being enabled because the application type condition match is failing.

JolokiaEndpointAutoConfiguration": {
          "notMatched": [
            {
              "condition": "OnWebApplicationCondition",
              "message": "not a servlet web application"
            }
          ],
          "matched": [
            {
              "condition": "OnClassCondition",
              "message": "@ConditionalOnClass found required class 'org.jolokia.http.AgentServlet'"
            }
          ]
        },

Is there a way to make it work in a WebFlux app?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

I implemented a POC of WebFlux Jolokia integration. Probably it’s not complete and has some flaws but it seems to work.

Comments and suggestions in order to improve it are appreciated.

Does an alternative exist for exposing JXM beans using WebFlux (different from Jolokia)?

I use Micrometer with Prometheus now

@taxone, thank you!

I modified your code a little, which as a result allows me to use Spring Boot 3, WebFlux, Jolokia 2 and Spring Boot Admin. In particular, a custom actuator endpoint was created instead of a RestController:

import jakarta.annotation.PostConstruct
import org.jolokia.server.core.config.ConfigKey
import org.jolokia.server.core.config.StaticConfiguration
import org.jolokia.server.core.http.HttpRequestHandler
import org.jolokia.server.core.restrictor.RestrictorFactory
import org.jolokia.server.core.service.JolokiaServiceManagerFactory
import org.jolokia.server.core.service.api.LogHandler
import org.jolokia.server.core.service.api.Restrictor
import org.jolokia.server.core.service.impl.StdoutLogHandler
import org.jolokia.server.core.util.NetworkUtil
import org.jolokia.service.jmx.LocalRequestHandler
import org.jolokia.service.serializer.JolokiaSerializer
import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint
import org.springframework.http.server.reactive.ServerHttpRequest
import org.springframework.stereotype.Component
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.server.ServerWebExchange
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import reactor.core.scheduler.Scheduler
import reactor.core.scheduler.Schedulers
import java.io.IOException

@Component
@RestControllerEndpoint(id = "jolokia")
class ReactiveJolokiaActuatorEndpoint {

    private lateinit var requestHandler: HttpRequestHandler
    private val scheduler: Scheduler = Schedulers.newBoundedElastic(3, 10, "jolokiaThreads")

    @PostConstruct
    fun init() {
        val config = StaticConfiguration(ConfigKey.AGENT_ID, NetworkUtil.getAgentId(hashCode(), "servlet"))
        val logHandler: LogHandler = StdoutLogHandler()
        val restrictor: Restrictor = RestrictorFactory.createRestrictor(config, logHandler)
        val serviceManager = JolokiaServiceManagerFactory.createJolokiaServiceManager(
            config,
            logHandler,
            restrictor,
        )
        serviceManager.addServices { setOf(JolokiaSerializer(), LocalRequestHandler(1000)) }
        val jolokiaContext = serviceManager.start()
        requestHandler = HttpRequestHandler(jolokiaContext)
    }

    @GetMapping(path = ["/**"])
    fun gioia(exchange: ServerWebExchange): Mono<String> = Mono.fromCallable {
        val pReq: ServerHttpRequest = exchange.request
        val pathInfo = pathInfo(pReq)
        requestHandler.handleGetRequest(pReq.uri.toString(), pathInfo, getParameterMap(pReq)).toJSONString()
    }.subscribeOn(scheduler)

    @PostMapping(path = ["/**"], consumes = ["application/json"])
    fun post(exchange: ServerWebExchange): Flux<String> = exchange.request.body.handle { body, sink ->
        val inputStream = body.asInputStream(true)
        val pReq: ServerHttpRequest = exchange.request
        try {
            sink.next(
                requestHandler.handlePostRequest(
                    pReq.uri.toString(),
                    inputStream,
                    null,
                    getParameterMap(pReq)
                ).toJSONString()
            )
        } catch (e: IOException) {
            sink.error(RuntimeException(e))
        }
    }.subscribeOn(scheduler)

    private fun pathInfo(pReq: ServerHttpRequest): String = pReq.path.subPath(4).toString()

    private fun getParameterMap(pReq: ServerHttpRequest): Map<String, Array<String>> =
        pReq.queryParams.entries.associate { it.key to it.value.toTypedArray() }
}

Gradle dependencies:

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("de.codecentric:spring-boot-admin-starter-client:3.1.8")

    implementation(platform("org.jolokia:jolokia-parent:2.0.0-M4"))
    implementation("org.jolokia:jolokia-server-core")
    implementation("org.jolokia:jolokia-service-jmx")
    implementation("org.jolokia:jolokia-service-serializer")
}

Maybe someone will find this useful.