All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.logicbus.backend.AbstractServant Maven / Gradle / Ivy

There is a newer version: 1.6.17
Show newest version
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.backend.message.JsonMessage;
import com.logicbus.backend.message.XMLMessage;
import com.logicbus.backend.message.YamlMessage;
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]
* - 采用新的指标接口
* * @version 1.6.12.27 [20190403 duanyy]
* - 增加对yaml的支持
* * @version 1.6.12.29 [20190409]
* - 优化底层的错误代码的处理;
*/ public abstract class AbstractServant extends Servant implements MetricsCollector { protected String formatDefault = "json"; protected static Handler metricsHandler = null; @Override public int actionProcess(Context ctx){ String json = getArgument("format",formatDefault,ctx); if (json.equalsIgnoreCase("json")){ return onJson(ctx); } if (json.equalsIgnoreCase("yaml")){ return onYaml(ctx); } return onXml(ctx); } @Override public void actionException(Context ctx,String code,String message){ String json = getArgument("format",formatDefault,ctx); if (json.equalsIgnoreCase("json")){ ctx.asMessage(JsonMessage.class); return; } if (json.equalsIgnoreCase("yaml")){ ctx.asMessage(YamlMessage.class); return; } ctx.asMessage(XMLMessage.class); } @Override public void create(ServiceDescription sd){ super.create(sd); metricsHandler = MetricsHandlerFactory.getClientInstance(); formatDefault = PropertiesConstants.getString(sd.getProperties(),"formatDefault",formatDefault); onCreate(sd); } @Override 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."); } protected int onYaml(Context ctx){ // NOSONAR throw new ServantException("core.e1000","Protocol YAML 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); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy