templates.thrift.client.perservice.client.service.decorator.class.vm Maven / Gradle / Ivy
#parse("${include}/header.include.vm")
#parse("${include}/generic.include.vm")
#parse("thrift/include/client.decorator.include.vm")
#set ( $interfaceName = "${sourceinfo.interfaceClass.simpleName}")
#set ( $className ="${interfaceName}ThriftClient")
#set ( $sourceinfo.generatePackage = $pkg )
$codewriter.setCurrentJavaFilename("$pkg", "${className}.java")
${sourceinfo.backupImportedList()}##
${sourceinfo.rebuildImportedList($methods,true,true,false)}##
package $pkg;
#outputImports()
import com.gitee.l0km.xthrift.thrift.ClientFactory;
#if ($sourceinfo.needTransformer)
import com.gitee.l0km.xthrift.thrift.TypeTransformer;
#end
import com.facebook.swift.service.RuntimeTApplicationException;
import com.google.common.net.HostAndPort;
import static com.google.common.base.Preconditions.*;
/**
* 基于thrift/swift框架生成的client端代码提供{@link #typeName(${sourceinfo.interfaceClass})}接口的RPC实现(线程安全)
* 转发所有{@link #typeName(${sourceinfo.interfaceClass})}接口方法到{@link ${esc.hash}delegate()}指定的实例
* 所有服务端抛出的{@link RuntimeException}异常被封装到{@link ServiceRuntimeException}中抛出
* Example:
*
* ${className} thriftInstance = ClientFactory
* .builder()
* .setHostAndPort("127.0.0.1",26413)
* .build(#typeName(${sourceinfo.interfaceClass}).class, ${className}.class);
*
* $GENERAED_BY
* @author guyadong
*
*/
public class ${className} implements #typeName(${sourceinfo.interfaceClass}) {
private final ClientFactory factory;
public ClientFactory getFactory() {
return factory;
}
public ${className}(ClientFactory factory) {
super();
this.factory = checkNotNull(factory,"factory is null");
}
/**
* @param host RPC service host
* @param port RPC service port
*/
public ${className}(String host,int port) {
this(ClientFactory.builder().setHostAndPort(host,port));
}
/**
* @param hostAndPort RPC service host and port
*/
public ${className}(HostAndPort hostAndPort) {
this(ClientFactory.builder().setHostAndPort(hostAndPort));
}
/**
* test if connectable for RPC service
* @return return {@code true} if connectable ,otherwise {@code false}
*/
public boolean testConnect(){
return factory.testConnect();
}
/**
* @return 返回{@link $sourceinfo.clientInterfaceName}实例
*/
protected $sourceinfo.clientInterfaceName delegate() {
return factory.applyInstance(${sourceinfo.clientInterfaceName}.class);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("${className} [factory=");
builder.append(factory);
builder.append(",interface=");
builder.append(#typeName(${sourceinfo.interfaceClass}).class.getName());
builder.append("]");
return builder.toString();
}
#set($methodCount = 0 )
#foreach ($method in $TOOL.sortBySignature($methods))
#set($methodCount= $methodCount + 1 )
@Override
#if($schemainfo.isIsLocalMethod($method))
public boolean ${method.name}(){
return false;
}
#else
#set ($declaredExceptions = $TOOL.sortByName($method.genericExceptionTypeSet))
#set ($throwExceptions = $declaredExceptions)
#set ($portName = "${schemainfo.escapeThrift(${sourceinfo.getPortName($method)})}${sourceinfo.methodSuffix($method)}")
public #typeName($method.genericReturnType) ${method.name}(#join($method.parameters '#typeName($e.genericType) $e.name' ',
'))
#fillThrows($throwExceptions){
$sourceinfo.clientInterfaceName instance = delegate();
try{
#RETURN() #setValue($method.genericReturnType "instance.${portName}(#join($method.parameters '#returnValue($e.genericType,$e.name)' ',
'))");
}
#foreach($exp in $throwExceptions)
catch(${sourceinfo.toClientThriftType($exp)} e){
throw #setValue($exp,'e');
}
#end
catch(${sourceinfo.toClientThriftType($schemainfo.serviceRuntimeExceptionClass)} e){
throw new ServiceRuntimeException(e);
}
#if(!$method.genericReturnType.primitive && !$TOOL.isVoid($method.genericReturnType))
catch(RuntimeTApplicationException e){
return com.gitee.l0km.xthrift.thrift.ThriftUtils.returnNull(e);
}
#end
finally{
factory.releaseInstance(instance);
}
}
#end
#end##foreach
}
${sourceinfo.restoreImportedList()}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy