org.apache.dubbo.rpc.service.GenericException Java Examples

The following examples show how to use org.apache.dubbo.rpc.service.GenericException. 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: GenericImplOfHelloService.java    From dubbo-samples with Apache License 2.0 6 votes vote down vote up
@Override
public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
    if (method.equals("sayHello")) {
        System.out.print("executing sayHello.");
        throw new RuntimeException("sayHello: throws exception");
    } else if (method.equals("sayHelloAsync")) {
        System.out.print("executing sayHelloAsync.");
        return CompletableFuture.completedFuture("sayHelloAsync: hello " + args[0]);
    } else {
        try {
            return defaultOperation(method, parameterTypes, args);
        } catch (Exception e) {
            throw new GenericException(e);
        }
    }
}
 
Example #2
Source File: DubboClientHttpResponseFactory.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
public ClientHttpResponse build(Object result, GenericException exception,
		RequestMetadata requestMetadata, RestMethodMetadata restMethodMetadata) {

	DubboHttpOutputMessage httpOutputMessage = new DubboHttpOutputMessage();

	HttpMessageConverterHolder httpMessageConverterHolder = httpMessageConverterResolver
			.resolve(requestMetadata, restMethodMetadata);

	if (httpMessageConverterHolder != null) {
		MediaType mediaType = httpMessageConverterHolder.getMediaType();
		HttpMessageConverter converter = httpMessageConverterHolder.getConverter();
		try {
			converter.write(result, mediaType, httpOutputMessage);
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	return new DubboClientHttpResponse(httpOutputMessage, exception);
}
 
Example #3
Source File: ApacheDubboProxyService.java    From soul with Apache License 2.0 5 votes vote down vote up
/**
 * Generic invoker object.
 *
 * @param body     the body
 * @param metaData the meta data
 * @param exchange the exchange
 * @return the object
 * @throws SoulException the soul exception
 */
public Mono<Object> genericInvoker(final String body, final MetaData metaData, final ServerWebExchange exchange) throws SoulException {
    ReferenceConfig<GenericService> reference = ApplicationConfigCache.getInstance().get(metaData.getServiceName());
    if (Objects.isNull(reference) || StringUtils.isEmpty(reference.getInterface())) {
        ApplicationConfigCache.getInstance().invalidate(metaData.getServiceName());
        reference = ApplicationConfigCache.getInstance().initRef(metaData);
    }
    GenericService genericService = reference.get();
    Pair<String[], Object[]> pair;
    try {
        if (null == body || "".equals(body) || "{}".equals(body) || "null".equals(body)) {
            pair = new ImmutablePair<>(new String[]{}, new Object[]{});
        } else {
            pair = dubboParamResolveService.buildParameter(body, metaData.getParameterTypes());
        }
        CompletableFuture<Object> future = genericService.$invokeAsync(metaData.getMethodName(), pair.getLeft(), pair.getRight());
        return Mono.fromFuture(future.thenApply(ret -> {
            if (Objects.nonNull(ret)) {
                exchange.getAttributes().put(Constants.DUBBO_RPC_RESULT, ret);
            } else {
                exchange.getAttributes().put(Constants.DUBBO_RPC_RESULT, Constants.DUBBO_RPC_RESULT_EMPTY);
            }
            exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE, ResultEnum.SUCCESS.getName());
            return ret;
        }));
    } catch (GenericException e) {
        log.error("dubbo invoker have exception", e);
        throw new SoulException(e.getMessage());
    }
}
 
Example #4
Source File: DubboClientHttpResponse.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
DubboClientHttpResponse(DubboHttpOutputMessage httpOutputMessage,
		GenericException exception) {
	this.httpStatus = exception != null ? HttpStatus.INTERNAL_SERVER_ERROR
			: HttpStatus.OK;
	this.statusText = exception != null ? exception.getExceptionMessage()
			: httpStatus.getReasonPhrase();
	this.httpOutputMessage = httpOutputMessage;
	this.httpHeaders.putAll(httpOutputMessage.getHeaders());
}
 
Example #5
Source File: DubboProxyService.java    From bird-java with MIT License 5 votes vote down vote up
/**
 * dubbo rpc invoke.
 *
 * @param paramMap    request paramMap.
 * @param dubboHandle dubboHandle.
 * @return rpc result.
 * @throws GatewayException exception for rpc.
 */
public Object genericInvoker(ServerWebExchange exchange, final DubboHandle dubboHandle) throws GatewayException {
    ReferenceConfig<GenericService> reference = buildReferenceConfig(dubboHandle);

    ReferenceConfigCache referenceConfigCache = ReferenceConfigCache.getCache();

    GenericService genericService = null;
    try {
        genericService = referenceConfigCache.get(reference);
    } catch (Exception ex) {
        REFERENCE_CONFIG_MAP.remove(dubboHandle);
        reference.destroy();
        referenceConfigCache.destroy(reference);

        log.error(dubboHandle.getInterfaceName() + "服务连接失败");
        throw new GatewayException(ex);
    }

    final Map<String, Object> paramMap = resolveParam(exchange);
    final Pair<String[], Object[]> pair = buildParameter(paramMap, dubboHandle);

    try {
        return genericService.$invoke(dubboHandle.getMethodName(), pair.getLeft(), pair.getRight());
    } catch (GenericException e) {
        log.error(e.getExceptionMessage());
        if (StringUtils.equals(e.getExceptionClass(), UserFriendlyException.class.getName())) {
            throw new UserFriendlyException(e.getExceptionMessage());
        } else {
            throw new GatewayException(e.getMessage());
        }
    }
}
 
Example #6
Source File: DubboGatewayServlet.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Override
public void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {

	String serviceName = resolveServiceName(request);

	String restPath = substringAfter(request.getRequestURI(), serviceName);

	// 初始化 serviceName 的 REST 请求元数据
	repository.initializeMetadata(serviceName);
	// 将 HttpServletRequest 转化为 RequestMetadata
	RequestMetadata clientMetadata = buildRequestMetadata(request, restPath);

	DubboRestServiceMetadata dubboRestServiceMetadata = repository.get(serviceName,
			clientMetadata);

	if (dubboRestServiceMetadata == null) {
		// if DubboServiceMetadata is not found, executes next
		throw new ServletException("DubboServiceMetadata can't be found!");
	}

	RestMethodMetadata dubboRestMethodMetadata = dubboRestServiceMetadata
			.getRestMethodMetadata();

	GenericService genericService = serviceFactory.create(dubboRestServiceMetadata,
			dubboTranslatedAttributes);

	// TODO: Get the Request Body from HttpServletRequest
	byte[] body = getRequestBody(request);

	MutableHttpServerRequest httpServerRequest = new MutableHttpServerRequest(
			new HttpRequestAdapter(request), body);

	DubboGenericServiceExecutionContext context = contextFactory
			.create(dubboRestMethodMetadata, httpServerRequest);

	Object result = null;
	GenericException exception = null;

	try {
		result = genericService.$invoke(context.getMethodName(),
				context.getParameterTypes(), context.getParameters());
	}
	catch (GenericException e) {
		exception = e;
	}
	response.getWriter().println(result);
}
 
Example #7
Source File: DubboTransporterInterceptor.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
		ClientHttpRequestExecution execution) throws IOException {

	URI originalUri = request.getURI();

	String serviceName = originalUri.getHost();

	RequestMetadata clientMetadata = buildRequestMetadata(request);

	DubboRestServiceMetadata metadata = repository.get(serviceName, clientMetadata);

	if (metadata == null) {
		// if DubboServiceMetadata is not found, executes next
		return execution.execute(request, body);
	}

	RestMethodMetadata dubboRestMethodMetadata = metadata.getRestMethodMetadata();

	GenericService genericService = serviceFactory.create(metadata,
			dubboTranslatedAttributes);

	MutableHttpServerRequest httpServerRequest = new MutableHttpServerRequest(request,
			body);

	customizeRequest(httpServerRequest, dubboRestMethodMetadata, clientMetadata);

	DubboGenericServiceExecutionContext context = contextFactory
			.create(dubboRestMethodMetadata, httpServerRequest);

	Object result = null;
	GenericException exception = null;

	try {
		result = genericService.$invoke(context.getMethodName(),
				context.getParameterTypes(), context.getParameters());
	}
	catch (GenericException e) {
		exception = e;
	}

	return clientHttpResponseFactory.build(result, exception, clientMetadata,
			dubboRestMethodMetadata);
}