Browse Source

流程编排代码重构,简化编排事件处理

Airhead 8 năm trước cách đây
mục cha
commit
224cc3c9f4

+ 0 - 30
hos-arbiter/src/main/java/com/yihu/hos/arbiter/common/constants/Constants.java

@ -1,30 +0,0 @@
package com.yihu.hos.arbiter.common.constants;
/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/8/25.
 */
public interface Constants {
    //流程-模板类型
    String JAVA = "java";
    String CLASS = "class";
    //流程-操作消息
    String FlOW_REFRESH = "flowRefresh";
    String FLOW_STARTED = "flowStarted";
    String PROCESSOR_ADDED = "processorAdded";
    String PROCESSOR_DATA_CHANGED = "processorDataChanged";
    String PROCESSOR_DATA_DELETED = "processorDataDeleted";
    String ROUTE_DEFINE_ADDED = "routeDefineAdded";
    String ROUTE_DEFINE_CHANGED = "routeDefineChanged";
    String ROUTE_DEFINE_DELETED = "routeDefineDeleted";
    String ROUTE_CLASS_ADDED = "routeClassAdded";   //java类型的路由添加
    String ROUTE_CLASS_CHANGED = "routeClassChanged";//java类型的路由修改
    String PROCESSOR_CLASS_ADDED = "processorClassAdded"; //java类型的处理器添加
    String PROCESSOR_CLASS_CHANGED = "processorClassChanged"; //java类型的处理器修改
    //BrokerServer
    String BROKER_SERVER_ON = "brokerServerOn";
    String BROKER_SERVER_OFF = "brokerServerOff";
}

+ 9 - 14
hos-arbiter/src/main/java/com/yihu/hos/arbiter/routers/ServiceFlowEventRouter.java

@ -1,7 +1,7 @@
package com.yihu.hos.arbiter.routers;
import com.yihu.hos.arbiter.common.constants.Constants;
import com.yihu.hos.arbiter.configuration.ActivemqConfiguration;
import com.yihu.hos.web.framework.constant.ServiceFlowConstant;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
@ -29,19 +29,14 @@ public class ServiceFlowEventRouter extends RouteBuilder {
        from("service.flow.event:queue:configuration.service.flow")
                .choice()
                .when(header("tenant").isNotNull()).to("bean:serviceFlowService?method=proxy")
                .when(header("event").isEqualTo(Constants.FlOW_REFRESH)).to("bean:serviceFlowService?method=flowRefresh")
                .when(header("event").isEqualTo(Constants.PROCESSOR_ADDED)).to("bean:serviceFlowService?method=processorAdded")
                .when(header("event").isEqualTo(Constants.PROCESSOR_DATA_CHANGED)).to("bean:serviceFlowService?method=processorDataChanged")
                .when(header("event").isEqualTo(Constants.PROCESSOR_DATA_DELETED)).to("bean:serviceFlowService?method=processorDataDeleted")
                .when(header("event").isEqualTo(Constants.ROUTE_DEFINE_ADDED)).to("bean:serviceFlowService?method=routeDefineAdded")
                .when(header("event").isEqualTo(Constants.ROUTE_DEFINE_CHANGED)).to("bean:serviceFlowService?method=routeDefineChanged")
                .when(header("event").isEqualTo(Constants.ROUTE_DEFINE_DELETED)).to("bean:serviceFlowService?method=routeDefineDeleted")
                .when(header("event").isEqualTo(Constants.ROUTE_CLASS_ADDED)).to("bean:serviceFlowService?method=routeClassAdded")
                .when(header("event").isEqualTo(Constants.ROUTE_CLASS_CHANGED)).to("bean:serviceFlowService?method=routeClassChanged")
                .when(header("event").isEqualTo(Constants.PROCESSOR_CLASS_ADDED)).to("bean:serviceFlowService?method=processorClassAdded")
                .when(header("event").isEqualTo(Constants.PROCESSOR_CLASS_CHANGED)).to("bean:serviceFlowService?method=processorClassChanged")
                .when(header("event").isEqualTo(Constants.BROKER_SERVER_ON)).to("bean:serviceFlowService?method=brokerServerOn")
                .when(header("event").isEqualTo(Constants.FLOW_STARTED)).to("bean:brokerServerService?method=flowStarted")
                .when(header("event").isEqualTo(ServiceFlowConstant.SERVICE_FLOW_STARTED)).to("bean:serviceFlowService?method=serviceFlowStarted")
                .when(header("event").isEqualTo(ServiceFlowConstant.SERVICE_FLOW_STOPPED)).to("bean:serviceFlowService?method=serviceFlowStopped")
                .when(header("event").isEqualTo(ServiceFlowConstant.SERVICE_FLOW_ADDED)).to("bean:serviceFlowService?method=serviceFlowAdd")
                .when(header("event").isEqualTo(ServiceFlowConstant.SERVICE_FLOW_MODIFIED_ADD)).to("bean:serviceFlowService?method=serviceFlowModifyAdd")
                .when(header("event").isEqualTo(ServiceFlowConstant.SERVICE_FLOW_MODIFIED_REDUCE)).to("bean:serviceFlowService?method=serviceFlowModifyReduce")
                .when(header("event").isEqualTo(ServiceFlowConstant.SERVICE_FLOW_DELETED)).to("bean:serviceFlowService?method=serviceFlowDelete")
                .when(header("event").isEqualTo(ServiceFlowConstant.BROKER_SERVER_ON)).to("bean:serviceFlowService?method=brokerServerOn")
                .when(header("event").isEqualTo(ServiceFlowConstant.BROKER_SERVER_OFF)).to("bean:brokerServerService?method=brokerServerOff")
                .endChoice();
    }
}

+ 2 - 2
hos-arbiter/src/main/java/com/yihu/hos/arbiter/services/BrokerServerService.java

