فهرست منبع

修改broker流程事件实现

Airhead 8 سال پیش
والد
کامیت
945b567dce

+ 4 - 5
hos-broker/src/main/java/com/yihu/hos/broker/controllers/ESBCamelController.java

@ -4,21 +4,20 @@ import com.yihu.hos.broker.services.ESBCamelService;
import com.yihu.hos.web.framework.model.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
 * Created by lingfeng on 2016/8/4.
 */
@RestController
@RequestMapping("/esb")
public class ESBCamelController {
    @Resource(name = ESBCamelService.BEAN_ID)
    ESBCamelService esbCamelService;
    @Autowired
    private ESBCamelService esbCamelService;
    @RequestMapping(value = "/test", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ApiOperation(value = "新增Processor处理器", produces = "application/json", notes = "当外界组件通知一个新的processor处理器被定义时,该事件被触发")
@ -31,7 +30,7 @@ public class ESBCamelController {
    public Result onProcessorAdded(
            @ApiParam(name = "msg", value = "消息", required = true)
            @RequestBody() String msg) {
        return Result.success(msg);
        return esbCamelService.onProcessorAdded(msg);
    }

+ 3 - 131
hos-broker/src/main/java/com/yihu/hos/broker/listeners/ApplicationStartListener.java

@ -1,32 +1,15 @@
package com.yihu.hos.broker.listeners;
import com.mongodb.client.MongoDatabase;
import com.yihu.hos.broker.configurations.MongoConfiguration;
import com.yihu.hos.broker.common.constants.BrokerConstant;
import com.yihu.hos.broker.daos.BrokerDao;
import com.yihu.hos.core.constants.CoreConstant;
import com.yihu.hos.core.datatype.ClassFileUtil;
import com.yihu.hos.core.datatype.CollectionUtil;
import com.yihu.hos.core.encrypt.DES;
import com.yihu.hos.broker.models.SystemCamelContext;
import com.yihu.hos.broker.models.SystemClassMapping;
import com.yihu.hos.broker.models.SystemServiceFlow;
import com.yihu.hos.broker.models.SystemServiceFlowClass;
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.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ApplicationStartListener implements ApplicationListener<ContextRefreshedEvent> {
    private static Logger logger = LogManager.getLogger(ApplicationStartListener.class);
@ -45,114 +28,9 @@ public class ApplicationStartListener implements ApplicationListener<ContextRefr
     * 正式系统中,这个值可来自于系统的配置文件
     */
    private void camelRouteStart(ContextRefreshedEvent contextRefreshedEvent) throws Exception {
        //从数据库获取所有路由对应的class路径以便动态加载
        BrokerDao brokerDao = (BrokerDao) contextRefreshedEvent.getApplicationContext().getBean(BrokerDao.BEAN_ID);
        MongoConfiguration mongoConfig = contextRefreshedEvent.getApplicationContext().getBean(MongoConfiguration.class);
        MongoDatabase mongoDatabase = mongoConfig.mongoClient().getDatabase("upload");
        List<SystemServiceFlow> systemServiceFlowList = brokerDao.getSystemServiceFlowList();
        List<SystemServiceFlowClass> systemServiceFlowClassList = brokerDao.getSystemServiceFlowClassList();
        File systemClassPath = new File(this.getClass().getProtectionDomain().getClassLoader().getResource("").getPath());
        List<File> systemClassFlowPathList = new ArrayList<>();
        //对所有route与processor进行分类存储
        Map<String, List<SystemServiceFlowClass>> systemServiceFlowClassGroupMap = new HashMap<>();
        for (SystemServiceFlowClass systemServiceFlowClass : systemServiceFlowClassList) {
            Integer flowId = systemServiceFlowClass.getFlowId();
            String type = systemServiceFlowClass.getType();
            if (systemServiceFlowClassGroupMap.containsKey(type + flowId.toString())) {
                List<SystemServiceFlowClass> classList = systemServiceFlowClassGroupMap.get(type + flowId);
                classList.add(systemServiceFlowClass);
                systemServiceFlowClassGroupMap.put(type + flowId.toString(), classList);
            } else {
                List<SystemServiceFlowClass> classList = new ArrayList<>();
                classList.add(systemServiceFlowClass);
                systemServiceFlowClassGroupMap.put(type + flowId.toString(), classList);
            }
        }
        List<RouteBuilder> alreadyRouteBuilders = new ArrayList<>();
        Map<String, Boolean> isCorrectClassMap = new HashMap<>();
        for (SystemServiceFlow systemServiceFlow : systemServiceFlowList) {
            Integer flowId = systemServiceFlow.getId();
            String code = systemServiceFlow.getCode();
            //默认所有class均为正确
            // 这是system业务系统在本地存储class的根目录
            List<SystemServiceFlowClass> processesClassList = systemServiceFlowClassGroupMap.get("processor" + flowId);
            List<SystemServiceFlowClass> routesClassList = systemServiceFlowClassGroupMap.get("route" + flowId);
            // 创建processor文件
            isCorrectClassMap.put(code, true);
            if (!CollectionUtil.isEmpty(processesClassList)) {
                for (SystemServiceFlowClass processesClass : processesClassList) {
                    String className = processesClass.getClassName();
                    String packageName = processesClass.getPackageName();
                    String classPath = processesClass.getClassPath();
                    // 创建文件
                    FileOutputStream out = ClassFileUtil.createClassfile(systemClassPath.toURI().toURL(), packageName, className);
                    if (out != null) {
                        String fileName = DES.decrypt(classPath, DES.COMMON_PASSWORD);
                    // 记录到工具类中,以便其它线程需要时进行取用
                        if (GridFSUtil.readFile(mongoDatabase, out, fileName)) {
                            SystemClassMapping.getSystemClassNameMapping().put(code + BrokerConstant.PROCESSOR + className, packageName + CoreConstant.DOT + className);
                        } else {
                            isCorrectClassMap.put(code, false);
                        }
                    } else {
                        isCorrectClassMap.put(code, false);
                    }
                }
            }
            if (!CollectionUtil.isEmpty(routesClassList)) {
                // 创建route文件
                for (SystemServiceFlowClass routesClass : routesClassList) {
                    String className = routesClass.getClassName();
                    String packageName = routesClass.getPackageName();
                    String classPath = routesClass.getClassPath();
                    // 创建文件
                    FileOutputStream out = ClassFileUtil.createClassfile(systemClassPath.toURI().toURL(), packageName, className);
                    if (out != null) {
                        String fileName = DES.decrypt(classPath, DES.COMMON_PASSWORD);
                        // 记录到工具类中,以便其它线程需要时进行取用
                        if (GridFSUtil.readFile(mongoDatabase, out, fileName)) {
                            SystemClassMapping.getSystemClassNameMapping().put(code + BrokerConstant.ROUTE + className, packageName + CoreConstant.DOT + className);
                        } else {
                            isCorrectClassMap.put(code, false);
                        }
                    } else {
                        isCorrectClassMap.put(code, false);
                    }
                    if (isCorrectClassMap.get(code)) {
                        ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
                        try {
                            Class<RouteBuilder> routeBuilderClass = (Class<RouteBuilder>) currentClassLoader.loadClass(SystemClassMapping.getSystemClassNameMapping().get(code + BrokerConstant.ROUTE + className));
                            if (routeBuilderClass != null) {
                                RouteBuilder routeBuilder = routeBuilderClass.newInstance();
                                alreadyRouteBuilders.add(routeBuilder);
                            }
                        } catch (Exception e) {
                            logger.info("缺少class文件:" + code);
                             continue;
                        }
                    }
                }
            } else {
                isCorrectClassMap.put(code, false);
            }
            if (isCorrectClassMap.get(code)) {
                File systemClassFlowPath = new File(systemClassPath.getPath() + "/" + code);
                if (!systemClassFlowPathList.contains(systemClassFlowPath)) {
                    systemClassFlowPathList.add(systemClassFlowPath);
                }
            }
        }
        // 4、=============
        // 首先启动Apache Camel服务
        logger.info("Apache Camel Context 启动完成......");
        logger.info("Apache Camel Context 启动...");
        // 加载和设置ClassLoader
        List<URL> URLs = new ArrayList<>();
        for (File systemClassFlowPath : systemClassFlowPathList) {
            URLs.add(systemClassFlowPath.toURI().toURL());
        }
        ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
        ClassLoader camelESBClassLoader = new URLClassLoader(URLs.toArray(new URL[]{}), currentClassLoader);
@ -161,14 +39,8 @@ public class ApplicationStartListener implements ApplicationListener<ContextRefr
        // 然后就可以进行RouteBuilder的加载
        SystemCamelContext.getDefaultCamelContext().setTracing(true);
        SystemCamelContext.getDefaultCamelContext().setUseMDCLogging(true);
        for (RouteBuilder routeBuilder : alreadyRouteBuilders) {
            try {
                SystemCamelContext.getDefaultCamelContext().addRoutes(routeBuilder);
            } catch (Exception e) {
                logger.warn(e.getMessage(), e);
            }
        }
        SystemCamelContext.getDefaultCamelContext().start();
        logger.info("Apache Camel Context 启动完成...");
    }
}

+ 115 - 0
hos-broker/src/main/java/com/yihu/hos/broker/models/bo/ServiceFlow.java

@ -0,0 +1,115 @@
package com.yihu.hos.broker.models.bo;
import java.util.ArrayList;
import java.util.Date;
/**
 * @author Airhead
 * @since 2016/8/4.
 */
public class ServiceFlow {
    private String routeCode;
    private ArrayList<HandleFile> handleFiles;
    private Date updated;
    private String flowType;    //pull or push?
    private String cron;    //采集任务时使用
    public String getCron() {
        return cron;
    }
    public void setCron(String cron) {
        this.cron = cron;
    }
    public String getRouteCode() {
        return routeCode;
    }
    public void setRouteCode(String routeCode) {
        this.routeCode = routeCode;
    }
    public Date getUpdated() {
        return updated;
    }
    public void setUpdated(Date updated) {
        this.updated = updated;
    }
    public ArrayList<HandleFile> getHandleFiles() {
        return handleFiles;
    }
    public void setHandleFiles(ArrayList<HandleFile> handleFiles) {
        this.handleFiles = handleFiles;
    }
    public void addHandleFile(HandleFile handleFile) {
        if (handleFiles == null) {
            handleFiles = new ArrayList<>();
        }
        handleFiles.add(handleFile);
    }
    public String getFlowType() {
        return flowType;
    }
    public void setFlowType(String flowType) {
        this.flowType = flowType;
    }
    public class HandleFile {
        private String usage;   //router or processor
        private String packageName;
        private String className;
        private String filePath;
        private String fileType;    //java or class
        public HandleFile() {
        }
        public String getPackageName() {
            return packageName;
        }
        public void setPackageName(String packageName) {
            this.packageName = packageName;
        }
        public String getClassName() {
            return className;
        }
        public void setClassName(String className) {
            this.className = className;
        }
        public String getFilePath() {
            return filePath;
        }
        public void setFilePath(String filePath) {
            this.filePath = filePath;
        }
        public String getFileType() {
            return fileType;
        }
        public void setFileType(String fileType) {
            this.fileType = fileType;
        }
        public String getUsage() {
            return usage;
        }
        public void setUsage(String usage) {
            this.usage = usage;
        }
    }
}

+ 217 - 107
hos-broker/src/main/java/com/yihu/hos/broker/services/ESBCamelService.java

@ -1,17 +1,19 @@
package com.yihu.hos.broker.services;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.MongoDatabase;
import com.yihu.hos.broker.common.classLoader.DynamicClassLoader;
import com.yihu.hos.broker.common.compiler.CamelCompiler;
import com.yihu.hos.broker.common.compiler.ClassParams;
import com.yihu.hos.broker.configurations.MongoConfiguration;
import com.yihu.hos.broker.common.constants.BrokerConstant;
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.broker.models.bo.ServiceFlow;
import com.yihu.hos.core.constants.CoreConstant;
import com.yihu.hos.core.datatype.ClassFileUtil;
import com.yihu.hos.core.datatype.StringUtil;
import com.yihu.hos.core.encrypt.DES;
import com.yihu.hos.broker.models.SystemCamelContext;
import com.yihu.hos.broker.models.SystemClassMapping;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.GridFSUtil;
import org.apache.camel.builder.RouteBuilder;
@ -24,34 +26,43 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Map;
/**
 * Created by lingfeng on 2016/8/4.
 */
@Service("ESBCamelService")
@Service
public class ESBCamelService {
    public static final String BEAN_ID = "ESBCamelService";
    private static Logger logger = LogManager.getLogger(ESBCamelService.class);
    @Autowired
    private MongoConfiguration mongoConfig;
    private String dbName = "upload";
    private static Logger logger = LogManager.getLogger(ESBCamelService.class);
    @Autowired
    private ObjectMapper objectMapper;
    /**
     * 当外界组件通知一个新的processor处理器被定义时,该事件被触发。
     * @param serviceFlow 本次processor处理器变化,所涉及的业务系统唯一标识。
     * @param packageName processor处理器定义涉及的class包名
     * @param className processor处理器定义涉及的class类名
     * @param path processor处理器定义涉及的class对应路径
     */
    public Result onProcessorAdded(String serviceFlow, String packageName, String className, String path) {
    public Result onProcessorAdded(String msg) {
        try {
            if(StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
                    || StringUtil.isEmpty(className) || StringUtil.isEmpty(path)) {
            ServiceFlow serviceFlow = objectMapper.readValue(msg, ServiceFlow.class);
            ArrayList<ServiceFlow.HandleFile> handleFiles = serviceFlow.getHandleFiles();
            if (handleFiles == null || handleFiles.size() == 0) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            FileOutputStream out = this.createClassfile(serviceFlow, packageName, className, BrokerConstant.PROCESSOR);
            String fileName = DES.decrypt(path, DES.COMMON_PASSWORD);
            ServiceFlow.HandleFile handleFile = handleFiles.get(0);
            if (handleFile == null || StringUtil.isEmpty(serviceFlow.getRouteCode()) || StringUtil.isEmpty(handleFile.getPackageName())
                    || StringUtil.isEmpty(handleFile.getPackageName()) || StringUtil.isEmpty(handleFile.getFilePath())) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            FileOutputStream out = this.createClassfile(serviceFlow.getRouteCode(), handleFile.getPackageName(), handleFile.getPackageName(), BrokerConstant.PROCESSOR);
            String fileName = DES.decrypt(handleFile.getFilePath(), DES.COMMON_PASSWORD);
            MongoDatabase db = mongoConfig.mongoClient().getDatabase(dbName);
            if (GridFSUtil.readFile(db, out, fileName)) {
                return Result.success("新增处理器成功!");
@ -67,20 +78,33 @@ public class ESBCamelService {
    /**
     * 当外界组件通知一个已有的processor处理器data部分发生变化时,该事件被触发。
     */
    public Result onProcessorDataChanged(String serviceFlow , String packageName , String className , String path) {
    public Result onProcessorDataChanged(String msg) {
        try {
            if(StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
            ServiceFlow serviceFlow = objectMapper.readValue(msg, ServiceFlow.class);
            ArrayList<ServiceFlow.HandleFile> handleFiles = serviceFlow.getHandleFiles();
            if (handleFiles == null || handleFiles.size() == 0) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            ServiceFlow.HandleFile handleFile = handleFiles.get(0);
            String routeCode = serviceFlow.getRouteCode();
            String packageName = handleFile.getPackageName();
            String className = handleFile.getClassName();
            String path = handleFile.getFilePath();
            if (StringUtil.isEmpty(routeCode) || StringUtil.isEmpty(packageName)
                    || StringUtil.isEmpty(className) || StringUtil.isEmpty(path)) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            FileOutputStream out = this.updateClassfile(serviceFlow, packageName, className, BrokerConstant.PROCESSOR);
            FileOutputStream out = this.updateClassfile(routeCode, packageName, className, BrokerConstant.PROCESSOR);
            String fileName = DES.decrypt(path, DES.COMMON_PASSWORD);
            MongoDatabase db = mongoConfig.mongoClient().getDatabase(dbName);
            if (GridFSUtil.readFile(db, out, fileName)) {
                SystemCamelContext.getDefaultCamelContext().stopRoute(serviceFlow);
                SystemCamelContext.getDefaultCamelContext().removeRoute(serviceFlow);
                SystemCamelContext.getDefaultCamelContext().stopRoute(routeCode);
                SystemCamelContext.getDefaultCamelContext().removeRoute(routeCode);
                DynamicClassLoader classLoader = new DynamicClassLoader(DynamicClassLoader.class.getClassLoader());
                Class<RouteBuilder> routeBuilderClass = (Class<RouteBuilder>) classLoader.loadClass(this.getClass().getProtectionDomain().getClassLoader().getResource("").getPath(), SystemClassMapping.getSystemClassNameMapping().get(serviceFlow + BrokerConstant.ROUTE + className));
                classLoader.loadClass(ClassLoader.getSystemResource(CoreConstant.EMPTY).getPath(), SystemClassMapping.getSystemClassNameMapping().get(serviceFlow + BrokerConstant.PROCESSOR + className));
@ -101,17 +125,29 @@ public class ESBCamelService {
    /**
     * 当外界组件通知一个已有的Processor路由定义被删除时,该事件被触发。
     */
    public Result onProcessorDataDelete(String serviceFlow, String packageName, String className) {
    public Result onProcessorDataDelete(String msg) {
        try {
            if(StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
            ServiceFlow serviceFlow = objectMapper.readValue(msg, ServiceFlow.class);
            ArrayList<ServiceFlow.HandleFile> handleFiles = serviceFlow.getHandleFiles();
            if (handleFiles == null || handleFiles.size() == 0) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            ServiceFlow.HandleFile handleFile = handleFiles.get(0);
            String routeCode = serviceFlow.getRouteCode();
            String packageName = handleFile.getPackageName();
            String className = handleFile.getClassName();
            if (StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
                    || StringUtil.isEmpty(className)) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            SystemCamelContext.getDefaultCamelContext().stopRoute(serviceFlow);
            SystemCamelContext.getDefaultCamelContext().removeRoute(serviceFlow);
            this.deleteClassfile(serviceFlow, packageName, className, BrokerConstant.PROCESSOR);
            SystemCamelContext.getDefaultCamelContext().stopRoute(routeCode);
            SystemCamelContext.getDefaultCamelContext().removeRoute(routeCode);
            this.deleteClassfile(routeCode, packageName, className, BrokerConstant.PROCESSOR);
            return Result.success("删除路由成功!");
        } catch (Exception e) {
            return Result.error("删除路由失败!");
@ -121,22 +157,35 @@ public class ESBCamelService {
    /**
     * 当外界组件通知一个新的RouteDefine路由被定义时,该事件被触发
     */
    public Result onRouteDefineAdded(String serviceFlow, String packageName, String className, String path) {
    public Result onRouteDefineAdded(String msg) {
        try {
            if(StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
            ServiceFlow serviceFlow = objectMapper.readValue(msg, ServiceFlow.class);
            ArrayList<ServiceFlow.HandleFile> handleFiles = serviceFlow.getHandleFiles();
            if (handleFiles == null || handleFiles.size() == 0) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            ServiceFlow.HandleFile handleFile = handleFiles.get(0);
            String routeCode = serviceFlow.getRouteCode();
            String packageName = handleFile.getPackageName();
            String className = handleFile.getClassName();
            String path = handleFile.getFilePath();
            if (StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
                    || StringUtil.isEmpty(className) || StringUtil.isEmpty(path)) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            // 第1、2两步处理过程,都是在这里完成
            FileOutputStream out = this.createClassfile(serviceFlow, packageName, className, BrokerConstant.ROUTE);
            FileOutputStream out = this.createClassfile(routeCode, packageName, className, BrokerConstant.ROUTE);
            String fileName = DES.decrypt(path, DES.COMMON_PASSWORD);
            MongoDatabase db = mongoConfig.mongoClient().getDatabase(dbName);
            if (GridFSUtil.readFile(db, out, fileName)) {
                // 3、===============加载到CamelContext中
                DynamicClassLoader classLoader = new DynamicClassLoader(DynamicClassLoader.class.getClassLoader());
                Class<RouteBuilder> routeBuilderClass = (Class<RouteBuilder>) classLoader.loadClass(SystemClassMapping.getSystemClassNameMapping().get(serviceFlow + BrokerConstant.ROUTE + className));
                if(routeBuilderClass != null) {
                if (routeBuilderClass != null) {
                    RouteBuilder routeBuilder = routeBuilderClass.newInstance();
                    SystemCamelContext.getDefaultCamelContext().addRoutes(routeBuilder);
                }
@ -153,22 +202,36 @@ public class ESBCamelService {
    /**
     * 当外界组件通知一个已有的RouteDefine路由定义被改变时,主要就是路由定义内容被改变时,该事件被触发。
     */
    public Result onRouteDefineChanged(String serviceFlow, String packageName, String className, String path) {
    public Result onRouteDefineChanged(String msg) {
        try {
            ServiceFlow serviceFlow = objectMapper.readValue(msg, ServiceFlow.class);
            ArrayList<ServiceFlow.HandleFile> handleFiles = serviceFlow.getHandleFiles();
            if (handleFiles == null || handleFiles.size() == 0) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            ServiceFlow.HandleFile handleFile = handleFiles.get(0);
            String routeCode = serviceFlow.getRouteCode();
            String packageName = handleFile.getPackageName();
            String className = handleFile.getClassName();
            String path = handleFile.getFilePath();
            if (StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
                    || StringUtil.isEmpty(className) || StringUtil.isEmpty(path)) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            SystemCamelContext.getDefaultCamelContext().stopRoute(serviceFlow);
            SystemCamelContext.getDefaultCamelContext().removeRoute(serviceFlow);
            FileOutputStream out = this.updateClassfile(serviceFlow, packageName, className, BrokerConstant.ROUTE);
            SystemCamelContext.getDefaultCamelContext().stopRoute(routeCode);
            SystemCamelContext.getDefaultCamelContext().removeRoute(routeCode);
            FileOutputStream out = this.updateClassfile(routeCode, packageName, className, BrokerConstant.ROUTE);
            String fileName = DES.decrypt(path, DES.COMMON_PASSWORD);
            MongoDatabase db = mongoConfig.mongoClient().getDatabase(dbName);
            if (GridFSUtil.readFile(db, out, fileName)) {
                // 3、===============加载到CamelContext中
                DynamicClassLoader classLoader = new DynamicClassLoader(DynamicClassLoader.class.getClassLoader());
                Class<RouteBuilder> routeBuilderClass = (Class<RouteBuilder>) classLoader.loadClass(ClassLoader.getSystemResource(CoreConstant.EMPTY).getPath(), SystemClassMapping.getSystemClassNameMapping().get(serviceFlow + BrokerConstant.ROUTE+className));
                Class<RouteBuilder> routeBuilderClass = (Class<RouteBuilder>) classLoader.loadClass(ClassLoader.getSystemResource(CoreConstant.EMPTY).getPath(), SystemClassMapping.getSystemClassNameMapping().get(serviceFlow + BrokerConstant.ROUTE + className));
                if (routeBuilderClass != null) {
                    RouteBuilder routeBuilder = routeBuilderClass.newInstance();
                    SystemCamelContext.getDefaultCamelContext().addRoutes(routeBuilder);
@ -184,17 +247,31 @@ public class ESBCamelService {
    /**
     * 当外界组件通知一个已有的RouteDefine路由定义被删除时,该事件被触发。
     */
    public Result onRouteDefineDelete(String serviceFlow, String packageName, String className) {
    public Result onRouteDefineDelete(String msg) {
        try {
            if(StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
            ServiceFlow serviceFlow = objectMapper.readValue(msg, ServiceFlow.class);
            ArrayList<ServiceFlow.HandleFile> handleFiles = serviceFlow.getHandleFiles();
            if (handleFiles == null || handleFiles.size() == 0) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            ServiceFlow.HandleFile handleFile = handleFiles.get(0);
            String routeCode = serviceFlow.getRouteCode();
            String packageName = handleFile.getPackageName();
            String className = handleFile.getClassName();
            String path = handleFile.getFilePath();
            if (StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
                    || StringUtil.isEmpty(className)) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            SystemCamelContext.getDefaultCamelContext().stopRoute(serviceFlow);
            SystemCamelContext.getDefaultCamelContext().removeRoute(serviceFlow);
            this.deleteClassfile(serviceFlow, packageName, className, BrokerConstant.ROUTE);
            SystemCamelContext.getDefaultCamelContext().stopRoute(routeCode);
            SystemCamelContext.getDefaultCamelContext().removeRoute(routeCode);
            this.deleteClassfile(routeCode, packageName, className, BrokerConstant.ROUTE);
            return Result.success("删除路由成功!");
        } catch (Exception e) {
            return Result.error("删除路由失败!");
@ -204,14 +281,28 @@ public class ESBCamelService {
    /**
     * 启动路由时,该事件被触发。
     */
    public Result onRouteDefineStart(String serviceFlow) {
    public Result onRouteDefineStart(String msg) {
        try {
            if(StringUtil.isEmpty(serviceFlow)) {
            ServiceFlow serviceFlow = objectMapper.readValue(msg, ServiceFlow.class);
            ArrayList<ServiceFlow.HandleFile> handleFiles = serviceFlow.getHandleFiles();
            if (handleFiles == null || handleFiles.size() == 0) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            SystemCamelContext.getDefaultCamelContext().startRoute(serviceFlow);
            ServiceFlow.HandleFile handleFile = handleFiles.get(0);
            String routeCode = serviceFlow.getRouteCode();
            String packageName = handleFile.getPackageName();
            String className = handleFile.getClassName();
            String path = handleFile.getFilePath();
            if (StringUtil.isEmpty(serviceFlow)) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            SystemCamelContext.getDefaultCamelContext().startRoute(routeCode);
            return Result.success("启动路由成功!");
        } catch (Exception e) {
            return Result.error("启动路由失败!");
@ -221,82 +312,55 @@ public class ESBCamelService {
    /**
     * 停止路由时,该事件被触发。
     */
    public Result onRouteDefineStop(String serviceFlow) {
    public Result onRouteDefineStop(String msg) {
        try {
            if(StringUtil.isEmpty(serviceFlow)) {
            ServiceFlow serviceFlow = objectMapper.readValue(msg, ServiceFlow.class);
            ArrayList<ServiceFlow.HandleFile> handleFiles = serviceFlow.getHandleFiles();
            if (handleFiles == null || handleFiles.size() == 0) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            ServiceFlow.HandleFile handleFile = handleFiles.get(0);
            String routeCode = serviceFlow.getRouteCode();
            String packageName = handleFile.getPackageName();
            String className = handleFile.getClassName();
            String path = handleFile.getFilePath();
            if (StringUtil.isEmpty(serviceFlow)) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            SystemCamelContext.getDefaultCamelContext().stopRoute(serviceFlow);
            SystemCamelContext.getDefaultCamelContext().stopRoute(routeCode);
            return Result.success("停止路由成功!");
        } catch (Exception e) {
            return Result.error("停止路由失败!");
        }
    }
    public Result onProcessorClassAdded(String msg) {
        try {
            ServiceFlow serviceFlow = objectMapper.readValue(msg, ServiceFlow.class);
            ArrayList<ServiceFlow.HandleFile> handleFiles = serviceFlow.getHandleFiles();
            if (handleFiles == null || handleFiles.size() == 0) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            ServiceFlow.HandleFile handleFile = handleFiles.get(0);
            String routeCode = serviceFlow.getRouteCode();
            String packageName = handleFile.getPackageName();
            String className = handleFile.getClassName();
            String path = handleFile.getFilePath();
    private FileOutputStream createClassfile(String serviceFlow, String packageName, String className, String type) throws MalformedURLException {
        // 1、============
        File systemClassFlowPath = new File(this.getClass().getProtectionDomain().getClassLoader().getResource("").getPath());
        // 记录到工具类中,以便其它线程需要时进行取用
        SystemClassMapping.getSystemClassNameMapping().put(serviceFlow + type + className, packageName + CoreConstant.DOT + className);
        // 2、============开始写入class文件
        FileOutputStream out = ClassFileUtil.createClassfile(systemClassFlowPath.toURI().toURL(), packageName, className);
        // 完成
        logger.info("===================" + packageName + CoreConstant.DOT + className + ".class 生成过程结束");
        return out;
    }
    private FileOutputStream updateClassfile(String serviceFlow, String packageName, String className, String type) throws MalformedURLException {
        // 1、============
        Map<String, String> systemClassNameMapping = SystemClassMapping.getSystemClassNameMapping();
        String systemClassName = systemClassNameMapping.get(serviceFlow + type + className);
        if(StringUtil.isEmpty(systemClassName)) {
            return null;
        }
        File systemClassFlowPath = new File(this.getClass().getProtectionDomain().getClassLoader().getResource("").getPath());
        // 2、============开始写入class文件
        FileOutputStream out = ClassFileUtil.updateClassfile(systemClassFlowPath.toURI().toURL(), packageName, className);
        // 完成
        logger.info("===================" + packageName + CoreConstant.DOT + className + ".class 修改过程结束");
        return out;
    }
    private void deleteClassfile(String serviceFlow, String packageName, String className, String type) {
        // 1、============
        Map<String, String> systemClassNameMapping = SystemClassMapping.getSystemClassNameMapping();
        String systemClassName = systemClassNameMapping.get(serviceFlow + type + className);
        if(StringUtil.isEmpty(systemClassName)) {
            return;
        }
        String packagePath = StringUtil.replaceStrAll(packageName, ".", "/");
        String classPath = ClassLoader.getSystemResource("").getPath() + "/" + packagePath + "/" + className + ".class";
        // 2、============开始写入class文件
        ClassFileUtil.deleteClassfile(classPath);
        // 完成
        logger.info("===================" + packageName + CoreConstant.DOT + className + ".class 删除过程结束");
    }
    /* **************************       修改任务cron生成新的camel文件 add by hzy   *********************************** */
    public Result onProcessorClassAdded(String serviceFlow, String packageName, String className, String path) {
        try {
            if(StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
            if (StringUtil.isEmpty(serviceFlow) || StringUtil.isEmpty(packageName)
                    || StringUtil.isEmpty(className) || StringUtil.isEmpty(path)) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
            }
            return this.genProcessorFile(serviceFlow, packageName, className, path);
            return this.genProcessorFile(routeCode, packageName, className, path);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e);
@ -309,7 +373,7 @@ public class ESBCamelService {
     */
    public Result onRouteClassAdded(ClassParams params) {
        try {
            if(StringUtil.isEmpty(params.getRouteId()) || StringUtil.isEmpty(params.getPackageName())
            if (StringUtil.isEmpty(params.getRouteId()) || StringUtil.isEmpty(params.getPackageName())
                    || StringUtil.isEmpty(params.getOldClassName()) || StringUtil.isEmpty(params.getFilePath())) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
@ -320,22 +384,21 @@ public class ESBCamelService {
            // 3、===============加载到CamelContext中
            ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
            Class<RouteBuilder> routeBuilderClass = (Class<RouteBuilder>) currentClassLoader.loadClass(SystemClassMapping.getSystemClassNameMapping().get(params.getRouteId() + BrokerConstant.ROUTE + params.getNewClassName()));
            if(routeBuilderClass != null) {
            if (routeBuilderClass != null) {
                RouteBuilder routeBuilder = routeBuilderClass.newInstance();
                SystemCamelContext.getDefaultCamelContext().addRoutes(routeBuilder);
            }
            return Result.success("新增路由成功!");
        } catch (Exception e) {
           e.printStackTrace();
            e.printStackTrace();
            logger.error(e);
            return Result.error("新增路由失败!");
        }
    }
    public Result onRouteClassChanged(ClassParams params) {
        try {
            if(StringUtil.isEmpty(params.getRouteId()) || StringUtil.isEmpty(params.getPackageName())
            if (StringUtil.isEmpty(params.getRouteId()) || StringUtil.isEmpty(params.getPackageName())
                    || StringUtil.isEmpty(params.getOldClassName()) || StringUtil.isEmpty(params.getFilePath())) {
                logger.error("必要的入参数据不正确,请检查!");
                return Result.error("必要的入参数据不正确,请检查!");
@ -360,12 +423,15 @@ public class ESBCamelService {
    }
    /* **************************       修改任务cron生成新的camel文件 add by hzy   *********************************** */
    public Result genProcessorFile(String serviceFlow, String packageName, String className, String path) {
        try {
            String filePath= CamelCompiler.genProcessClass(serviceFlow, path, packageName, className);
            if (filePath !=null){
            String filePath = CamelCompiler.genProcessClass(serviceFlow, path, packageName, className);
            if (filePath != null) {
                return Result.success(filePath);
            }else {
            } else {
                return Result.error("生成新文件失败1!");
            }
@ -385,6 +451,50 @@ public class ESBCamelService {
        }
    }
    private FileOutputStream createClassfile(String serviceFlow, String packageName, String className, String type) throws MalformedURLException {
        // 1、============
        File systemClassFlowPath = new File(this.getClass().getProtectionDomain().getClassLoader().getResource("").getPath());
        // 记录到工具类中,以便其它线程需要时进行取用
        SystemClassMapping.getSystemClassNameMapping().put(serviceFlow + type + className, packageName + CoreConstant.DOT + className);
        // 2、============开始写入class文件
        FileOutputStream out = ClassFileUtil.createClassfile(systemClassFlowPath.toURI().toURL(), packageName, className);
        // 完成
        logger.info("===================" + packageName + CoreConstant.DOT + className + ".class 生成过程结束");
        return out;
    }
    private FileOutputStream updateClassfile(String serviceFlow, String packageName, String className, String type) throws MalformedURLException {
        // 1、============
        Map<String, String> systemClassNameMapping = SystemClassMapping.getSystemClassNameMapping();
        String systemClassName = systemClassNameMapping.get(serviceFlow + type + className);
        if (StringUtil.isEmpty(systemClassName)) {
            return null;
        }
        File systemClassFlowPath = new File(this.getClass().getProtectionDomain().getClassLoader().getResource("").getPath());
        // 2、============开始写入class文件
        FileOutputStream out = ClassFileUtil.updateClassfile(systemClassFlowPath.toURI().toURL(), packageName, className);
        // 完成
        logger.info("===================" + packageName + CoreConstant.DOT + className + ".class 修改过程结束");
        return out;
    }
    private void deleteClassfile(String serviceFlow, String packageName, String className, String type) {
        // 1、============
        Map<String, String> systemClassNameMapping = SystemClassMapping.getSystemClassNameMapping();
        String systemClassName = systemClassNameMapping.get(serviceFlow + type + className);
        if (StringUtil.isEmpty(systemClassName)) {
            return;
        }
        String packagePath = StringUtil.replaceStrAll(packageName, ".", "/");
        String classPath = ClassLoader.getSystemResource("").getPath() + "/" + packagePath + "/" + className + ".class";
        // 2、============开始写入class文件
        ClassFileUtil.deleteClassfile(classPath);
        // 完成
        logger.info("===================" + packageName + CoreConstant.DOT + className + ".class 删除过程结束");
    }
}