首页 > 解决方案 > Payara 上的 Eclipse MicroProfile Metrics 注释 @Timed 线程安全吗?

问题描述

由于@Timed 注释不适用于我的基于 SOAP 的 Web 服务,因此我编写了一个 SOAP 处理程序,以便能够自己测量 Web 服务调用的持续时间。我想知道这个解决方案是否是线程安全的。

我的 SOAP 处理程序的源代码:

package nl.tent.laboratory.emp.metrics;

import javax.inject.Inject;
import javax.xml.namespace.QName;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import org.eclipse.microprofile.metrics.MetricID;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.Timer;

/**
 * Deze klasse klokt de web service calls.
 *
 * Daar de Eclipse MicroProfile Metrics annotatie @Timed niet werkt in combinatie met de annotatie @WebService klokken we zelf.
 */
public class TimerSOAPHandler extends AbstractGenericSOAPHandler {

    @Inject
    MetricRegistry metricRegistry;

    public TimerSOAPHandler() {
        super();
    }

    @Override
    public boolean handleMessage(SOAPMessageContext context) {
        // Bij het inkomende bericht (request) wordt de tijdsmeting gestart.
        if (isInbound(context)) {
            startTiming(context);
        }
        // Bij het uitgaande bericht (response) wordt de tijdsmeting gestopt.
        if (isOutbound(context)) {
            stopTiming(context);
        }

        return true;
    }

    @Override
    public boolean handleFault(SOAPMessageContext context) {
        // Bij het inkomende bericht (request) wordt de tijdsmeting gestart.
        if (isInbound(context)) {
            startTiming(context);
        }
        // Bij het uitgaande bericht (response) wordt de tijdsmeting gestopt.
        if (isOutbound(context)) {
            stopTiming(context);
        }

        return true;
    }

    private void startTiming(SOAPMessageContext context) {
        String serviceName = ((QName) context.get(MessageContext.WSDL_SERVICE)).toString();
        String operationName = ((QName) context.get(MessageContext.WSDL_OPERATION)).getLocalPart();

        Timer timer = metricRegistry.timer(serviceName + "_" + operationName + "_timer");

        Timer.Context timerContext = timer.time(); // start
    }

    private void stopTiming(SOAPMessageContext context) {
        String serviceName = ((QName) context.get(MessageContext.WSDL_SERVICE)).toString();
        String operationName = ((QName) context.get(MessageContext.WSDL_OPERATION)).getLocalPart();

        MetricID metricID = new MetricID(serviceName + "_" + operationName + "_timer");

        Timer timer = metricRegistry.getTimers().get(metricID);

        Timer.Context timerContext = timer.time();
        timerContext.stop();
    }

}

与此同时,@Timed 注释正在工作,我更喜欢使用@Timed 注释。但是,我想知道这(@Timed)是否是线程安全的。

我使用 Eclipse MicroProfile Metrics 的 Payara 实现。

标签: javathread-safetymetricspayaramicroprofile

解决方案


推荐阅读