Search in sources :

Example 11 with EmptyNullException

use of com.duangframework.core.exceptions.EmptyNullException in project duangframework by tcrct.

the class MainProcess method doWork.

@Override
public IResponse doWork(IRequest req, IResponse res) throws Exception {
    HttpRequest request = (HttpRequest) req;
    HttpResponse response = (HttpResponse) res;
    if (ToolsKit.isEmpty(request)) {
        throw new EmptyNullException("request is null");
    }
    if (ToolsKit.isEmpty(response)) {
        throw new EmptyNullException("response is null");
    }
    String target = request.getRequestURI().toString();
    AsyncContext asyncContext = new AsyncContextThreadImpl(target, request, response);
    return asyncContext.complete();
// AsyncContext asyncContext = new AsyncContextQueueImpl(target, request, response);
// return asyncContext.complete();
}
Also used : HttpRequest(com.duangframework.core.common.dto.http.request.HttpRequest) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException) HttpResponse(com.duangframework.core.common.dto.http.response.HttpResponse) AsyncContext(com.duangframework.core.common.dto.http.request.AsyncContext)

Example 12 with EmptyNullException

use of com.duangframework.core.exceptions.EmptyNullException in project duangframework by tcrct.

the class MongoBaseDao method findAll.

/**
 * 查找所有
 * @param mongoQuery		查询条件
 * @return 结果集合,元素为指定的泛型
 * @throws Exception
 */
private List<T> findAll(MongoQuery mongoQuery) throws Exception {
    if (null == mongoQuery) {
        throw new EmptyNullException("Mongodb findList is Fail: mongoQuery is null");
    }
    Bson queryDoc = mongoQuery.getQueryBson();
    PageDto<T> page = mongoQuery.getPage();
    int pageNo = page.getPageNo();
    int pageSize = page.getPageSize();
    FindIterable<Document> documents = collection.find(queryDoc);
    BasicDBObject fieldDbo = (BasicDBObject) mongoQuery.getDBFields();
    if (ToolsKit.isNotEmpty(fieldDbo) && !fieldDbo.isEmpty()) {
        documents.projection(fieldDbo);
    }
    BasicDBObject orderDbo = (BasicDBObject) mongoQuery.getDBOrder();
    if (ToolsKit.isNotEmpty(orderDbo) && !orderDbo.isEmpty()) {
        documents.sort(orderDbo);
    }
    if (pageNo > 0 && pageSize > 0) {
        documents.skip((pageNo > 0 ? (pageNo - 1) : pageNo) * pageSize);
        documents.limit(pageSize);
    }
    BasicDBObject hintDbo = (BasicDBObject) mongoQuery.getHintDBObject();
    if (ToolsKit.isNotEmpty(hintDbo) && !hintDbo.isEmpty()) {
        documents.hint(hintDbo);
    }
    if (ToolsKit.isEmpty(documents)) {
        return null;
    }
    final List<T> resultList = new ArrayList();
    documents.forEach(new Block<Document>() {

        @Override
        public void apply(Document document) {
            resultList.add((T) MongoUtils.toEntity(document, cls));
        }
    });
    return resultList;
}
Also used : EmptyNullException(com.duangframework.core.exceptions.EmptyNullException) Document(org.bson.Document) Bson(org.bson.conversions.Bson)

Example 13 with EmptyNullException

use of com.duangframework.core.exceptions.EmptyNullException in project duangframework by tcrct.

the class AutoBuildServiceInterface method createBatchInterface.

/**
 *  批量创建Service类接口文件到指定目录下
 * @param interFaceDirPath      存放RPC接口文件的目录
 * @param  customizeDir           自定义目录
 * @return
 * @throws Exception
 */
public static boolean createBatchInterface(String interFaceDirPath, String customizeDir) throws Exception {
    Map<Class<?>, Object> serviceMap = BeanUtils.getAllBeanMaps().get(Service.class.getSimpleName());
    if (ToolsKit.isEmpty(serviceMap)) {
        throw new EmptyNullException("serviceMap is null");
    }
    try {
        for (Iterator<Class<?>> iterator = serviceMap.keySet().iterator(); iterator.hasNext(); ) {
            Class<?> clazz = iterator.next();
            String packagePath = RpcUtils.getRpcPackagePath(customizeDir);
            createInterface(clazz, interFaceDirPath, packagePath);
        }
        return true;
    } catch (Exception e) {
        logger.warn(e.getMessage(), e);
        throw new RpcException("batch create service interface is fail: " + e.getMessage(), e);
    }
}
Also used : EmptyNullException(com.duangframework.core.exceptions.EmptyNullException) RpcException(com.duangframework.core.exceptions.RpcException) Service(com.duangframework.core.annotation.mvc.Service) CtClass(javassist.CtClass) RpcException(com.duangframework.core.exceptions.RpcException) NotFoundException(javassist.NotFoundException) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException)

