templates.thrift.client.perservice.client.async.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}ThriftClientAsync")
#set ( $sourceinfo.generatePackage = $pkg )
#set ($referExceptions = $TOOL.sortByName($sourceinfo.referExceptions))
$codewriter.setCurrentJavaFilename("$pkg", "${className}.java")
${sourceinfo.backupImportedList()}##
${sourceinfo.rebuildImportedList($methods,false,true,false)}##
package $pkg;
#outputImports()
import com.google.common.net.HostAndPort;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.ListenableFuture;
import com.gitee.l0km.xthrift.thrift.ClientFactory;
#if ($sourceinfo.needTransformer)
import com.gitee.l0km.xthrift.thrift.TypeTransformer;
#end
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} {
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}.Async}实例
*/
protected ${sourceinfo.clientInterfaceName}.Async delegate() {
return factory.applyInstance(${sourceinfo.clientInterfaceName}.Async.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();
}
/**
* 默认的{@link FutureCallback}实现
* @author guyadong
*
* @param
*/
public static class DefaultCallback implements FutureCallback{
@Override
public void onSuccess(V result) {
// DO NOTHING
}
@Override
public void onFailure(Throwable t) {
try{
throw t;
}
#foreach($exp in $referExceptions)
catch(${sourceinfo.toClientThriftType($exp)} e){
on${exp.simpleName}(e);
}
#end
catch(${sourceinfo.toClientThriftType($schemainfo.serviceRuntimeExceptionClass)} e){
on${schemainfo.serviceRuntimeExceptionClass.simpleName}(e);
}
catch(Throwable e){
onThrowable(e);
}
}
#foreach($exp in $referExceptions)
protected void on${exp.simpleName}(${sourceinfo.toClientThriftType($exp)} e){
System.out.println(e.getServiceStackTraceMessage());
}
#end
protected void on${schemainfo.serviceRuntimeExceptionClass.simpleName}(${sourceinfo.toClientThriftType($schemainfo.serviceRuntimeExceptionClass)} e){
System.out.println(e.getServiceStackTraceMessage());
}
protected void onThrowable(Throwable e){
e.printStackTrace();
}
}
#set($methodCount = 0 )
#foreach ($method in $TOOL.sortBySignature($methods))
#set($methodCount= $methodCount + 1 )
/**
* see also {@link ${method.docSignature}}
*/
#if($schemainfo.isIsLocalMethod($method))
public boolean ${method.name}(){
return false;
}
#else
#set ($returnType = $schemainfo.wrap($method.genericReturnType))
public ListenableFuture<#typeName($returnType)> ${method.name}(#join($method.parameters '#typeName($e.genericType) $e.name' ',
')){
${sourceinfo.clientInterfaceName}.Async async = delegate();
#set ($portName = "${schemainfo.escapeThrift(${sourceinfo.getPortName($method)})}${sourceinfo.methodSuffix($method)}")
#if($sourceinfo.isClientThriftType($returnType))
### 返回类型不需要转换
ListenableFuture<#typeName($returnType)> future = async.${portName}(#join($method.parameters '#returnValue($e.genericType,$e.name)' ',
'));
#else
ListenableFuture<#typeName($returnType)> future = com.google.common.util.concurrent.Futures.transform(
async.${portName}(#join($method.parameters '#returnValue($e.genericType,$e.name)' ',
')),
new com.google.common.base.Function<${sourceinfo.toClientThriftType($returnType)},#typeName($returnType)>(){
@Override
public #typeName($returnType) apply(${sourceinfo.toClientThriftType($returnType)} input) {
return #setValue($returnType,'input');
}
});
#end
return factory.wrap(async,future);
}
public void ${method.name}(#join($method.parameters '#typeName($e.genericType) $e.name,
' '')FutureCallback<#typeName($returnType)>callback){
factory.addCallback(${method.name}(#join($method.parameters '$e.name' ',')), callback);
}
#end
#end##foreach
}
${sourceinfo.restoreImportedList()}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy