فهرست منبع

暂时提交 任务配置代码

demon 8 سال پیش
والد
کامیت
1b30b259f4

+ 137 - 0
hos-broker/src/main/java/com/yihu/hos/common/compiler/CamelCompiler.java

@ -0,0 +1,137 @@
package com.yihu.hos.common.compiler;
import com.yihu.hos.core.file.FileUtil;
import javax.tools.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
/**
 *  java编译工具类
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/11/17.
 */
public class CamelCompiler {
    private static String packagePathTemplate = System.getProperty("user.dir")//获取到项目的根路径
            + "/hos-broker/src/main/java/";
    private static String classPathTemplate = System.getProperty("user.dir")//获取到项目的根路径
            + "/hos-broker/src/main/java/%s/%s.java";
    /**
     * 编译java文件
     * @param packageName     java包路径
     * @param oldClassName   旧java文件名
     * @param newClassName   新java文件名
     * @param newCron         新cron表达式
     * @throws IOException
     */
    public static void compiler(String packageName,String oldClassName,String newClassName,String newCron) throws IOException {
        String classPath = CamelCompiler.class.getProtectionDomain().getCodeSource().getLocation().getPath() ;
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        // 建立DiagnosticCollector对象
        DiagnosticCollector diagnostics = new DiagnosticCollector();
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
        // 建立源文件对象,每个文件被保存在一个从JavaFileObject继承的类中
        File file = genNewJava(packageName, oldClassName,newClassName, newCron);
        if (file!=null){
            Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(file.getAbsolutePath());
            // options命令行选项
            Iterable<String> options = Arrays.asList("-d",classPath);// 指定的路径一定要存在,javac不会自己创建文件夹
            JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
            // 编译源程序
            boolean success = task.call();
            if (!success){
                List diagnostics1 = diagnostics.getDiagnostics();
                for (int i=0;i<diagnostics1.size();i++){
                    System.out.println(diagnostics1.get(i).toString());
                }
            }
            fileManager.close();
            System.out.println((success) ? "编译成功" : "编译失败");
        }
    }
    /**
     *  修改cron表达式,生成新java文件
     * @param packageName   包名
     * @param oldClassName  旧类名
     * @param newClassName  新类名
     * @param newContent    新cron表达式
     */
    public static File genNewJava(String packageName, String oldClassName,String newClassName, String newContent) {
        try {
            String oldPath = String.format(classPathTemplate, packageName, oldClassName);
            String newPath = String.format(classPathTemplate, packageName, newClassName);
            String text = FileUtil.readFileText(new File(oldPath));
            if (text.contains("?cron=")){
                String oldStr = text.substring(text.indexOf("?cron=")+6);
                String cron = oldStr.substring(0,oldStr.indexOf("\""));
                text = text.replace(cron,newContent);
            }
            if (text.contains(oldClassName)){
                text = text.replace(oldClassName,newClassName);
            }
            File f = new File(newPath);
            FileWriter fw = new FileWriter(f);
            fw.write(text);
            fw.flush();
            fw.close();//这里只是产生一个JAVA文件,简单的IO操作
            return f;
        }
        catch (Exception e) {
            System.out.println("修改操作出错");
            e.printStackTrace();
        }
        return null;
    }
    public static void compiler2(String packageName,String className) throws IOException {
        String classPath = CamelCompiler.class.getProtectionDomain().getCodeSource().getLocation().getPath() ;
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        // 建立DiagnosticCollector对象
        DiagnosticCollector diagnostics = new DiagnosticCollector();
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
        // 建立源文件对象,每个文件被保存在一个从JavaFileObject继承的类中
        Iterable compilationUnits = fileManager.getJavaFileObjects(String.format(classPathTemplate, packageName, className));
        // options命令行选项
        // 指定的路径一定要存在,javac不会自己创建文件夹
        Iterable<String> options = Arrays.asList(  "-d", classPath);
        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
        // 编译源程序
        boolean success = task.call();
        if (!success){
            List diagnostics1 = diagnostics.getDiagnostics();
            for (int i=0;i<diagnostics1.size();i++){
                System.out.println(diagnostics1.get(i).toString());
            }
        }
        fileManager.close();
        System.out.println((success) ? "编译成功" : "编译失败");
    }
    public static void main(String[] args) {
        try {
            compiler("/crawler/route", "QuartzRoute","QuartzRoute001","xx000xx");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

+ 21 - 0
hos-broker/src/main/java/com/yihu/hos/controllers/ESBCamelController.java

@ -118,4 +118,25 @@ public class ESBCamelController {
            @RequestParam(value = "serviceFlow") String serviceFlow) {
        return esbCamelService.onRouteDefineStop(serviceFlow);
    }
    @RequestMapping(value = "/genCamelFile", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value = "生成新的camel文件", produces = "application/json", notes = "生成新的camel文件")
    public Result genCamelFile(
            @ApiParam(name = "routeId", value = "routeId", required = true)
            @RequestParam(value = "routeId") String routeId,
            @ApiParam(name = "pageName", value = "包名", required = true)
            @RequestParam(value = "pageName") String pageName,
            @ApiParam(name = "oldClassName", value = "旧类名", required = true)
            @RequestParam(value = "oldClassName") String oldClassName,
            @ApiParam(name = "newClassName", value = "新类名", required = true)
            @RequestParam(value = "newClassName") String newClassName,
            @ApiParam(name = "newCron", value = "新cron表达式", required = true)
            @RequestParam(value = "routeId") String newCron) {
        return esbCamelService.genNewClassfile(routeId, pageName, oldClassName, newClassName, newCron);
    }
}

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

@ -1,6 +1,7 @@
package com.yihu.hos.services;
import com.yihu.hos.common.classLoader.DynamicClassLoader;
import com.yihu.hos.common.compiler.CamelCompiler;
import com.yihu.hos.common.constants.BrokerConstant;
import com.yihu.hos.core.constants.CoreConstant;
import com.yihu.hos.core.datatype.ClassFileUtil;
@ -14,6 +15,7 @@ import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Map;
@ -231,4 +233,19 @@ public class ESBCamelService {
        // 完成
        logger.info("===================" + packageName + CoreConstant.DOT + className + ".class 删除过程结束");
    }
}
    /* **************************       修改任务cron生成新的camel文件 add by hzy   *********************************** */
    public Result genNewClassfile(String routeId, String packageName, String oldClassName, String newClassName,String newCron) {
        try {
            CamelCompiler.compiler(packageName,oldClassName,newClassName,newCron);
            return Result.success("生成新文件成功!");
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("生成新文件失败!");
        }
    }
    }

+ 137 - 0
hos-camel/src/main/java/compiler/JavaCompilerUtil.java

@ -0,0 +1,137 @@
package compiler;
import com.yihu.hos.core.file.FileUtil;
import javax.tools.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
/**
 *  java编译工具类
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/11/17.
 */
public class JavaCompilerUtil {
    private static String packagePathTemplate = System.getProperty("user.dir")//获取到项目的根路径
            + "/hos-camel/src/main/java/";
    private static String classPathTemplate = System.getProperty("user.dir")//获取到项目的根路径
            + "/hos-camel/src/main/java/%s/%s.java";
    /**
     * 编译java文件
     * @param packageName     java包路径
     * @param oldClassName   旧java文件名
     * @param newClassName   新java文件名
     * @param newCron         新cron表达式
     * @throws IOException
     */
    public static void compiler(String packageName,String oldClassName,String newClassName,String newCron) throws IOException {
        String classPath = JavaCompilerUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath() ;
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        // 建立DiagnosticCollector对象
        DiagnosticCollector diagnostics = new DiagnosticCollector();
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
        // 建立源文件对象,每个文件被保存在一个从JavaFileObject继承的类中
        File file = genNewJava(packageName, oldClassName,newClassName, newCron);
        if (file!=null){
            Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(file.getAbsolutePath());
            // options命令行选项
            Iterable<String> options = Arrays.asList("-d",classPath);// 指定的路径一定要存在,javac不会自己创建文件夹
            JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
            // 编译源程序
            boolean success = task.call();
            if (!success){
                List diagnostics1 = diagnostics.getDiagnostics();
                for (int i=0;i<diagnostics1.size();i++){
                    System.out.println(diagnostics1.get(i).toString());
                }
            }
            fileManager.close();
            System.out.println((success) ? "编译成功" : "编译失败");
        }
    }
    /**
     *  修改cron表达式,生成新java文件
     * @param packageName   包名
     * @param oldClassName  旧类名
     * @param newClassName  新类名
     * @param newContent    新cron表达式
     */
    public static File genNewJava(String packageName, String oldClassName,String newClassName, String newContent) {
        try {
            String oldPath = String.format(classPathTemplate, packageName, oldClassName);
            String newPath = String.format(classPathTemplate, packageName, newClassName);
            String text = FileUtil.readFileText(new File(oldPath));
            if (text.contains("?cron=")){
                String oldStr = text.substring(text.indexOf("?cron=")+6);
                String cron = oldStr.substring(0,oldStr.indexOf("\""));
                text = text.replace(cron,newContent);
            }
            if (text.contains(oldClassName)){
                text = text.replace(oldClassName,newClassName);
            }
            File f = new File(newPath);
            FileWriter fw = new FileWriter(f);
            fw.write(text);
            fw.flush();
            fw.close();//这里只是产生一个JAVA文件,简单的IO操作
            return f;
        }
        catch (Exception e) {
            System.out.println("修改操作出错");
            e.printStackTrace();
        }
        return null;
    }
    public static void compiler2(String packageName,String className) throws IOException {
        String classPath = JavaCompilerUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath() ;
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        // 建立DiagnosticCollector对象
        DiagnosticCollector diagnostics = new DiagnosticCollector();
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
        // 建立源文件对象,每个文件被保存在一个从JavaFileObject继承的类中
        Iterable compilationUnits = fileManager.getJavaFileObjects(String.format(classPathTemplate, packageName, className));
        // options命令行选项
        // 指定的路径一定要存在,javac不会自己创建文件夹
        Iterable<String> options = Arrays.asList(  "-d", classPath);
        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
        // 编译源程序
        boolean success = task.call();
        if (!success){
            List diagnostics1 = diagnostics.getDiagnostics();
            for (int i=0;i<diagnostics1.size();i++){
                System.out.println(diagnostics1.get(i).toString());
            }
        }
        fileManager.close();
        System.out.println((success) ? "编译成功" : "编译失败");
    }
    public static void main(String[] args) {
        try {
            compiler("/crawler/route", "QuartzRoute","QuartzRoute001","xx000xx");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

+ 25 - 0
hos-camel/src/main/java/crawler/route/QuartzRoute.java

@ -0,0 +1,25 @@
package crawler.route;
import crawler.processor.Processor0;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/11/17.
 */
public class QuartzRoute extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("quartz://myGroupName/myTimerName?cron=0/5+*+*+*+*+?")
                .process(new Processor0() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        System.out.println("I'm running every 5 sec...Change by XXXX===========");
                    }
                });
    }
}

