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

net.gdface.service.sdk.FaceApiService Maven / Gradle / Ivy

The newest version!
package net.gdface.service.sdk;

import java.util.List;
import java.util.concurrent.TimeUnit;

import com.facebook.swift.service.ThriftEventHandler;
import com.facebook.swift.service.ThriftServerConfig;
import com.gitee.l0km.xthrift.service.ThriftServerService;
import com.google.common.collect.ImmutableList;
import io.airlift.units.Duration;
import net.gdface.sdk.BaseFaceApi;
import net.gdface.sdk.FaceApi;
import net.gdface.sdk.decorator.FaceApiThriftDecorator;
import net.gdface.sdk.fse.FeatureSe;
import net.gdface.sdk.fse.decorator.FeatureSeThriftDecorator;
import net.gdface.service.sdk.spring.RestfulService;

import static com.google.common.base.Preconditions.*;
/**
 * {@link FaceApi}服务封装
 * @author guyadong
 *
 */
public class FaceApiService extends ThriftServerService implements FaceApiServiceConstants {
	private static FaceApiService service;
	private static FaceApiService httpService;
	private static final FaceApiServiceConfig serviceConfig = FaceApiServiceConfig.getInstance();
	private static final FaceApi FACEAPI_INSTANCE = serviceConfig.getFaceapi();
	private static final FeatureSe FSE_INSTANCE = BaseFaceApi.getFeatureSeInstance(FACEAPI_INSTANCE);
	// 封装为thrift服务的faceapi接口静态实例
	private static final FaceApiThriftDecorator FACEAPI_THRIFT_DECORATOR = new FaceApiThriftDecorator(FACEAPI_INSTANCE, serviceConfig.isTrace());
	/**
	 * 从配置文件中读取参数创建{@link ThriftServerConfig}实例
	 */
	public static ThriftServerConfig makeThriftServerConfig(){
		ThriftServerConfig thriftServerConfig = new ThriftServerConfig();
		int intValue ;
		thriftServerConfig.setPort(serviceConfig.getServicePort());
		if((intValue  = serviceConfig.getConnectionLimit()) >0){
			thriftServerConfig.setConnectionLimit(intValue);
		}
		if((intValue = serviceConfig.getIdleConnectionTimeout())>0){
			Duration timeout = new Duration(intValue,TimeUnit.SECONDS);
			thriftServerConfig.setIdleConnectionTimeout(timeout);
		}
		if((intValue = serviceConfig.getWorkThreads())>0){
			thriftServerConfig.setWorkerThreads(intValue);
		}
		return thriftServerConfig;
	}
	/**
	 * 创建服务实例(frame,binary)
	 */
	public static synchronized final FaceApiService buildService(){
		return service = buildService(service,makeThriftServerConfig());
	}
	/**
	 * 创建HTTP服务实例(http,json)
	 */
	public static synchronized final FaceApiService buildHttpService(){
		return httpService = buildService(httpService,
				makeThriftServerConfig()
					.setPort(serviceConfig.getXhrPort())
					.setTransportName(ThriftServerService.HTTP_TRANSPORT)
					.setProtocolName(ThriftServerService.JSON_PROTOCOL));
	}
	/**
	 * 启动RESTful WEB服务实例
	 */
	public static synchronized final void startRestfulService(){
		
		RestfulService.setHttpPort(serviceConfig.getRestfulPort());
		RestfulService.setInterfaceInstance(FACEAPI_INSTANCE);
		RestfulService.setInterfaceInstance(FSE_INSTANCE);
		RestfulService.setSwaggerEnable(serviceConfig.isSwaggerEnable());
		RestfulService.setCorsEnable(serviceConfig.isCorsEnable());
		RestfulService.run();
	}
	/**
	 * 创建服务实例
* @param service 服务实例,如果为{@code null}或服务已经停止则创建新的服务实例 * @param config thrift服务配置 * @return 返回{@code service}或创建的新的服务实例 */ private static FaceApiService buildService(FaceApiService service,ThriftServerConfig config){ if(null == service || State.TERMINATED == service.state() || State.FAILED == service.state()){ ImmutableList.Builder builder = ImmutableList.builder(); builder.add(FACEAPI_THRIFT_DECORATOR); if(null != FSE_INSTANCE){ builder.add(new FeatureSeThriftDecorator(FSE_INSTANCE)); } service = ThriftServerService.bulider() .withServices(builder.build()) .setThriftServerConfig(config) .build(FaceApiService.class); } checkState(State.NEW == service.state(),"INVALID service state %s ",service.toString()); return service; } public FaceApiService(List services, List eventHandlers, ThriftServerConfig thriftServerConfig) { super(services, eventHandlers, thriftServerConfig); } /** * @return service */ public static FaceApiService getService() { return service; } /** * @return httpService */ public static FaceApiService getHttpService() { return httpService; } }