Example 14 with EmptyNullException

use of com.duangframework.core.exceptions.EmptyNullException in project duangframework by tcrct.

the class RpcFactory method initService.

/**
 * 初始化RPC服务, 发布到注册中心
 * @param classSet
 */
public static void initService(Set<Class<?>> classSet) throws Exception {
    if (ToolsKit.isEmpty(classSet)) {
        throw new EmptyNullException("rpc server class set is null");
    }
    for (Class<?> rpcInterfaceClass : classSet) {
        Rpc rpcAnnotation = rpcInterfaceClass.getAnnotation(Rpc.class);
        if (!rpcInterfaceClass.isInterface() || ToolsKit.isEmpty(rpcAnnotation)) {
            continue;
        }
        String interfaceName = rpcInterfaceClass.getName();
        String serviceName = rpcAnnotation.service();
        if (ToolsKit.isEmpty(serviceName)) {
            throw new EmptyNullException("serviceFullPath is empty");
        }
        HANDLER_MAP.put(interfaceName, new RpcAction(ClassUtils.loadClass(serviceName), rpcInterfaceClass, IpUtils.getLocalHostIP(false), IpUtils.getLocalHostIP(true), RpcUtils.getPort()));
    }
    if (ToolsKit.isEmpty(HANDLER_MAP)) {
        logger.warn("Rpc service is null, exit initService...");
        return;
    }
    // 启动Netty服务
    if (startRpcServer()) {
        // 发布服务
        publish();
    }
}
Also used : Rpc(com.duangframework.core.annotation.rpc.Rpc) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException) RpcAction(com.duangframework.rpc.common.RpcAction)

Example 15 with EmptyNullException

use of com.duangframework.core.exceptions.EmptyNullException in project duangframework by tcrct.

the class NettyServiceHandler method channelRead0.

@Override
public void channelRead0(final ChannelHandlerContext ctx, final MessageHolder<RpcRequest> holder) throws Exception {
    RpcRequest request = holder.getBody();
    if (ToolsKit.isEmpty(request)) {
        throw new EmptyNullException("request is null");
    }
    RpcResponse response = new RpcResponse(request.getStartTime(), request.getRequestId());
    try {
        Object result = handle(request);
        response.setResult(result);
    } catch (Throwable t) {
        response.setError(t);
    }
    // 根据netty上下方,取出通道,如果通道正常,返回将结果以异步的方式写入,返回到请求端
    if (ctx.channel().isOpen()) {
        try {
            MessageHolder<RpcResponse> messageHolder = new MessageHolder<RpcResponse>(Protocol.RESPONSE, Protocol.OK, response);
            ChannelFuture wf = ctx.channel().writeAndFlush(messageHolder).sync();
            wf.addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (!future.isSuccess()) {
                        ctx.channel().close();
                    }
                }
            });
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
        }
    } else {
        logger.warn("NettyServiceHandler : channel is close!");
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException) ChannelFutureListener(io.netty.channel.ChannelFutureListener) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException)

Aggregations

EmptyNullException (com.duangframework.core.exceptions.EmptyNullException)16 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 Document (org.bson.Document)2 Bson (org.bson.conversions.Bson)2 Mapping (com.duangframework.core.annotation.mvc.Mapping)1 Service (com.duangframework.core.annotation.mvc.Service)1 Rpc (com.duangframework.core.annotation.rpc.Rpc)1 AsyncContext (com.duangframework.core.common.dto.http.request.AsyncContext)1 HttpRequest (com.duangframework.core.common.dto.http.request.HttpRequest)1 HttpResponse (com.duangframework.core.common.dto.http.response.HttpResponse)1 RpcException (com.duangframework.core.exceptions.RpcException)1 ServiceException (com.duangframework.core.exceptions.ServiceException)1 Action (com.duangframework.mvc.core.Action)1 RpcAction (com.duangframework.rpc.common.RpcAction)1 ParamItem (com.duangframework.rule.entity.ParamItem)1 RuleParam (com.duangframework.rule.entity.RuleParam)1 RuleResult (com.duangframework.rule.entity.RuleResult)1 RuleInfoModel (com.duangframework.rule.entity.generate.RuleInfoModel)1 BasicDBObject (com.mongodb.BasicDBObject)1