Search code examples
eclipsemetricspayaramicroprofile

Eclipse MicroProfile Metrics with SOAP-based web services


Isn’t it possible to use Eclipse MicroProfile Metrics with SOAP-based web services on Payara Server 5.193.1? @Counted and @Timed don’t seem to work with @WebService and @WebMethod? Although, @Metric works. Is this by design or is it an issue?

Here is my code:

Interface:

package nl.tent.laboratory.emp.metrics;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface MyWebService {

    @WebMethod
    String sayHello();

}

Implementation:

package nl.tent.laboratory.emp.metrics;

import javax.jws.WebService;
import org.eclipse.microprofile.metrics.annotation.Counted;

@WebService(endpointInterface = "nl.tent.laboratory.emp.metrics.MyWebService")
public class MyWebServiceImpl implements MyWebService {

//    @Inject
//    @Metric
//    Counter counter;

    public MyWebServiceImpl() {
        super();
    }

    @Counted(name = "myCounter")
    @Override
    public String sayHello() {
//        counter.inc();
        return "Hello Marc!";
    }

}

Solution

  • @Counted and @Timed are method interceptors and work only on CDI beans. @Metric injects metrics objects and works where injection is supported, including Servlets and Web services.

    In Payara Server, a web service object is implemented as a servlet by default. Servlets can inject CDI beans but they aren't CDI beans themselves and CDI interceptors don't work on them.

    You need to turn your WS into a CDI bean (e.g. with @RequestScoped) or EJB (@Stateless) to enable the Metrics interceptors.