org.apache.thrift.AsyncProcessFunction Java Examples

The following examples show how to use org.apache.thrift.AsyncProcessFunction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ThriftCallService.java    From armeria with Apache License 2.0 6 votes vote down vote up
private static void invokeAsynchronously(Object impl, ThriftFunction func, TBase<?, ?> args,
                                         CompletableRpcResponse reply) throws TException {

    final AsyncProcessFunction<Object, TBase<?, ?>, Object> f = func.asyncFunc();
    if (func.isOneWay()) {
        f.start(impl, args, ONEWAY_CALLBACK);
        reply.complete(null);
    } else {
        f.start(impl, args, new AsyncMethodCallback<Object>() {
            @Override
            public void onComplete(Object response) {
                reply.complete(response);
            }

            @Override
            public void onError(Exception e) {
                reply.completeExceptionally(e);
            }
        });
    }
}
 
Example #2
Source File: ThriftServiceMetadata.java    From armeria with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
private void registerFunction(Class<?> iface, String name,
                              Object func, @Nullable Object implementation) {
    if (functions.containsKey(name)) {
        logger.warn("duplicate Thrift method name: " + name);
        return;
    }

    try {
        final ThriftFunction f;
        if (func instanceof ProcessFunction) {
            f = new ThriftFunction(iface, (ProcessFunction) func, implementation);
        } else {
            f = new ThriftFunction(iface, (AsyncProcessFunction) func, implementation);
        }
        functions.put(name, f);
    } catch (Exception e) {
        throw new IllegalArgumentException("failed to retrieve function metadata: " +
                                           iface.getName() + '.' + name + "()", e);
    }
}
 
Example #3
Source File: ThriftServiceMetadata.java    From armeria with Apache License 2.0 5 votes vote down vote up
private Set<Class<?>> init(@Nullable Object implementation, Iterable<Class<?>> candidateInterfaces) {

        // Build the map of method names and their corresponding process functions.
        // If a method is defined multiple times, we take the first definition
        final Set<Class<?>> interfaces = new HashSet<>();

        for (Class<?> iface : candidateInterfaces) {
            final Map<String, AsyncProcessFunction<?, ?, ?>> asyncProcessMap;
            asyncProcessMap = getThriftAsyncProcessMap(implementation, iface);
            if (asyncProcessMap != null) {
                asyncProcessMap.forEach(
                        (name, func) -> registerFunction(iface, name, func, implementation));
                interfaces.add(iface);
            }

            final Map<String, ProcessFunction<?, ?>> processMap;
            processMap = getThriftProcessMap(implementation, iface);
            if (processMap != null) {
                processMap.forEach(
                        (name, func) -> registerFunction(iface, name, func, implementation));
                interfaces.add(iface);
            }
        }

        if (functions.isEmpty()) {
            if (implementation != null) {
                throw new IllegalArgumentException('\'' + implementation.getClass().getName() +
                                                   "' is not a Thrift service implementation.");
            } else {
                throw new IllegalArgumentException("not a Thrift service interface: " + candidateInterfaces);
            }
        }

        return Collections.unmodifiableSet(interfaces);
    }
 
Example #4
Source File: ThriftServiceMetadata.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Nullable
private static Map<String, AsyncProcessFunction<?, ?, ?>> getThriftAsyncProcessMap(
        @Nullable Object service, Class<?> iface) {

    final String name = iface.getName();
    if (!name.endsWith("$AsyncIface")) {
        return null;
    }

    final String processorName = name.substring(0, name.length() - 10) + "AsyncProcessor";
    try {
        final Class<?> processorClass = Class.forName(processorName, false, iface.getClassLoader());
        if (!TBaseAsyncProcessor.class.isAssignableFrom(processorClass)) {
            return null;
        }

        final Constructor<?> processorConstructor = processorClass.getConstructor(iface);

        @SuppressWarnings("rawtypes")
        final TBaseAsyncProcessor processor =
                (TBaseAsyncProcessor) processorConstructor.newInstance(service);

        @SuppressWarnings("unchecked")
        final Map<String, AsyncProcessFunction<?, ?, ?>> processMap =
                (Map<String, AsyncProcessFunction<?, ?, ?>>) processor.getProcessMapView();

        return processMap;
    } catch (Exception e) {
        logger.debug("Failed to retrieve the asynchronous process map from:: {}", iface, e);
        return null;
    }
}
 