+ 31 - 0
hos-core/src/main/java/com/yihu/hos/core/file/FileUtil.java

@ -284,4 +284,35 @@ public class FileUtil {
        return buffer;
    }
    /**
     * 读取文本文件内容
     * @param file  文件路径
     * @return
     */
    public static String readFileText(File file) {
        StringBuilder stringBuilder = new StringBuilder();
        InputStream in = null;
        BufferedReader br = null;
        try {
            in = new FileInputStream(file);
            br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                }
            }
        }
        return stringBuilder.toString();
    }
}

+ 9 - 1
src/main/java/com/yihu/hos/datacollect/model/RsJobConfig.java

@ -23,13 +23,21 @@ public class RsJobConfig implements java.io.Serializable {
	private Date repeatStartTime;
	private Date repeatEndTime;
	private Integer delayTime;
	private Integer flowId;
	// Constructors
	/** default constructor */
	public RsJobConfig() {
	}
	public Integer getFlowId() {
		return flowId;
	}
	public void setFlowId(Integer flowId) {
		this.flowId = flowId;
	}
	public String getJobContent() {
		return jobContent;
	}

+ 18 - 0
src/main/java/com/yihu/hos/datacollect/service/DatacollectManager.java

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yihu.hos.common.Services;
import com.yihu.hos.core.http.HttpClientKit;
import com.yihu.hos.datacollect.dao.intf.IDatacollectDao;
import com.yihu.hos.datacollect.model.*;
import com.yihu.hos.datacollect.service.intf.IDatacollectManager;
@ -25,6 +26,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -528,4 +530,20 @@ public class DatacollectManager implements IDatacollectManager {
            return;
        }
    }
    /**
     * TODO 调用broker接口生成camel相关文件
     * @param jobId
     * @param newCron
     * @throws Exception
     */
    private void genCamelFile(String jobId, String newCron) throws Exception {
        String url = "http://192.168.131.11:8080/esb/genCamelFile";
        Map<String,String> params = new HashMap<>();
        String result = HttpClientKit.post(url, params).getBody();
    }
}

