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();
}
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;
}
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);
}
}
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();
}
}
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!");
}
}
Aggregations