|
@ -3,8 +3,6 @@ package com.yihu.hos.broker.services.camel;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.mongodb.client.MongoDatabase;
|
|
|
import com.yihu.hos.broker.configurations.MongoConfiguration;
|
|
|
import com.yihu.hos.broker.models.SystemCamelContext;
|
|
|
import com.yihu.hos.broker.models.SystemClassMapping;
|
|
|
import com.yihu.hos.core.constants.CoreConstant;
|
|
|
import com.yihu.hos.core.datatype.ClassFileUtil;
|
|
|
import com.yihu.hos.core.datatype.StringUtil;
|
|
@ -13,7 +11,6 @@ import com.yihu.hos.web.framework.constant.ServiceFlowConstant;
|
|
|
import com.yihu.hos.web.framework.model.Result;
|
|
|
import com.yihu.hos.web.framework.model.bo.ServiceFlow;
|
|
|
import com.yihu.hos.web.framework.util.GridFSUtil;
|
|
|
import org.apache.camel.builder.RouteBuilder;
|
|
|
import org.apache.log4j.LogManager;
|
|
|
import org.apache.log4j.Logger;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@ -57,24 +54,34 @@ public class ESBCamelService {
|
|
|
if (serviceFlowValid.is()) return Result.error("必要的入参数据不正确,请检查!");
|
|
|
|
|
|
ServiceFlow serviceFlow = serviceFlowValid.getServiceFlow();
|
|
|
ServiceFlow.HandleFile handleFile = serviceFlowValid.getHandleFile();
|
|
|
boolean created;
|
|
|
if ("java".equals(handleFile.getFileType())) {
|
|
|
created = this.generateClassFile(handleFile);
|
|
|
} else {
|
|
|
created = this.createClassFile(handleFile);
|
|
|
}
|
|
|
if (!created) {
|
|
|
return Result.error("服务流程变更增加失败!");
|
|
|
ArrayList<ServiceFlow.HandleFile> handleFiles = serviceFlowValid.getHandleFiles();
|
|
|
for (ServiceFlow.HandleFile handleFile : handleFiles) {
|
|
|
boolean created;
|
|
|
if ("java".equals(handleFile.getFileType())) {
|
|
|
logger.debug("generate class file...");
|
|
|
created = this.generateClassFile(handleFile);
|
|
|
} else {
|
|
|
logger.debug("create class file");
|
|
|
created = this.createClassFile(handleFile);
|
|
|
}
|
|
|
|
|
|
if (!created) {
|
|
|
logger.debug("create class file failed");
|
|
|
return Result.error("服务流程增加失败!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
this.addRouter(handleFile);
|
|
|
this.startRouter(serviceFlow.getRouteCode());
|
|
|
logger.debug("add camel router" + serviceFlow.getRouteCode());
|
|
|
this.addRouter(serviceFlow.getHandleFiles());
|
|
|
|
|
|
return Result.error("服务流程变更增加成功!");
|
|
|
logger.debug("start camel router," + serviceFlow.getRouteCode());
|
|
|
SystemCamelContext.startRouter(serviceFlow.getRouteCode());
|
|
|
|
|
|
logger.debug("start camel router success," + serviceFlow.getRouteCode());
|
|
|
return Result.error("服务流程增加成功!");
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e);
|
|
|
return Result.error("服务流程变更增加失败!");
|
|
|
return Result.error("服务流程增加失败!");
|
|
|
}
|
|
|
}
|
|
|
|
|
@ -98,10 +105,10 @@ public class ESBCamelService {
|
|
|
return Result.error("服务流程变更增加失败!");
|
|
|
}
|
|
|
|
|
|
this.stopRouter(serviceFlow.getRouteCode());
|
|
|
this.removeRouter(serviceFlow.getRouteCode());
|
|
|
this.addRouter(handleFile);
|
|
|
this.startRouter(serviceFlow.getRouteCode());
|
|
|
SystemCamelContext.stopRouter(serviceFlow.getRouteCode());
|
|
|
SystemCamelContext.removeRouter(serviceFlow.getRouteCode());
|
|
|
this.addRouter(serviceFlowValid.getHandleFiles());
|
|
|
SystemCamelContext.startRouter(serviceFlow.getRouteCode());
|
|
|
|
|
|
return Result.error("服务流程变更增加成功!");
|
|
|
} catch (Exception e) {
|
|
@ -124,8 +131,8 @@ public class ESBCamelService {
|
|
|
SystemCamelContext.getContext().stopRoute(routeCode);
|
|
|
SystemCamelContext.getContext().removeRoute(routeCode);
|
|
|
this.deleteClassFile(handleFile);
|
|
|
this.addRouter(handleFile);
|
|
|
this.startRouter(serviceFlow.getRouteCode());
|
|
|
this.addRouter(serviceFlowValid.getHandleFiles());
|
|
|
SystemCamelContext.startRouter(serviceFlow.getRouteCode());
|
|
|
|
|
|
return Result.success("服务流程变更减少成功!");
|
|
|
} catch (Exception e) {
|
|
@ -172,11 +179,11 @@ public class ESBCamelService {
|
|
|
if (!created) {
|
|
|
return Result.error("服务流程启动失败!");
|
|
|
}
|
|
|
|
|
|
this.addRouter(handleFile);
|
|
|
}
|
|
|
|
|
|
this.startRouter(serviceFlow.getRouteCode());
|
|
|
this.addRouter(handleFiles);
|
|
|
|
|
|
SystemCamelContext.startRouter(serviceFlow.getRouteCode());
|
|
|
|
|
|
return Result.success("服务流程启动成功!");
|
|
|
} catch (Exception e) {
|
|
@ -206,8 +213,8 @@ public class ESBCamelService {
|
|
|
if (handleFile == null) {
|
|
|
return false;
|
|
|
}
|
|
|
SystemClassMapping.put(handleFile.getRouteCode(), handleFile.getPackageName(), handleFile.getUsage(), handleFile.getClassName());
|
|
|
URL resource = SystemClassMapping.getResource(this);
|
|
|
SystemCamelContext.putClassMapping(handleFile.getRouteCode(), handleFile.getPackageName(), handleFile.getUsage(), handleFile.getClassName());
|
|
|
URL resource = SystemCamelContext.getResource(this);
|
|
|
FileOutputStream outputStream = ClassFileUtil.createFile(resource, handleFile.getPackageName(), handleFile.getClassName(), ClassFileUtil.CLASS_FILE);
|
|
|
|
|
|
String fileName = DES.decrypt(handleFile.getFilePath(), DES.COMMON_PASSWORD);
|
|
@ -225,8 +232,8 @@ public class ESBCamelService {
|
|
|
if (handleFile == null) {
|
|
|
return false;
|
|
|
}
|
|
|
SystemClassMapping.put(handleFile.getRouteCode(), handleFile.getPackageName(), handleFile.getUsage(), handleFile.getClassName());
|
|
|
URL resource = SystemClassMapping.getResource(this);
|
|
|
SystemCamelContext.putClassMapping(handleFile.getRouteCode(), handleFile.getPackageName(), handleFile.getUsage(), handleFile.getClassName());
|
|
|
URL resource = SystemCamelContext.getResource(this);
|
|
|
FileOutputStream outputStream = ClassFileUtil.createFile(resource, handleFile.getPackageName(), handleFile.getClassName(), ClassFileUtil.JAVA_FILE);
|
|
|
|
|
|
String fileName = DES.decrypt(handleFile.getFilePath(), DES.COMMON_PASSWORD);
|
|
@ -244,7 +251,7 @@ public class ESBCamelService {
|
|
|
}
|
|
|
|
|
|
private void deleteClassFile(ServiceFlow.HandleFile handleFile) {
|
|
|
String className = SystemClassMapping.get(handleFile.getRouteCode(), handleFile.getClassName(), handleFile.getUsage());
|
|
|
String className = SystemCamelContext.getClassMapping(handleFile.getRouteCode(), handleFile.getClassName(), handleFile.getUsage());
|
|
|
if (StringUtil.isEmpty(className)) {
|
|
|
return;
|
|
|
}
|
|
@ -261,41 +268,29 @@ public class ESBCamelService {
|
|
|
}
|
|
|
|
|
|
private void addRouter(ArrayList<ServiceFlow.HandleFile> handleFiles) throws Exception {
|
|
|
ArrayList<ServiceFlow.HandleFile> routerFiles = new ArrayList<>();
|
|
|
handleFiles.forEach(handleFile -> {
|
|
|
try {
|
|
|
addRouter(handleFile);
|
|
|
if (handleFile.getUsage().equals(ServiceFlowConstant.FLOW_TYPE_ROUTE)) {
|
|
|
String className = SystemCamelContext.getClassMapping(handleFile.getRouteCode(), handleFile.getClassName(), handleFile.getUsage());
|
|
|
SystemCamelContext.getQueue().put(className);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage());
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
private void addRouter(ServiceFlow.HandleFile handleFile) throws Exception {
|
|
|
if (handleFile.getUsage().equals(ServiceFlowConstant.FLOW_TYPE_PROCESSOR)) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
CamelClassLoader classLoader = new CamelClassLoader(CamelClassLoader.class.getClassLoader());
|
|
|
String path = ClassLoader.getSystemResource(CoreConstant.EMPTY).getPath();
|
|
|
String className = SystemClassMapping.get(handleFile.getRouteCode(), handleFile.getClassName(), handleFile.getUsage());
|
|
|
Class<RouteBuilder> routeBuilderClass = (Class<RouteBuilder>) classLoader.loadClass(path, className);
|
|
|
if (routeBuilderClass != null) {
|
|
|
RouteBuilder routeBuilder = routeBuilderClass.newInstance();
|
|
|
SystemCamelContext.getContext().addRoutes(routeBuilder);
|
|
|
}
|
|
|
}
|
|
|
routerFiles.forEach(handleFile -> {
|
|
|
try {
|
|
|
|
|
|
private void startRouter(String routeCode) throws Exception {
|
|
|
SystemCamelContext.getContext().startRoute(routeCode);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage());
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
});
|
|
|
|
|
|
private void stopRouter(String routeCode) throws Exception {
|
|
|
SystemCamelContext.getContext().stopRoute(routeCode);
|
|
|
}
|
|
|
|
|
|
private boolean removeRouter(String routeCode) throws Exception {
|
|
|
return SystemCamelContext.getContext().removeRoute(routeCode);
|
|
|
}
|
|
|
|
|
|
private class ServiceFlowValid {
|