All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.logicbus.backend.AbstractServant Maven / Gradle / Ivy
package com.logicbus.backend;
import com.alogic.metrics.Dimensions;
import com.alogic.metrics.Fragment;
import com.alogic.metrics.Fragment.Method;
import com.alogic.metrics.Measures;
import com.alogic.metrics.impl.DefaultFragment;
import com.alogic.metrics.stream.MetricsCollector;
import com.alogic.metrics.stream.MetricsHandlerFactory;
import com.anysoft.stream.Handler;
import com.anysoft.util.PropertiesConstants;
import com.logicbus.models.servant.ServiceDescription;
/**
* Servant虚基类
*
* @author duanyy
* @since 1.2.3
*
* @version 1.2.5 [20140722 duanyy]
* - Servant的destroy方法改为close
*
* @version 1.2.8 [20140914 duanyy]
* - 增加指标收集体系
*
* @version 1.4.0 [20141117 duanyy]
* - Servant体系抛弃MessageDoc
*
* @version 1.6.4.29 [20160126 duanyy]
* - 清除Servant体系中处于deprecated的方法
*
* @version 1.6.6.13 [20170109 duanyy]
* - 采用新的指标接口
*/
public abstract class AbstractServant extends Servant implements MetricsCollector {
protected boolean jsonDefault = true;
protected static Handler metricsHandler = null;
@Override
public int actionProcess(Context ctx){
boolean json = getArgument("json",jsonDefault,ctx);
if (json){
return onJson(ctx);
}else{
return onXml(ctx);
}
}
public void create(ServiceDescription sd){
super.create(sd);
metricsHandler = MetricsHandlerFactory.getClientInstance();
jsonDefault = PropertiesConstants.getBoolean(sd.getProperties(),"jsonDefault",jsonDefault);
onCreate(sd);
}
public void close(){
super.close();
onDestroy();
}
protected abstract void onDestroy();
protected abstract void onCreate(ServiceDescription sd);
/**
* 以XML协议进行服务处理
* @param ctx 上下文
* @return 结果
*
* @since 1.4.0
*/
protected int onXml(Context ctx){ // NOSONAR
throw new ServantException("core.e1000","Protocol XML is not suppurted.");
}
/**
* 以JSON协议进行服务处理
* @param ctx 上下文
* @return 结果
* @since 1.4.0
*/
protected abstract int onJson(Context ctx); // NOSONAR
@Override
public void metricsIncr(Fragment fragment){
if (metricsHandler != null){
Dimensions dims = fragment.getDimensions();
if (dims != null){
dims.set("svc", getDescription().getPath(), false);
}
metricsHandler.handle(fragment,System.currentTimeMillis());
}
}
public void metricsIncr(String mId,String measure,long value,Method m){
Fragment f = new DefaultFragment(mId);
Measures meas = f.getMeasures();
if (meas != null){
meas.set(measure, value,m);
}
metricsIncr(f);
}
public void metricsIncr(String mId,String measure,double value,Method m){
Fragment f = new DefaultFragment(mId);
Measures meas = f.getMeasures();
if (meas != null){
meas.set(measure, value,m);
}
metricsIncr(f);
}
public void metricsIncr(String mId,String measure,long value){
metricsIncr(mId,measure,value,Method.sum);
}
public void metricsIncr(String mId,long value){
metricsIncr(mId,"v",value,Method.sum);
}
public void metricsIncr(String mId,String measure,double value){
metricsIncr(mId,measure,value,Method.sum);
}
public void metricsIncr(String mId,double value){
metricsIncr(mId,"v",value,Method.sum);
}
}