java - 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 实现。
解决方案
推荐阅读
- swiftui - PassthroughSubject 中的 Never 是什么?
- java - 为什么串行算法比没有额外线程的并行版本慢?
- flutter - 检测软键盘上的手势
- bash - 奇怪的比较结果
- python - 在反向字符串函数上添加了额外的空间
- ios - 水平滚动视图滚动但仅部分滚动并反弹回原始位置
- php - 2019 如何使用 PHPMailer V6.0.7 和 PHP 从 Web 表单发送多个附件
- ruby-on-rails - 如何将 Sunspot 连接到远程 solr 服务器
- postman - 此调用的 User-Agent 标头应该是什么?
- excel - 引用 Range.Row() 设置的 Range 对象会生成错误 1004“应用程序定义或对象定义错误”