Example #5
Source File: AsyncServerConfig.java    From thrift-mock with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, AsyncProcessFunction> getProcessMap() {
  return processMap;
}
 
Example #6
Source File: AsyncThriftMockServer.java    From thrift-mock with Apache License 2.0 4 votes vote down vote up
public void setExpectReturn(String methodName, AsyncProcessFunction processFunction) {
  serverConfig.getProcessMap().put(methodName, processFunction);
}
 
Example #7
Source File: AsyncTMockProcessor.java    From thrift-mock with Apache License 2.0 4 votes vote down vote up
public AsyncTMockProcessor(Map<String, AsyncProcessFunction> processFunctionMap) {
  super(new MockIface() {}, processFunctionMap);
}
 
Example #8
Source File: AsyncServiceProcessor.java    From ikasoa with MIT License 4 votes vote down vote up
@SuppressWarnings("rawtypes")
private static Map<String, AsyncProcessFunction<AsyncService, ? extends TBase, ?>> getProcessMap(
		Map<String, AsyncProcessFunction<AsyncService, ? extends TBase, ?>> processMap) {
	processMap.put(FUNCTION_NAME, new GetAsyncProcessFunction());
	return processMap;
}
 
Example #9
Source File: ThriftFunction.java    From armeria with Apache License 2.0 4 votes vote down vote up
ThriftFunction(Class<?> serviceType, AsyncProcessFunction<?, ?, ?> func) throws Exception {
    this(serviceType, func.getMethodName(), func, Type.ASYNC,
         getArgFields(func), getResult(func), getDeclaredExceptions(func), null);
}
 
Example #10
Source File: ThriftFunction.java    From armeria with Apache License 2.0 4 votes vote down vote up
ThriftFunction(Class<?> serviceType, AsyncProcessFunction<?, ?, ?> func,
               @Nullable Object implementation) throws Exception {
    this(serviceType, func.getMethodName(), func, Type.ASYNC,
         getArgFields(func), getResult(func), getDeclaredExceptions(func), implementation);
}
 
Example #11
Source File: ThriftFunction.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Nullable
private static TBase<?, ?> getResult(AsyncProcessFunction<?, ?, ?> asyncFunc) {
    return getResult0(Type.ASYNC, asyncFunc.getClass(), asyncFunc.getMethodName());
}
 
Example #12
Source File: ThriftFunction.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static TBase<?, ?> getArgs(AsyncProcessFunction<?, ?, ?> asyncFunc) {
    return getArgs0(Type.ASYNC, asyncFunc.getClass(), asyncFunc.getMethodName());
}
 
Example #13
Source File: ThriftFunction.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static TFieldIdEnum[] getArgFields(AsyncProcessFunction<?, ?, ?> asyncFunc) {
    return getArgFields0(Type.ASYNC, asyncFunc.getClass(), asyncFunc.getMethodName());
}
 
Example #14
Source File: ThriftFunction.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static Class<?>[] getDeclaredExceptions(AsyncProcessFunction<?, ?, ?> asyncFunc) {
    return getDeclaredExceptions0(Type.ASYNC, asyncFunc.getClass(), asyncFunc.getMethodName());
}
 
Example #15
Source File: ThriftFunction.java    From armeria with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the {@link AsyncProcessFunction}.
 *
 * @throws ClassCastException if this function is synchronous
 */
@SuppressWarnings("unchecked")
public AsyncProcessFunction<Object, TBase<?, ?>, Object> asyncFunc() {
    return (AsyncProcessFunction<Object, TBase<?, ?>, Object>) func;
}