+ 13 - 0
src/main/java/com/yihu/hos/system/controller/FlowController.java

@ -190,5 +190,18 @@ public class FlowController extends BaseController {
    }
    @RequestMapping(value = "/flowList", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public Result flowList(String type) {
        try {
            Result result = flowManage.getFlowList(type);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }
}

+ 9 - 0
src/main/java/com/yihu/hos/system/dao/FlowDao.java

@ -1,11 +1,13 @@
package com.yihu.hos.system.dao;
import com.yihu.hos.system.dao.intf.IFlowDao;
import com.yihu.hos.system.model.SystemServiceFlow;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.model.Result;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Map;
/**
@ -30,5 +32,12 @@ public class FlowDao  extends SQLGeneralDAO implements IFlowDao {
        return super.getDataGridResult(sb.toString(), Integer.valueOf(params.get("page").toString()), Integer.valueOf(params.get("rows").toString()));
    }
    @Override
    public List<SystemServiceFlow> getFlowList(String type) throws Exception {
        String sql = "select * from system_service_flow where valid = 1 and file_type= '"+type+"' order by create_date";
        List<SystemServiceFlow> list = super.queryListBySql(sql,SystemServiceFlow.class);
        return list;
    }
}

+ 4 - 0
src/main/java/com/yihu/hos/system/dao/intf/IFlowDao.java

@ -1,8 +1,10 @@
package com.yihu.hos.system.dao.intf;
import com.yihu.hos.system.model.SystemServiceFlow;
import com.yihu.hos.web.framework.dao.XSQLGeneralDAO;
import com.yihu.hos.web.framework.model.Result;
import java.util.List;
import java.util.Map;
/**
@ -13,4 +15,6 @@ import java.util.Map;
public interface IFlowDao extends XSQLGeneralDAO {
    Result getFlowList(Map<String, Object> params) throws Exception;
    List<SystemServiceFlow> getFlowList(String type) throws Exception;
}

+ 28 - 0
src/main/java/com/yihu/hos/system/service/FlowManager.java

@ -9,6 +9,8 @@ import com.yihu.hos.system.dao.intf.IFlowDao;
import com.yihu.hos.system.model.SystemServiceFlow;
import com.yihu.hos.system.model.SystemServiceFlowClass;
import com.yihu.hos.system.service.intf.IFlowManage;
import com.yihu.hos.web.framework.model.DictItem;
import com.yihu.hos.web.framework.model.DictionaryResult;
import com.yihu.hos.web.framework.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -17,6 +19,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@ -214,4 +217,29 @@ public class FlowManager implements IFlowManage {
        }
    }
    /**
     * 获取流程列表
     * @param type 流程的文件类型
     * @return
     * @throws Exception
     */
    @Override
    public  DictionaryResult getFlowList(String type) throws Exception {
        List<SystemServiceFlow> flowList = flowDao.getFlowList(type);
        DictionaryResult re = new DictionaryResult("FLOW_LIST");
        if(flowList!=null&&flowList.size()>0)
        {
            List<DictItem> dictList = new ArrayList<>();
            for(SystemServiceFlow item:flowList){
                DictItem dict = new DictItem();
                dict.setCode(item.getId().toString());
                dict.setValue(item.getName());
                dict.setExtend("");
                dictList.add(dict);
            }
            re.setDetailModelList(dictList);
        }
        return re;
    }
}

+ 1 - 0
src/main/java/com/yihu/hos/system/service/intf/IFlowManage.java

@ -15,6 +15,7 @@ import java.util.Map;
 */
public interface IFlowManage extends IBaseManager {
    Result getFlowList(Map<String, Object> params) throws Exception;
    Result getFlowList(String type) throws Exception;
    SystemServiceFlow getFlowById(Integer id) throws Exception;

+ 5 - 0
src/main/resources/resource/RsJobConfig.hbm.xml

@ -71,5 +71,10 @@
                <comment>延迟时间</comment>
            </column>
        </property>
        <property name="flowId" type="java.lang.Integer">
            <column name="flow_id">
                <comment>流程ID</comment>
            </column>
        </property>
    </class>
</hibernate-mapping>