0

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!";
    }

}
Marc Beckers
  • 143
  • 1
  • 9

1 Answers1

2

@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.

OndroMih
  • 7,280
  • 1
  • 26
  • 44
  • I have already tried that, but unfortunately without the desired result... Annotating my web service with @ Stateless, @ Stateful, @ Singleton, @ Named, RequestScoped, etc. all make no difference... Am I missing something? – Marc Beckers Nov 27 '19 at 09:14
  • Annotating my web service with @Stateless results in the following error message: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: java.lang.ClassCastException: nl.tent.laboratory.emp.metrics.MyWebServiceImpl cannot be cast to javax.servlet.Servlet|#] – Marc Beckers Nov 27 '19 at 09:15
  • Removing the deployment descriptor web.xml solved the problem of the ClassCastException. So, adding @Stateless to the web service was the right answer to my question. – Marc Beckers Dec 10 '19 at 14:19