@ -3,8 +3,8 @@ package com.yihu.hos.arbiter.services;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.WriteResult;
import com.yihu.hos.arbiter.common.constants.Constants;
import com.yihu.hos.arbiter.models.BrokerServer;
import com.yihu.hos.web.framework.constant.ServiceFlowConstant;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.log4j.LogManager;
@ -66,7 +66,7 @@ public class BrokerServerService {
            String msg = objectMapper.writeValueAsString(brokerServer);
            ProducerTemplate producerTemplate = createProducerTemplate();
            Map<String, Object> header = new HashMap<>();
            header.put("event", Constants.BROKER_SERVER_ON);
            header.put("event", ServiceFlowConstant.BROKER_SERVER_ON);
            producerTemplate.sendBodyAndHeaders("service.flow.event", msg, header);
        } catch (JsonProcessingException e) {
            e.printStackTrace();

+ 21 - 37
hos-arbiter/src/main/java/com/yihu/hos/arbiter/services/ServiceFlowService.java

@ -2,11 +2,11 @@ package com.yihu.hos.arbiter.services;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.hos.arbiter.common.constants.Constants;
import com.yihu.hos.arbiter.models.BrokerServer;
import com.yihu.hos.web.framework.model.bo.ServiceFlow;
import com.yihu.hos.core.http.HTTPResponse;
import com.yihu.hos.core.http.HttpClientKit;
import com.yihu.hos.web.framework.constant.ServiceFlowConstant;
import com.yihu.hos.web.framework.model.bo.ServiceFlow;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
@ -66,48 +66,28 @@ public class ServiceFlowService {
        return null;
    }
    public void flowRefresh(String msg) {
        flowController("post", "/esb/serviceFlow", msg);
    }
    public void processorAdded(String msg) {
        flowController("post", "/esb/processor", msg);
    }
    public void processorDataChanged(String msg) {
        flowController("put", "/esb/processor", msg);
    }
    public void processorDataDeleted(String msg) {
        flowController("delete", "/esb/processor", msg);
    }
    public void routeDefineAdded(String msg) {
        flowController("post", "/esb/route", msg);
    }
    public void routeDefineChanged(String msg) {
        flowController("put", "/esb/route", msg);
    public void serviceFlowStarted(String msg) {
        flowController("post", "/esb/serviceFlow/start", msg);
    }
    public void routeDefineDeleted(String msg) {
        flowController("delete", "/esb/route", msg);
    public void serviceFlowStopped(String msg) {
        flowController("post", "/esb/serviceFlow/stop", msg);
    }
    public void routeClassAdded(String msg) {
        flowController("post", "/esb/genRoute", msg);
    public void serviceFlowAdd(String msg) {
        flowController("post", "/esb/serviceFlow", msg);
    }
    public void routeClassChanged(String msg) {
        flowController("put", "/esb/updateRoute", msg);
    public void serviceFlowModifyAdd(String msg) {
        flowController("put", "/esb/serviceFlow/add", msg);
    }
    public void processorClassAdded(String msg) {
        flowController("post", "/esb/genProcessor", msg);
    public void serviceFlowModifyReduce(String msg) {
        flowController("put", "/esb/serviceFlow/reduce", msg);
    }
    public void processorClassChanged(String msg) {
        flowController("put", "/esb/genProcessor", msg);
    public void serviceFlowDelete(String msg) {
        flowController("delete", "/esb/serviceFlow", msg);
    }
    public void brokerServerOn(String msg) {
@ -115,7 +95,7 @@ public class ServiceFlowService {
        serviceFlowList.forEach(serviceFlow -> {
            try {
                String serviceFlowMsg = objectMapper.writeValueAsString(serviceFlow);
                flowController("post", "/esb/serviceFlow", serviceFlowMsg);
                serviceFlowStarted(msg);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
@ -123,6 +103,10 @@ public class ServiceFlowService {
    }
    public void brokerServerOff(String msg) {
        //下先Broker就可以了
    }
    /**
     * SAAS化的管理端过来的消息会被proxy进行中转,之后发送到终端的Arbiter对Broker进行实际的控制。
@ -138,7 +122,7 @@ public class ServiceFlowService {
            ServiceFlow serviceFlow = objectMapper.readValue(msg, ServiceFlow.class);
            this.save(serviceFlow);
            boolean one = Constants.JAVA.equals(serviceFlow.getFlowType());   //有cron表达式,就是采集任务。
            boolean one = ServiceFlowConstant.JAVA.equals(serviceFlow.getFlowType());   //有cron表达式,就是采集任务。
            if (one) {
                List<BrokerServer> flowOnBroker = brokerServerService.getFlowOnBroker(serviceFlow.getRouteCode());
                if (flowOnBroker != null && flowOnBroker.size() != 0) {
@ -154,7 +138,7 @@ public class ServiceFlowService {
                switch (method) {
                    case "post":
                        HTTPResponse response = HttpClientKit.post(brokerServer.getURL() + path, msg);
                        if (response.getStatusCode() == 200){
                        if (response.getStatusCode() == 200) {
                            String body = response.getBody();
                        }
                        break;

+ 23 - 73
hos-broker/src/main/java/com/yihu/hos/broker/controllers/ESBCamelController.java

@ -25,102 +25,52 @@ public class ESBCamelController {
        return Result.success("test");
    }
    @RequestMapping(value = "/processor", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ApiOperation(value = "新增Processor处理器", produces = "application/json", notes = "当外界组件通知一个新的processor处理器被定义时,该事件被触发")
    public Result onProcessorAdded(
    @RequestMapping(value = "/serviceFlow", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ApiOperation(value = "新增流程", produces = "application/json", notes = "当Arbiter通知一个的流程被创建时,该事件被触发")
    public Result onServiceFlowAdd(
            @ApiParam(name = "msg", value = "消息", required = true)
            @RequestBody() String msg) {
        return esbCamelService.onServiceFlowAdd(msg);
    }
    @RequestMapping(value = "/processor", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
    @ApiOperation(value = "修改Processor处理器", produces = "application/json", notes = "当外界组件通知一个已有的processor处理器data部分发生变化时,该事件被触发")
    public Result onProcessorDataChanged(
            @ApiParam(name = "msg", value = "消息", required = true)
            @RequestBody() String msg) {
        return Result.success(msg);
    }
    @RequestMapping(value = "/processor", produces = "application/json;charset=UTF-8", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除Processor处理器", produces = "application/json", notes = "当外界组件通知一个已有的processor处理器data部分发生删除时,该事件被触发")
    public Result onProcessorDataDelete(
            @ApiParam(name = "msg", value = "消息", required = true)
            @RequestBody() String msg) {
        return Result.success(msg);
    }
    @RequestMapping(value = "/route", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ApiOperation(value = "新增Route路由", produces = "application/json", notes = "当外界组件通知一个新的RouteDefine路由被定义时,该事件被触发")
    public Result onRouteDefineAdded(
            @ApiParam(name = "msg", value = "消息", required = true)
            @RequestBody() String msg) {
        return Result.success(msg);
    }
    @RequestMapping(value = "/route", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
    @ApiOperation(value = "修改Route路由", produces = "application/json", notes = "当外界组件通知一个已有的RouteDefine路由定义被改变时,主要就是路由定义内容被改变时,该事件被触发")
    public Result onRouteDefineChanged(
            @ApiParam(name = "msg", value = "消息", required = true)
            @RequestBody() String msg) {
        return Result.success(msg);
    }
    @RequestMapping(value = "/route", produces = "application/json;charset=UTF-8", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除Route路由", produces = "application/json", notes = "当外界组件通知一个已有的RouteDefine路由定义被删除时,该事件被触发")
    public Result onRouteDefineDelete(
            @ApiParam(name = "msg", value = "消息", required = true)
            @RequestBody() String msg) {
        return Result.success(msg);
    }
    @RequestMapping(value = "/route/start", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
    @ApiOperation(value = "删除Route路由", produces = "application/json", notes = "启动路由时,该事件被触发")
    public Result onRouteDefineStart(
            @ApiParam(name = "msg", value = "消息", required = true)
            @RequestBody() String msg) {
        return Result.success(msg);
    }
    @RequestMapping(value = "/route/stop", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
    @ApiOperation(value = "删除Route路由", produces = "application/json", notes = "停止路由时,该事件被触发")
    public Result onRouteDefineStop(
    @RequestMapping(value = "/serviceFlow/add", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
    @ApiOperation(value = "修改流程", produces = "application/json", notes = "当Arbiter通知一个的流程被修改(Router,Process被修改或增加)时,该事件被触发")
    public Result onServiceFlowModifyAdd(
            @ApiParam(name = "msg", value = "消息", required = true)
            @RequestBody() String msg) {
        return Result.success(msg);
        return esbCamelService.onServiceFlowModifyAdd(msg);
    }
    @RequestMapping(value = "/genRoute", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ApiOperation(value = "生成新的route文件", produces = "application/json", notes = "生成新的camel文件")
    public Result genRoute(
    @RequestMapping(value = "/serviceFlow/reduce", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
    @ApiOperation(value = "修改流程", produces = "application/json", notes = "当Arbiter通知一个的流程被修改(Router,Process被删除)时,该事件被触发")
    public Result onServiceFlowModifyReduce(
            @ApiParam(name = "msg", value = "消息", required = true)
            @RequestBody() String msg) {
        return Result.success(msg);
        return esbCamelService.onServiceFlowModifyReduce(msg);
    }
    @RequestMapping(value = "/updateRoute", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
    @ApiOperation(value = "修改route文件", produces = "application/json", notes = "生成新的camel文件")
    public Result updateRoute(
    @RequestMapping(value = "/serviceFlow", produces = "application/json;charset=UTF-8", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除流程", produces = "application/json", notes = "当Arbiter通知一个的流程被删除时,该事件被触发")
    public Result onServiceFlowDelete(
            @ApiParam(name = "msg", value = "消息", required = true)
            @RequestBody() String msg) {
        return Result.success(msg);
        return esbCamelService.onServiceFlowDelete(msg);
    }
    @RequestMapping(value = "/genProcessor", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ApiOperation(value = "生成新的processor文件", produces = "application/json", notes = "生成新的camel文件")
    public Result genProcessor(
    @RequestMapping(value = "/serviceFlow/start", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ApiOperation(value = "启动服务流程", produces = "application/json", notes = "启动服务流程时,该事件被触发")
    public Result onServiceFlowStart(
            @ApiParam(name = "msg", value = "消息", required = true)
            @RequestBody() String msg) {
        return Result.success(msg);
        return esbCamelService.onServiceFlowStart(msg);
    }
    @RequestMapping(value = "/serviceFlow", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ApiOperation(value = "新增Processor处理器", produces = "application/json", notes = "当外界组件通知一个新的processor处理器被定义时,该事件被触发")
    public Result flowRefresh(
    @RequestMapping(value = "/serviceFlow/stop", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ApiOperation(value = "停止服务流程", produces = "application/json", notes = "停止服务流程时,该事件被触发")
    public Result onServiceFlowStop(
            @ApiParam(name = "msg", value = "消息", required = true)
            @RequestBody() String msg) {
        return esbCamelService.onServiceFlowStart(msg);
        return esbCamelService.onServiceFlowStop(msg);
    }
}

+ 1 - 1
hos-broker/src/main/java/com/yihu/hos/broker/services/camel/ESBCamelService.java

@ -109,7 +109,7 @@ public class ESBCamelService {
    /**
     * 当外界组件通知一个已有的RouteDefine路由定义被删除时,该事件被触发。
     */
    public Result onServiceFlowModifyRedurce(String msg) {
    public Result onServiceFlowModifyReduce(String msg) {
        try {
            ServiceFlowValid serviceFlowValid = new ServiceFlowValid(msg).invoke();
            if (serviceFlowValid.is()) return Result.error("必要的入参数据不正确,请检查!");

+ 10 - 11
hos-web-framework/src/main/java/com/yihu/hos/web/framework/constant/ServiceFlowConstant.java

@ -25,15 +25,14 @@ public interface ServiceFlowConstant {
    String FLOW_OP_ADD = "add";
    //流程-操作消息
    String FlOW_REFRESH = "flowRefresh";
    String PROCESSOR_ADDED = "processorAdded";
    String PROCESSOR_DATA_CHANGED = "processorDataChanged";
    String PROCESSOR_DATA_DELETED = "processorDataDeleted";
    String ROUTE_DEFINE_ADDED = "routeDefineAdded";
    String ROUTE_DEFINE_CHANGED = "routeDefineChanged";
    String ROUTE_DEFINE_DELETED = "routeDefineDeleted";
    String ROUTE_CLASS_ADDED = "routeClassAdded";   //java类型的路由添加
    String ROUTE_CLASS_CHANGED = "routeClassChanged";//java类型的路由修改
    String PROCESSOR_CLASS_ADDED = "processorClassAdded"; //java类型的处理器添加
    String PROCESSOR_CLASS_CHANGED = "processorClassChanged"; //java类型的处理器修改
    String SERVICE_FLOW_STARTED = "serviceFlowStarted"; //启动服务流程
    String SERVICE_FLOW_STOPPED = "serviceFlowStopped"; //停止服务流程
    String SERVICE_FLOW_ADDED = "serviceFlowAdded"; //添加服务流程
    String SERVICE_FLOW_MODIFIED_ADD = "serviceFlowModifiedAdd";    //修改服务流程,修改或增加Router,Processor
    String SERVICE_FLOW_MODIFIED_REDUCE = "serviceFlowModifiedReduce";//修改服务流程,删除Router,Processor
    String SERVICE_FLOW_DELETED = "serviceFlowModifiedDelete";  //删除服务流程
    //BrokerServer
    String BROKER_SERVER_ON = "brokerServerOn"; //Broker启动
    String BROKER_SERVER_OFF = "brokerServerOff";//Broker停止
}

+ 0 - 8
hos-web-framework/src/main/java/com/yihu/hos/web/framework/model/bo/ServiceFlow.java

@ -15,15 +15,7 @@ public class ServiceFlow {
    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;

+ 3 - 3
src/main/java/com/yihu/hos/listeners/ApplicationStart.java

@ -31,7 +31,7 @@ public class ApplicationStart implements ServletContextListener {
    public void contextInitialized(ServletContextEvent context) {
        //使用自定义转化器转化时间格式
        ConvertUtils.register(new DateConvert(), Date.class);
        this.flowRefresh();
        this.serviceFlowStarted();
    }
    @Override
@ -42,10 +42,10 @@ public class ApplicationStart implements ServletContextListener {
     * admin启动时,触发一次所有流程更新事件,用于重启整个服务的情况。
     * 同时解决Broker中启动多个采集任务的问题。
     */
    private void flowRefresh() {
    private void serviceFlowStarted() {
        try {
            List<ServiceFlow> serviceFlowList = flowManager.getServiceFlowList();
            serviceFlowList.forEach(serviceFlow -> serviceFlowEventService.flowRefresh(serviceFlow));
            serviceFlowList.forEach(serviceFlow -> serviceFlowEventService.serviceFlowStarted(serviceFlow));
        } catch (Exception e) {
            e.printStackTrace();
        }

+ 109 - 108
src/main/java/com/yihu/hos/services/ServiceFlowEventService.java

@ -33,8 +33,12 @@ public class ServiceFlowEventService {
     * admin启动时,触发一次所有流程更新事件,用于重启整个服务的情况。
     * 同时解决Broker中启动多个采集任务的问题。
     */
    public void flowRefresh(ServiceFlow serviceFlow) {
        this.sendMsg(ServiceFlowConstant.FlOW_REFRESH, serviceFlow);
    public void serviceFlowStarted(ServiceFlow serviceFlow) {
        this.sendMsg(ServiceFlowConstant.SERVICE_FLOW_STARTED, serviceFlow);
    }
    public void serviceFlowStopped(ServiceFlow serviceFlow) {
        this.sendMsg(ServiceFlowConstant.SERVICE_FLOW_STOPPED, serviceFlow);
    }
    /**
@ -45,125 +49,122 @@ public class ServiceFlowEventService {
     * @param className   processor处理器定义涉及的class类名
     * @param path        processor处理器定义涉及的class内容,如果zookeeper数据结构中class分片存储,在业务级接口层面上也进行了合并
     */
    public void processorAdded(String serviceFlow, String packageName, String className, String path) {
        sendMsg(ServiceFlowConstant.PROCESSOR_ADDED, serviceFlow, packageName, className, path);
    }
    public void processorAdded(ServiceFlow serviceFlow) {
        this.sendMsg(ServiceFlowConstant.PROCESSOR_ADDED, serviceFlow);
//    public void serviceFlowAdded(String serviceFlow, String packageName, String className, String path) {
//        sendMsg(ServiceFlowConstant.SERVICE_FLOW_ADDED, serviceFlow, packageName, className, path);
//    }
    public void serviceFlowAdded(ServiceFlow serviceFlow) {
        this.sendMsg(ServiceFlowConstant.SERVICE_FLOW_ADDED, serviceFlow);
    }
    /**
     * 当外界组件通知一个已有的processor处理器data部分发生变化时,该事件被触发。
     */
    public void processorDataChanged(String routeCode, String packageName, String className, String path) {
        this.sendMsg(ServiceFlowConstant.PROCESSOR_DATA_CHANGED, routeCode, packageName, className, path);
    }
    public void processorDataChanged(ServiceFlow serviceFlow) {
        this.sendMsg(ServiceFlowConstant.PROCESSOR_DATA_CHANGED, serviceFlow);
//    public void serviceFlowModifiedAdd(String routeCode, String packageName, String className, String path) {
//        this.sendMsg(ServiceFlowConstant.SERVICE_FLOW_MODIFIED_ADD, routeCode, packageName, className, path);
//    }
    public void serviceFlowModifiedAdd(ServiceFlow serviceFlow) {
        this.sendMsg(ServiceFlowConstant.SERVICE_FLOW_MODIFIED_ADD, serviceFlow);
    }
    public void processorDataDeleted(String routeCode, String packageName, String className) {
        this.sendMsg(ServiceFlowConstant.PROCESSOR_DATA_DELETED, routeCode, packageName, className, null);
//    public void serviceFlowModifiedReduce(String routeCode, String packageName, String className) {
//        this.sendMsg(ServiceFlowConstant.SERVICE_FLOW_MODIFIED_REDUCE, routeCode, packageName, className, null);
//
//    }
    }
    public void processorDataDeleted(ServiceFlow serviceFlow) {
        this.sendMsg(ServiceFlowConstant.PROCESSOR_DATA_DELETED, serviceFlow);
    public void serviceFlowModifiedReduce(ServiceFlow serviceFlow) {
        this.sendMsg(ServiceFlowConstant.SERVICE_FLOW_MODIFIED_REDUCE, serviceFlow);
    }
    /**
     * 当外界组件通知一个新的RouteDefine路由被定义时,该事件被触发
     */
    public void routeDefineAdded(String routeCode, String packageName, String className, String path) {
        this.sendMsg(ServiceFlowConstant.ROUTE_DEFINE_ADDED, routeCode, packageName, className, path);
    }
    public void routeDefineAdded(ServiceFlow serviceFlow) {
        this.sendMsg(ServiceFlowConstant.ROUTE_DEFINE_ADDED, serviceFlow);
    }
    /**
     * 当外界组件通知一个已有的RouteDefine路由定义被改变时,主要就是路由定义内容被改变时,该事件被触发。
     */
    public void routeDefineChanged(String routeCode, String packageName, String className, String path) {
        this.sendMsg(ServiceFlowConstant.ROUTE_DEFINE_CHANGED, routeCode, packageName, className, path);
    }
    public void routeDefineChanged(ServiceFlow serviceFlow) {
        this.sendMsg(ServiceFlowConstant.ROUTE_DEFINE_CHANGED, serviceFlow);
    }
    /**
     * 当外界组件通知一个已有的RouteDefine路由定义被删除时,该事件被触发。
     */
    public void routeDefineDelete(String routeCode, String packageName, String className) {
        this.sendMsg(ServiceFlowConstant.ROUTE_DEFINE_DELETED, routeCode, packageName, className, null);
    }
    public void routeDefineDelete(ServiceFlow serviceFlow) {
        this.sendMsg(ServiceFlowConstant.ROUTE_DEFINE_DELETED, serviceFlow);
    }
    public void routeClassAdded(String routeCode, String packageName, String className, String path, String cron) {
        this.sendGenMsg(ServiceFlowConstant.ROUTE_CLASS_ADDED, routeCode, packageName, className, path, cron);
    }
    public void routeClassAdded(ServiceFlow serviceFlow) {
        this.sendMsg(ServiceFlowConstant.ROUTE_CLASS_ADDED, serviceFlow);
    }
    public void routeClassChanged(String routeCode, String packageName, String className, String path, String cron) {
        this.sendGenMsg(ServiceFlowConstant.ROUTE_CLASS_CHANGED, routeCode, packageName, className, path, cron);
    }
    public void routeClassChanged(ServiceFlow serviceFlow) {
        this.sendMsg(ServiceFlowConstant.ROUTE_CLASS_CHANGED, serviceFlow);
    }
    public void processorClassAdded(String routeCode, String packageName, String className, String path) {
        this.sendMsg(ServiceFlowConstant.PROCESSOR_CLASS_ADDED, routeCode, packageName, className, path);
    }
    public void processorClassAdded(ServiceFlow serviceFlow) {
        this.sendMsg(ServiceFlowConstant.PROCESSOR_CLASS_ADDED, serviceFlow);
    }
    public void processorClassChanged(String routeCode, String packageName, String className, String path) {
        this.sendMsg(ServiceFlowConstant.PROCESSOR_CLASS_CHANGED, routeCode, packageName, className, path);
    }
    public void processorClassChanged(ServiceFlow serviceFlow) {
        this.sendMsg(ServiceFlowConstant.PROCESSOR_CLASS_CHANGED, serviceFlow);
    }
    private void sendMsg(String event, String routeCode, String packageName, String className, String path) {
        ServiceFlow flow = new ServiceFlow();
        flow.setRouteCode(routeCode);
        ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
        handleFile.setPackageName(packageName);
        handleFile.setClassName(className);
        handleFile.setFilePath(path);
        handleFile.setFileType(ServiceFlowConstant.CLASS);
        handleFile.setRouteCode(routeCode);
        flow.addHandleFile(handleFile);
        this.sendMsg(event, flow);
    }
    private void sendGenMsg(String event, String routeCode, String packageName, String className, String path, String cron) {
        ServiceFlow flow = new ServiceFlow();
        flow.setRouteCode(routeCode);
        ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
        handleFile.setPackageName(packageName);
        handleFile.setClassName(className);
        handleFile.setFilePath(path);
        handleFile.setFileType(ServiceFlowConstant.JAVA);
        handleFile.setRouteCode(routeCode);
        flow.addHandleFile(handleFile);
        this.sendMsg(event, flow);
    }
//    public void serviceFlowDelete(String routeCode, String packageName, String className, String path) {
//        this.sendMsg(ServiceFlowConstant.SERVICE_FLOW_DELETED, routeCode, packageName, className, path);
//    }
    public void serviceFlowDelete(ServiceFlow serviceFlow) {
        this.sendMsg(ServiceFlowConstant.SERVICE_FLOW_DELETED, serviceFlow);
    }
//
//    /**
//     * 当外界组件通知一个已有的RouteDefine路由定义被改变时,主要就是路由定义内容被改变时,该事件被触发。
//     */
//    public void routeDefineChanged(String routeCode, String packageName, String className, String path) {
//        this.sendMsg(ServiceFlowConstant.ROUTE_DEFINE_CHANGED, routeCode, packageName, className, path);
//    }
//
//    public void routeDefineChanged(ServiceFlow serviceFlow) {
//        this.sendMsg(ServiceFlowConstant.ROUTE_DEFINE_CHANGED, serviceFlow);
//    }
//
//    /**
//     * 当外界组件通知一个已有的RouteDefine路由定义被删除时,该事件被触发。
//     */
//    public void routeDefineDelete(String routeCode, String packageName, String className) {
//        this.sendMsg(ServiceFlowConstant.ROUTE_DEFINE_DELETED, routeCode, packageName, className, null);
//    }
//
//    public void routeDefineDelete(ServiceFlow serviceFlow) {
//        this.sendMsg(ServiceFlowConstant.ROUTE_DEFINE_DELETED, serviceFlow);
//    }
//
//    public void routeClassAdded(String routeCode, String packageName, String className, String path, String cron) {
//        this.sendGenMsg(ServiceFlowConstant.ROUTE_CLASS_ADDED, routeCode, packageName, className, path, cron);
//    }
//
//    public void routeClassAdded(ServiceFlow serviceFlow) {
//        this.sendMsg(ServiceFlowConstant.ROUTE_CLASS_ADDED, serviceFlow);
//    }
//
//    public void routeClassChanged(String routeCode, String packageName, String className, String path, String cron) {
//        this.sendGenMsg(ServiceFlowConstant.ROUTE_CLASS_CHANGED, routeCode, packageName, className, path, cron);
//    }
//
//    public void routeClassChanged(ServiceFlow serviceFlow) {
//        this.sendMsg(ServiceFlowConstant.ROUTE_CLASS_CHANGED, serviceFlow);
//    }
//
//    public void processorClassAdded(String routeCode, String packageName, String className, String path) {
//        this.sendMsg(ServiceFlowConstant.PROCESSOR_CLASS_ADDED, routeCode, packageName, className, path);
//    }
//
//    public void processorClassAdded(ServiceFlow serviceFlow) {
//        this.sendMsg(ServiceFlowConstant.PROCESSOR_CLASS_ADDED, serviceFlow);
//    }
//
//    public void processorClassChanged(String routeCode, String packageName, String className, String path) {
//        this.sendMsg(ServiceFlowConstant.PROCESSOR_CLASS_CHANGED, routeCode, packageName, className, path);
//    }
//
//    public void processorClassChanged(ServiceFlow serviceFlow) {
//        this.sendMsg(ServiceFlowConstant.PROCESSOR_CLASS_CHANGED, serviceFlow);
//    }
//
//    private void sendMsg(String event, String routeCode, String packageName, String className, String path) {
//        ServiceFlow flow = new ServiceFlow();
//        flow.setRouteCode(routeCode);
//        ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
//        handleFile.setPackageName(packageName);
//        handleFile.setClassName(className);
//        handleFile.setFilePath(path);
//        handleFile.setFileType(ServiceFlowConstant.CLASS);
//        handleFile.setRouteCode(routeCode);
//        flow.addHandleFile(handleFile);
//
//        this.sendMsg(event, flow);
//    }
//
//    private void sendGenMsg(String event, String routeCode, String packageName, String className, String path, String cron) {
//        ServiceFlow flow = new ServiceFlow();
//        flow.setRouteCode(routeCode);
//        ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
//        handleFile.setPackageName(packageName);
//        handleFile.setClassName(className);
//        handleFile.setFilePath(path);
//        handleFile.setFileType(ServiceFlowConstant.JAVA);
//        handleFile.setRouteCode(routeCode);
//        flow.addHandleFile(handleFile);
//        this.sendMsg(event, flow);
//    }
    private void sendMsg(String event, ServiceFlow serviceFlow) {
        try {

+ 158 - 117
src/main/java/com/yihu/hos/system/service/FlowManager.java

@ -53,12 +53,80 @@ public class FlowManager implements IFlowManage {
    @Autowired
    private MongoConfig mongoConfig;
    /**
     * 生成Route流程的java文件
     *
     * @param routeId      流程Id
     * @param className    模板类名
     * @param tempFilePath 模板文件路径
     * @param newCron      cron表达式
     * @return
     */
    public static String genRouteJavaFile(String routeId, String className, String tempFilePath, String newCron) {
        try {
            String newFileName = className + routeId + ".java";
            String newFilePath = "/temp/" + newFileName;
            String text = GridFSUtil.readFile("upload", tempFilePath);
            if (text.contains("?cron=")) {
                String oldStr = text.substring(text.indexOf("?cron=") + 6);
                String cron = oldStr.substring(0, oldStr.indexOf("\""));
                text = text.replace(cron, newCron);
            }
            //修改java类名
            if (text.contains(className)) {
                text = text.replace(className, className + routeId);//新类名规则=旧类名+routeId
            }
            //修改routeId;模板规则 routeId("routeId")
            text = text.replace("routeId(\"routeId\")", "routeId(\"" + routeId + "\")");
            boolean succ = FileUtil.writeFile(newFilePath, text, "UTF-8");
            //TODO 上传到GridFS
            if (succ) {
                newFileName = GridFSUtil.uploadFile("upload", newFilePath, newFileName);
            } else {
                return null;
            }
            return newFileName;
        } catch (Exception e) {
            System.out.println("修改Route的java文件操作出错");
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 生成processor的java文件
     *
     * @param routeId      流程Code
     * @param tempFilePath 模板文件名
     * @param className    模板类名
     * @return
     */
    public static String genProcessorJavaFile(String routeId, String tempFilePath, String className) {
        try {
            String newFileName = className + routeId + ".java";
            String newFilePath = "/temp/" + className;
            String text = GridFSUtil.readFile("upload", tempFilePath);
            boolean succ = FileUtil.writeFile(newFilePath, text, "UTF-8");
            //TODO 上传到GridFS
            if (succ) {
                newFileName = GridFSUtil.uploadFile("upload", newFilePath, newFileName);
                return newFileName;
            }
        } catch (Exception e) {
            System.out.println("生成processor的java文件操作出错");
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public Result getFlowList(Map<String, Object> params) throws Exception {
        return flowDao.getFlowList(params);
    }
    /**
     * @return List<ServiceFlow> 返回所有可运行流程
     * @throws Exception ...
@ -142,6 +210,8 @@ public class FlowManager implements IFlowManage {
        return Result.success("保存成功");
    }
        /* ===================================  flowClass 部分================================================*/
    @Transactional
    public Result updateFlow(SystemServiceFlow obj) throws Exception {
        SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, obj.getId());
@ -216,10 +286,10 @@ public class FlowManager implements IFlowManage {
        SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, id);
        List<SystemServiceFlowClass> flowClassList = flowClassDao.getFlowClassByFlowId(id);
        List<SystemServiceFlowClass> processorFlowClassList = new ArrayList<>();
        if (ServiceFlowConstant.JAVA.equals(flow.getFileType())){
        if (ServiceFlowConstant.JAVA.equals(flow.getFileType())) {
            flowTempDao.deleteFlowTempByFlowId(id);
        } else {
            for (SystemServiceFlowClass flowClass:flowClassList){
            for (SystemServiceFlowClass flowClass : flowClassList) {
                flowClassDao.deleteEntity(flowClass);
                flowClass.setIsUpdate("1");
                //发送消息到MQ对列
@ -239,9 +309,6 @@ public class FlowManager implements IFlowManage {
    }
        /* ===================================  flowClass 部分================================================*/
    @Override
    public Result getFlowClassList(Map<String, Object> params) throws Exception {
        return null;
@ -318,33 +385,42 @@ public class FlowManager implements IFlowManage {
    /**
     * 发送MQ消息-更新路由
     *
     * @param flowCode  服务流程Code标识
     * @param flowClass
     * @param operate
     */
    public  void sendUpdateMessage(String flowCode,SystemServiceFlowClass flowClass,String operate){
        //发送消息到MQ对列
        if ("1".equals(flowClass.getIsUpdate()) && ServiceFlowConstant.FLOW_TYPE_ROUTE.equals(flowClass.getType())) {
            //route
            switch (operate){
                case "add" : serviceFlowEventService.routeDefineAdded(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
                case "delete" : serviceFlowEventService.routeDefineDelete(flowCode, flowClass.getPackageName(), flowClass.getClassName()); break;
                case "update" : serviceFlowEventService.routeDefineChanged(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
                default : break;
            }
        } else if ("1".equals(flowClass.getIsUpdate()) && ServiceFlowConstant.FLOW_TYPE_PROCESSOR.equals(flowClass.getType())) {
            //processor
            switch (operate){
                case "add" : serviceFlowEventService.processorAdded(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
                case "delete" : serviceFlowEventService.processorDataDeleted(flowCode, flowClass.getPackageName(), flowClass.getClassName()); break;
                case "update" : serviceFlowEventService.processorDataChanged(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
                default : break;
    public void sendUpdateMessage(String flowCode, SystemServiceFlowClass flowClass, String operate) {
        if ("1".equals(flowClass.getIsUpdate())) {
            ServiceFlow serviceFlow = new ServiceFlow();
            serviceFlow.setRouteCode(flowCode);
            serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
            ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
            handleFile.setRouteCode(flowCode);
            handleFile.setFileType(ServiceFlowConstant.CLASS);
            handleFile.setPackageName(flowClass.getPackageName());
            handleFile.setClassName(flowClass.getClassName());
            handleFile.setFilePath(flowClass.getClassPath());
            handleFile.setUsage(flowClass.getType());
            serviceFlow.addHandleFile(handleFile);
            switch (operate) {
                case "add":
                case "update":
                    serviceFlowEventService.serviceFlowModifiedAdd(serviceFlow);
                    break;
                case "delete":
                    serviceFlowEventService.serviceFlowModifiedReduce(serviceFlow);
                    break;
                default:
                    break;
            }
        }
    }
    /**
     * 获取流程列表
     *
     * @param type 流程的文件类型
     * @return
     * @throws Exception
@ -367,9 +443,9 @@ public class FlowManager implements IFlowManage {
        return re;
    }
    /**
     * 发送消息到broker,生成camel相关文件
     *
     * @param flowTempId
     * @param newCron
     * @throws Exception
@ -378,16 +454,16 @@ public class FlowManager implements IFlowManage {
        Long timestamp = System.currentTimeMillis();
        //发送生成processor文件的消息
        Integer newFlowId = sendAddProcessor(flowTempId, timestamp);
        if (newFlowId != null){
        if (newFlowId != null) {
            //发送生成route文件的消息
            newFlowId = sendAddRoute(flowTempId, newFlowId, newCron);
            if (newFlowId !=null){
            if (newFlowId != null) {
                return newFlowId;
            }else {
            } else {
                System.out.println("生成route文件失败");
                return null;
            }
        }else {
        } else {
            System.out.println("生成processor文件失败");
            return null;
        }
@ -395,35 +471,46 @@ public class FlowManager implements IFlowManage {
    /**
     * 修改任务,修改camel相关文件
     * @param flowId 流程ID
     * @param newCron  新cron
     *
     * @param flowId  流程ID
     * @param newCron 新cron
     * @return
     * @throws Exception
     */
    @Override
    public Integer updateCamelFile(Integer flowTempId,Integer flowId, String newCron) throws Exception {
    public Integer updateCamelFile(Integer flowTempId, Integer flowId, String newCron) throws Exception {
         /* 修改route文件无需重新生成flowClass记录,文件名根据className+routeId 生成;*/
        List<SystemServiceFlowTemp> flowTempRouters = flowTempDao.getFlowTemps(flowTempId, ServiceFlowConstant.FLOW_TYPE_ROUTE);
        List<SystemServiceFlowClass> flowClassRouters = flowClassDao.getFlowClass(flowId, ServiceFlowConstant.FLOW_TYPE_ROUTE);
        SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class,flowId);
        SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, flowId);
        //route模板文件记录是否存在。不存在就返回。
        if (!flowTempRouters.isEmpty()){
            SystemServiceFlowTemp flowTemp =flowTempRouters.get(0);
            StringBuilder basePath = new StringBuilder();;
            if (flowTemp.getPackageName()!=null){
        if (!flowTempRouters.isEmpty()) {
            SystemServiceFlowTemp flowTemp = flowTempRouters.get(0);
            StringBuilder basePath = new StringBuilder();
            ;
            if (flowTemp.getPackageName() != null) {
                String packagePath[] = flowTemp.getPackageName().split("\\.");
                for (int i=0;i<packagePath.length;i++){
                for (int i = 0; i < packagePath.length; i++) {
                    basePath.append(packagePath[i]).append("/");
                }
            }
            serviceFlowEventService.routeClassChanged(flow.getCode(),basePath.toString(), flowTemp.getClassName(), flowTemp.getClassPath(),newCron);
            ServiceFlow serviceFlow = new ServiceFlow();
            serviceFlow.setRouteCode(flow.getCode());
            serviceFlow.setFlowType(ServiceFlowConstant.JAVA);
            ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
            handleFile.setRouteCode(flow.getCode());
            handleFile.setFileType(ServiceFlowConstant.JAVA);
            handleFile.setPackageName(flowTemp.getClassPath());
            handleFile.setClassName(flowTemp.getClassName());
            handleFile.setFilePath(basePath.toString());
            handleFile.setUsage(ServiceFlowConstant.FLOW_TYPE_ROUTE);
            serviceFlowEventService.serviceFlowModifiedAdd(serviceFlow);
            return flowId;
        }
        return null;
    }
    /* *********************       发送消息方式生成文件   ********************************/
    public Integer sendAddRoute(Integer tempId, Integer flowId, String newCron) throws Exception {
        List<SystemServiceFlowTemp> flowTempRouters = flowTempDao.getFlowTemps(tempId, ServiceFlowConstant.FLOW_TYPE_ROUTE);
@ -453,11 +540,22 @@ public class FlowManager implements IFlowManage {
            flowClassDao.saveEntity(newFlowClass);
            newFlowClass.setIsUpdate("1");
            //生成新的route文件
            String  newFileName = genRouteJavaFile(newFlow.getCode(),flowTemp.getClassName(),deName,newCron);
            if (newFileName!=null){
                serviceFlowEventService.routeClassAdded(newFlow.getCode(), basePath.toString(),  flowTemp.getClassName(), deName,newCron);
            String newFileName = genRouteJavaFile(newFlow.getCode(), flowTemp.getClassName(), deName, newCron);
            if (newFileName != null) {
                ServiceFlow serviceFlow = new ServiceFlow();
                serviceFlow.setRouteCode(newFlow.getCode());
                serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
                ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
                handleFile.setRouteCode(newFlow.getCode());
                handleFile.setFileType(ServiceFlowConstant.CLASS);
                handleFile.setPackageName(flowTemp.getPackageName());
                handleFile.setClassName(flowTemp.getClassName());
                handleFile.setFilePath(newFileName);
                handleFile.setUsage(ServiceFlowConstant.FLOW_TYPE_ROUTE);
                serviceFlowEventService.serviceFlowModifiedAdd(serviceFlow);
                return newFlow.getId();
            }else {
            } else {
                System.out.println("生成route的java文件过程出错");
                return null;
            }
@ -507,12 +605,23 @@ public class FlowManager implements IFlowManage {
                processClass.setType(ServiceFlowConstant.FLOW_TYPE_PROCESSOR);
                processClass.setIsUpdate("1");
                //生成新的java文件
                String newFileName = genProcessorJavaFile(newFlow.getCode(),deName,processClass.getClassName());
                if (newFileName!=null){
                String newFileName = genProcessorJavaFile(newFlow.getCode(), deName, processClass.getClassName());
                if (newFileName != null) {
                    //发送消息
                    serviceFlowEventService.processorClassAdded(newFlow.getCode(),proPath.toString(), processClass.getClassName(), deName);
                    ServiceFlow serviceFlow = new ServiceFlow();
                    serviceFlow.setRouteCode(newFlow.getCode());
                    serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
                    ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
                    handleFile.setRouteCode(newFlow.getCode());
                    handleFile.setFileType(ServiceFlowConstant.CLASS);
                    handleFile.setPackageName(processClass.getPackageName());
                    handleFile.setClassName(processClass.getClassName());
                    handleFile.setFilePath(newFileName);
                    handleFile.setUsage(ServiceFlowConstant.FLOW_TYPE_PROCESSOR);
                    serviceFlowEventService.serviceFlowModifiedAdd(serviceFlow);
                    flowClassDao.saveEntity(processClass);
                }else {
                } else {
                    System.out.println("生成processor的java文件过程出错");
                    return null;
                }
@ -524,72 +633,4 @@ public class FlowManager implements IFlowManage {
    }
    /**
     * 生成Route流程的java文件
     * @param routeId       流程Id
     * @param className     模板类名
     * @param tempFilePath      模板文件路径
     * @param newCron       cron表达式
     * @return
     */
    public static String genRouteJavaFile(String routeId,String className,String tempFilePath,String newCron) {
        try {
            String newFileName = className+routeId+".java";
            String newFilePath = "/temp/"+newFileName;
            String text = GridFSUtil.readFile("upload",tempFilePath);
            if (text.contains("?cron=")) {
                String oldStr = text.substring(text.indexOf("?cron=") + 6);
                String cron = oldStr.substring(0, oldStr.indexOf("\""));
                text = text.replace(cron,newCron);
            }
            //修改java类名
            if (text.contains(className)) {
                text = text.replace(className, className+routeId);//新类名规则=旧类名+routeId
            }
            //修改routeId;模板规则 routeId("routeId")
            text = text.replace("routeId(\"routeId\")", "routeId(\"" + routeId + "\")");
            boolean succ = FileUtil.writeFile(newFilePath,text,"UTF-8");
            //TODO 上传到GridFS
            if (succ){
                newFileName = GridFSUtil.uploadFile("upload", newFilePath, newFileName);
            }else {
                return null;
            }
            return newFileName;
        } catch (Exception e) {
            System.out.println("修改Route的java文件操作出错");
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 生成processor的java文件
     * @param routeId       流程Code
     * @param tempFilePath      模板文件名
     * @param className     模板类名
     * @return
     */
    public static String genProcessorJavaFile(String routeId ,String tempFilePath, String className) {
        try {
            String newFileName = className+routeId+".java";
            String newFilePath = "/temp/"+className;
            String text = GridFSUtil.readFile("upload",tempFilePath);
            boolean succ = FileUtil.writeFile(newFilePath,text,"UTF-8");
            //TODO 上传到GridFS
            if (succ){
                newFileName = GridFSUtil.uploadFile("upload", newFilePath, newFileName);
                return newFileName;
            }
        } catch (Exception e) {
            System.out.println("生成processor的java文件操作出错");
            e.printStackTrace();
        }
        return null;
    }
}

+ 10 - 10
src/test/java/com/yihu/hos/services/ServiceFlowEventServiceTest.java

@ -17,27 +17,27 @@ public class ServiceFlowEventServiceTest {
    ServiceFlowEventService serviceFlowEventService;
    @Test
    public void processorAdded() throws Exception {
        serviceFlowEventService.processorAdded("hosServiceEventService", "services", "HosServiceEventService", "com/yihu/hos/services/HosServiceEventService.class");
    public void serviceFlowAdded() throws Exception {
        serviceFlowEventService.serviceFlowAdded(null);
    }
    @Test
    public void processorDataChanged() throws Exception {
        serviceFlowEventService.processorDataChanged("hosServiceEventService", "services", "HosServiceEventService", "com/yihu/hos/services/HosServiceEventService.class");
    public void serviceFlowModifiedAdd() throws Exception {
        serviceFlowEventService.serviceFlowModifiedAdd(null);
    }
    @Test
    public void routeDefineAdded() throws Exception {
        serviceFlowEventService.routeDefineAdded("hosServiceEventService", "services", "HosServiceEventService", "com/yihu/hos/services/HosServiceEventService.class");
    public void serviceFlowDelete() throws Exception {
        serviceFlowEventService.serviceFlowDelete(null);
    }
    @Test
    public void routeDefineChanged() throws Exception {
        serviceFlowEventService.routeDefineChanged("hosServiceEventService", "services", "HosServiceEventService", "com/yihu/hos/services/HosServiceEventService.class");
    public void serviceFlowModifiedReduce() throws Exception {
        serviceFlowEventService.serviceFlowModifiedReduce(null);
    }
    @Test
    public void routeDefineDelete() throws Exception {
        serviceFlowEventService.routeDefineDelete("hosServiceEventService", "services", "HosServiceEventService");
    public void serviceFlowStopped() throws Exception {
        serviceFlowEventService.serviceFlowStopped(null);
    }
}