Commit e0ee325b by guominglei

Merge remote-tracking branch 'origin/developer' into developer

parents 3494022d 80304b55
package com.byit.enums;
/**
* @author huangfu
*/
public enum LauncherEnum {
XML_PORT_SET_ERROR("配置文件端口出现错误!"),
XML_DATA_ERROR("配置文件解析失败,或配置不正确!"),
XML_AUTO_VALUE_ERROR("自动扫描配置只能为true或者false"),
;
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
LauncherEnum(String msg) {
this.msg = msg;
}
}
package com.byit.exptions;
/**
* @author huangfu
*/
public class LauncherException extends RuntimeException {
public LauncherException(String message) {
super(message);
}
}
package com.byit.launcher;
import com.byit.model.PluginConfigModel;
import com.byit.rpc.ServerRunThread;
import com.byit.utils.XmlParseUtil;
import lombok.extern.slf4j.Slf4j;
/**
* @program: byit-myth-job->JobRunLauncher
* @description: 运行job服务的启动器
* @author: huangfu
* @date: 2019/11/27 15:45
**/
@Slf4j
public class JobRunServerLauncher {
private PluginConfigModel pluginConfigModel;
/**
* 线程是否已经被启动的标志
*/
public static volatile boolean THREAD_RUN_MARK = false;
public JobRunServerLauncher(String filePath) throws InterruptedException {
pluginConfigModel = new PluginConfigModel();
XmlParseUtil.parse(filePath,pluginConfigModel);
runServer();
}
/**
* 开始启动服务 设置启动标志
*/
private void runServer() throws InterruptedException {
if(!THREAD_RUN_MARK){
new Thread(new ServerRunThread(pluginConfigModel)).start();
Thread.sleep(500);
log.info("---------------------线程启动------------------");
}
}
}
package com.byit.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* 数据配置类
* @author huangfu
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class PluginConfigModel {
private Integer port;
private List<String> classNames;
private Boolean authScan;
}
package com.byit.rpc;
import com.alibaba.fastjson.JSON;
import com.byit.dto.executor.AdminSenPluginDto;
import com.byit.dto.executor.DispatchResponseDto;
import com.byit.enums.JobResultEnum;
import com.byit.thread.RunJobThread;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @program: byit-myth-job->RunJobServer
* @description: 处理器最终处理类
* @author: huangfu
* @date: 2019/11/18 16:33
**/
@Slf4j
public class RunJobServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
/**
* LinkedBlockingQueue 不指定容量就变成了无界队列
*/
private static final ThreadPoolExecutor JOB_TRIGGER_POOL = new ThreadPoolExecutor(
50,
200,
60L,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1000),
r ->new Thread(r, "Netty RunJobServerHandler serverThread-" + r.hashCode()));
private static final String PENG = "PENG";
private static final String PONG = "PONG";
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
log.debug("----------------------有请求过来了------------------------");
DispatchResponseDto dispatchResponseDto = new DispatchResponseDto();
dispatchResponseDto.setCode(JobResultEnum.DISPATCH_FAIL.getCode());
dispatchResponseDto.setMsg(JobResultEnum.DISPATCH_FAIL.getMsg());
if(req != null){
//解析 调度中心 的数据对象
AdminSenPluginDto adminSenPluginDto = analysisParam(req.content( ));
if(null == adminSenPluginDto){
throw new Exception("核心参数为空!");
}
//检测是否有心跳参数,有心跳参数则为测试参数,且为PENG的话,服务端回复 PONG
String heartbeat = adminSenPluginDto.getHeartbeat();
if(null == heartbeat){
JOB_TRIGGER_POOL.execute(new RunJobThread(adminSenPluginDto));
dispatchResponseDto.setCode(JobResultEnum.DISPATCH_SUCCESS.getCode());
dispatchResponseDto.setMsg(JobResultEnum.DISPATCH_SUCCESS.getMsg());
}else if(PENG.equals(heartbeat)){
log.debug("----------调度平台心跳检测-------------");
dispatchResponseDto.setCode(JobResultEnum.DISPATCH_SUCCESS.getCode());
dispatchResponseDto.setMsg(JobResultEnum.DISPATCH_SUCCESS.getMsg());
dispatchResponseDto.setContent(PONG);
}
//----------------------------------消息发送-----------------------------------
ByteBuf byteBuf = Unpooled.copiedBuffer(JSON.toJSONString(dispatchResponseDto), CharsetUtil.UTF_8);
//HTTP响应
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,byteBuf);
//设置头信息
response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain");
//响应给客户端
response.headers().set(HttpHeaderNames.CONTENT_LENGTH,byteBuf.readableBytes());
ctx.writeAndFlush(response);
}
ctx.close();
}
/**
* 格式化参数
* @param byteBuf 将缓冲流更改为字符串
* @return 调用的必要信息承载类
*/
private AdminSenPluginDto analysisParam(ByteBuf byteBuf){
return JSON.parseObject(byteBuf.toString(CharsetUtil.UTF_8),AdminSenPluginDto.class);
}
/**
* 异常捕获
* @param ctx 上线文对象
* @param cause 异常信息
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
package com.byit.rpc;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import java.util.HashMap;
import java.util.Map;
/**
* @program: byit-myth-job->ServerChannelInitializer
* @description: 处理器初始化
* @author: huangfu
* @date: 2019/11/18 16:39
**/
public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline( );
//将请求委托给真正的最终处理类 自定义
pipeline.addLast(new HttpServerCodec());
// http 消息聚合器
pipeline.addLast("httpAggregator",new HttpObjectAggregator(1024*1024));
pipeline.addLast(new RunJobServerHandler());
}
}
package com.byit.rpc;
import com.byit.launcher.JobRunServerLauncher;
import com.byit.model.PluginConfigModel;
import com.byit.scan.JarRunScanProject;
import com.byit.scan.LocalFolderRunScanProject;
import com.byit.scan.XmlRunPlugin;
import com.byit.scan.base.IScanProject;
import com.byit.utils.JobUtils;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;
/**
* @program: byit-myth-job->ServerRunThread
* @description: 服务运行线程
* @author: huangfu
* @date: 2019/11/18 16:41
**/
@Slf4j
public class ServerRunThread implements Runnable {
private Integer port;
public ServerRunThread(PluginConfigModel pluginConfigModel) {
//获取任务信息
IScanProject iScanProject;
//如果设置自动转给程序自身处理 如果设置手动就读取xml进行
if (pluginConfigModel.getAuthScan()) {
if(JobUtils.isJarRun()){
log.info("-------------------jar包运行--------------------");
iScanProject = new JarRunScanProject(JobUtils.filePath());
}else{
log.info("-------------------本地运行---------------------");
iScanProject = new LocalFolderRunScanProject();
}
}else{
log.info("-------------------本地配置文件运行--------------------");
iScanProject = new XmlRunPlugin(pluginConfigModel);
}
iScanProject.addJobCache();
JobRunServerLauncher.THREAD_RUN_MARK = true;
this.port = pluginConfigModel.getPort();
}
@Override
public void run() {
//启动服务引导 框架启动引导类 屏蔽网络通讯配置信息
ServerBootstrap serverBootstrap = new ServerBootstrap();
//创建线程池组
//boss接收服务请求 但是并不处理而是将请求委托给worker
EventLoopGroup boss = new NioEventLoopGroup();
//真正的处理类
EventLoopGroup worker = new NioEventLoopGroup();
//将委托类和执行类关联至启动类
serverBootstrap.group(boss,worker)
.channel(NioServerSocketChannel.class)
.childHandler(new ServerChannelInitializer());
try {
ChannelFuture sync = serverBootstrap.bind(port).sync();
log.info("----------------------port:{}--------------------",port);
sync.channel().closeFuture().sync();
}catch (Exception e){
e.printStackTrace();
}
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}
package com.byit.scan;
import com.byit.task.annotations.JobHandler;
import com.byit.scan.base.IScanProject;
import com.byit.utils.JobUtils;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
* @program: byit-myth-job->JarRunScanProject
* @description: 这个是jar包运行时的操作
* @author: huangfu
* @date: 2019/11/29 11:16
**/
@Slf4j
public class JarRunScanProject implements IScanProject {
private static final String DOT_CLASS = ".class";
private static final String DOT_MF = ".MF";
private static String rootPath;
public JarRunScanProject(String rootPath) {
JarRunScanProject.rootPath = rootPath;
}
public static void classScan() throws IOException {
JarFile jarFile = new JarFile(rootPath);
File file = new File(rootPath);
URL url = file.toURI( ).toURL();
//获取当前jar的类加载器
URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url});
Enumeration<JarEntry> files = jarFile.entries( );
while (files.hasMoreElements()){
process(files.nextElement(),urlClassLoader);
}
}
private static void process(JarEntry jarEntry,ClassLoader classLoader) {
String name = jarEntry.getName( );
if(!name.endsWith(DOT_MF)){
if (name.endsWith(DOT_CLASS)) {
String classNameEndWithClass = name.replaceAll("/", ".");
String className = classNameEndWithClass.substring(0,classNameEndWithClass.length()-DOT_CLASS.length());
if(className.contains("$")){
className = className.substring(0,className.lastIndexOf("$"));
}
try {
Class objectiveClass = classLoader.loadClass(className);
if(JOB_ROOT_CLASS.isAssignableFrom(objectiveClass)){
if(objectiveClass.isAnnotationPresent(JOB_HANDLER_CLASS)){
JobHandler annotation = (JobHandler)objectiveClass.getAnnotation(JOB_HANDLER_CLASS);
JobUtils.jobCache.put(annotation.value(),objectiveClass);
}
}
}catch (Throwable ignored){
}
}
}
}
@Override
public void addJobCache() {
try {
classScan();
} catch (IOException e) {
e.printStackTrace( );
}
}
}
package com.byit.scan;
import com.byit.task.annotations.JobHandler;
import com.byit.executor.handler.interfaces.IJobHandler;
import com.byit.scan.base.IScanProject;
import com.byit.utils.JobUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @program: byit-myth-job->LocalFolderRunScanProject
* @description: 项目在本地运行,不是以jar包部署运行
* @author: huangfu
* @date: 2019/11/29 10:42
**/
public class LocalFolderRunScanProject implements IScanProject {
/**
* 获取项目的根路径
* @return
*/
private static String getProjectRootPath(){
String thisClassPath = new File(LocalFolderRunScanProject.class.getResource("/").getPath()).getPath();
return thisClassPath;
}
/**
* 获取任务类型的CLASS对象
* 根据是否是{@link IJobHandler}判断
* @return
*/
private static List<Class> getClassObjective() {
List<String> classFilePath = getClassFilePath( );
String projectRootPath = getProjectRootPath( );
//获取全限定名
List<Class> collect = classFilePath.stream( )
.map(filePth -> {
try {
//获取类全限定名
String replace = filePth.replace(projectRootPath+File.separator, "")
.replace(File.separator, ".")
.replace(".class", "");
return Class.forName(replace);
} catch (ClassNotFoundException e) {
e.printStackTrace( );
}
return null;
}).collect(Collectors.toList( ));
/**
* 符合条件的类
*/
List<Class> meetTheCriteria = collect.stream( ).filter(objectiveClass ->{
if (JOB_ROOT_CLASS.isAssignableFrom(objectiveClass)) {
if(objectiveClass.isAnnotationPresent(JOB_HANDLER_CLASS)){
return true;
}
}
return false;
}).collect(Collectors.toList( ));
return meetTheCriteria;
}
/**
* 获取项目根路径下所有的类文件
* @return
*/
private static List<String> getClassFilePath(){
ArrayList<String> classFileList = new ArrayList<>(10);
scan(getProjectRootPath(), classFileList);
return classFileList;
}
/**
* 递归扫描
* @param rootPath
* @param list
*/
private static void scan(String rootPath,List<String> list){
File rootFile = new File(rootPath);
File[] files = rootFile.listFiles( );
for (File file : files) {
if(file.isFile() && file.getName().endsWith(".class")){
list.add(file.getPath());
}else if(file.isDirectory()){
scan(file.getAbsolutePath(),list);
}
}
}
@Override
public void addJobCache() {
getClassObjective().forEach(jobClass ->{
JobHandler jobHandler = (JobHandler)jobClass.getAnnotation(JobHandler.class);
JobUtils.jobCache.put(jobHandler.value(),jobClass);
});
}
}
/*
package com.byit.scan;
import com.byit.task.annotations.JobHandler;
import com.byit.executor.handler.interfaces.IJobHandler;
import com.byit.utils.JobUtils;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
*/
/**
* @program: byit-myth-job->ScanRootPackage
* @description: 扫描项目
* @author: huangfu
* @date: 2019/11/19 17:31
**//*
@Slf4j
public class ScanRootPackage {
public ScanRootPackage() {
addJobCache();
}
*/
/**
* 任务基类
*//*
private static final Class<IJobHandler> JOB_ROOT_CLASS;
*/
/**
* 任务注解类
*//*
private static final Class<JobHandler> JOB_HANDLER_CLASS;
static {
JOB_ROOT_CLASS = IJobHandler.class;
JOB_HANDLER_CLASS = JobHandler.class;
}
*/
/**
* 获取项目的根路径
* @return
*//*
private static String getProjectRootPath(){
//String path = System.getProperty("java.class.path");
*/
/*String path1 = ScanRootPackage.class.getProtectionDomain().getCodeSource().getLocation().getPath();
try {
path1 = URLDecoder.decode(path1, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace( );
}
System.out.println(path1 );*//*
String thisClassPath = new File(ScanRootPackage.class.getResource("/").getPath()).getPath();
return thisClassPath;
}
*/
/**
* 获取任务类型的CLASS对象
* 根据是否是{@link IJobHandler}判断
* @return
*//*
private static List<Class> getClassObjective() {
List<String> classFilePath = getClassFilePath( );
String projectRootPath = getProjectRootPath( );
//获取全限定名
List<Class> collect = classFilePath.stream( )
.map(filePth -> {
try {
//获取类全限定名
String replace = filePth.replace(projectRootPath+File.separator, "")
.replace(File.separator, ".")
.replace(".class", "");
return Class.forName(replace);
} catch (ClassNotFoundException e) {
e.printStackTrace( );
}
return null;
}).collect(Collectors.toList( ));
*/
/**
* 符合条件的类
*//*
List<Class> meetTheCriteria = collect.stream( ).filter(objectiveClass ->{
if (JOB_ROOT_CLASS.isAssignableFrom(objectiveClass)) {
if(objectiveClass.isAnnotationPresent(JOB_HANDLER_CLASS)){
return true;
}
}
return false;
}).collect(Collectors.toList( ));
return meetTheCriteria;
}
*/
/**
* 向缓存添加任务信息
*//*
private static void addJobCache(){
getClassObjective().forEach(jobClass ->{
JobHandler jobHandler = (JobHandler)jobClass.getAnnotation(JobHandler.class);
JobUtils.jobCache.put(jobHandler.value(),jobClass);
});
}
*/
/**
* 获取项目根路径下所有的类文件
* @return
*//*
private static List<String> getClassFilePath(){
ArrayList<String> classFileList = new ArrayList<>(10);
scan(getProjectRootPath(), classFileList);
return classFileList;
}
*/
/**
* 递归扫描
* @param rootPath
* @param list
*//*
private static void scan(String rootPath,List<String> list){
File rootFile = new File(rootPath);
File[] files = rootFile.listFiles( );
for (File file : files) {
if(file.isFile() && file.getName().endsWith(".class")){
list.add(file.getPath());
}else if(file.isDirectory()){
scan(file.getAbsolutePath(),list);
}
}
}
}
*/
package com.byit.scan;
import com.byit.task.annotations.JobHandler;
import com.byit.executor.handler.interfaces.IJobHandler;
import com.byit.model.PluginConfigModel;
import com.byit.scan.base.IScanProject;
import com.byit.utils.JobUtils;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
/**
* @author huangfu
*/
@Slf4j
public class XmlRunPlugin implements IScanProject {
private PluginConfigModel pluginConfigModel;
public XmlRunPlugin(PluginConfigModel pluginConfigModel) {
this.pluginConfigModel = pluginConfigModel;
}
@Override
public void addJobCache() {
List<IJobHandler> iJobHandlers = instantiateJobHandler();
/**
* 添加到缓存
*/
iJobHandlers.forEach(iJobHandler -> {
Class<? extends IJobHandler> aClass = iJobHandler.getClass();
if (aClass.isAnnotationPresent(JOB_HANDLER_CLASS)) {
JobHandler annotation = aClass.getAnnotation(JOB_HANDLER_CLASS);
JobUtils.jobCache.put(annotation.value(),aClass);
}else{
log.warn("------{}被忽略,因为没有添加注解{}-------",iJobHandler,JOB_HANDLER_CLASS);
}
});
}
/**
* 实例化数据
* @return
*/
private List<IJobHandler> instantiateJobHandler(){
List<String> classNames = pluginConfigModel.getClassNames();
List<IJobHandler> iJobHandlers = new ArrayList<>(15);
classNames.forEach(className ->{
try {
Class<?> aClass = Class.forName(className);
Object o = aClass.newInstance();
if(o instanceof IJobHandler){
iJobHandlers.add((IJobHandler)o);
}else{
log.warn("---------{}被忽略,因为他不属于{}类型-------",o,JOB_ROOT_CLASS);
}
} catch (Exception e) {
e.printStackTrace();
}
});
return iJobHandlers;
}
}
package com.byit.scan.base;
import com.byit.task.annotations.JobHandler;
import com.byit.executor.handler.interfaces.IJobHandler;
/**
* @program: byit-myth-job->IScanProject
* @description: 扫描项目的基类
* @author: huangfu
* @date: 2019/11/29 10:41
**/
public interface IScanProject {
/**
* 任务基类
*/
Class<IJobHandler> JOB_ROOT_CLASS = IJobHandler.class;
/**
* 任务注解类
*/
Class<JobHandler> JOB_HANDLER_CLASS = JobHandler.class;
/**
* 将符合条件的任务添加到缓存层
*/
void addJobCache();
}
package com.byit.task.annotations;
import java.lang.annotation.*;
/**
* 主要应用在本地java任务上
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface JobHandler {
/**
* 任务名称
* @return 任务名称
*/
String value();
}
package com.byit.thread;
import com.alibaba.fastjson.JSON;
import com.byit.dto.executor.AdminSenPluginDto;
import com.byit.dto.executor.JobRunResultDto;
import com.byit.dto.web.ReturnResult;
import com.byit.enums.JobResultEnum;
import com.byit.executor.handler.interfaces.IJobHandler;
import com.byit.utils.JobUtils;
import com.byit.utils.PluginLogUtils;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
/**
* @program: byit-myth-job->RunJobThread
* @description: 任务线程
* @author: huangfu
* @date: 2019/12/16 11:40
**/
@Slf4j
public class RunJobThread implements Runnable {
private AdminSenPluginDto adminSenPluginDto;
public RunJobThread(AdminSenPluginDto adminSenPluginDto) {
this.adminSenPluginDto = adminSenPluginDto;
}
@Override
public void run() {
//创建回复对象
JobRunResultDto jobRunResultDto = new JobRunResultDto();
jobRunResultDto.setStartTime(new Date());
//设定运行标识
jobRunResultDto.setJobRunId(adminSenPluginDto.getRunId());
//运行任务
ReturnResult<String> stringReturnResult = runJob(adminSenPluginDto.getJobHandelName(), adminSenPluginDto.getJobParam());
//设置运行结果
jobRunResultDto.setReturnResult(stringReturnResult);
//获取回调通知URL
String callbackUrl = adminSenPluginDto.getCallbackUrl();
//设置结束时间
jobRunResultDto.setEndTime(new Date());
jobRunResultDto.setLogId(adminSenPluginDto.getLogId());
cn.hutool.http.HttpUtil.post(callbackUrl, JSON.toJSONString(jobRunResultDto));
log.info("---------服务器端:{},花费时间:{}-------------", jobRunResultDto,jobRunResultDto.getStartTime().getTime()-jobRunResultDto.getEndTime().getTime());
}
private ReturnResult<String> runJob(String jobHandlerName, String param){
Class<? extends IJobHandler> jobClass = JobUtils.jobCache.get(jobHandlerName);
try {
IJobHandler iJobHandler = jobClass.newInstance();
return iJobHandler.execute(param);
} catch (Exception e) {
e.printStackTrace( );
ReturnResult<String> returnResult = new ReturnResult<>();
returnResult.setMsg(PluginLogUtils.getMessage(e));
returnResult.setCode(JobResultEnum.FAIL.getCode());
return returnResult;
}
}
}
package com.byit.utils;
import com.byit.enums.LauncherEnum;
import com.byit.exptions.LauncherException;
import com.byit.model.PluginConfigModel;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
/**
* @author huangfu
*/
public class XmlParseUtil {
private final static String PORT = "port";
/**
* 将文档解析为文档类型的数据
* @param filePath xml文件地址
*/
public static void parse(String filePath,PluginConfigModel pluginConfigModel) {
URL resource = XmlParseUtil.class.getResource(filePath);
SAXReader reader = new SAXReader();
Document document;
try {
document = reader.read(resource);
} catch (DocumentException e) {
e.printStackTrace();
throw new LauncherException(LauncherEnum.XML_DATA_ERROR.getMsg());
}
Element rootElement = document.getRootElement();
if(rootElement == null){
throw new LauncherException(LauncherEnum.XML_DATA_ERROR.getMsg());
}
Element authScanElement = rootElement.element("auto-scan");
if(authScanElement == null){
pluginConfigModel.setAuthScan(false);
}else {
authScanParse(authScanElement,pluginConfigModel);
}
Element portElement = rootElement.element(PORT);
Element classNamesElement = rootElement.element("classNames");
if(portElement == null || classNamesElement == null){
throw new LauncherException(LauncherEnum.XML_DATA_ERROR.getMsg());
}
portParse(portElement,pluginConfigModel);
packageScanValueParse(classNamesElement,pluginConfigModel);
}
/**
* 自动扫描解析配置
* @param authScanElement 自动扫描
* @param pluginConfigModel 服务配置
*/
private static void authScanParse(Element authScanElement,PluginConfigModel pluginConfigModel){
String isScanStr = authScanElement.attributeValue("value");
try {
boolean isScan = Boolean.parseBoolean(isScanStr);
pluginConfigModel.setAuthScan(isScan);
}catch (Exception e){
e.printStackTrace();
throw new LauncherException(LauncherEnum.XML_PORT_SET_ERROR.getMsg());
}
}
/**
* 解析package
* @param packageRootElement 任务类
* @param pluginConfigModel 配置引用
*/
private static void packageScanValueParse(Element packageRootElement,PluginConfigModel pluginConfigModel){
Iterator<Element> elementIterator = packageRootElement.elementIterator();
while (elementIterator.hasNext()) {
Element packageElement = elementIterator.next();
String packageElementText = packageElement.getText();
if(pluginConfigModel.getClassNames() == null){
pluginConfigModel.setClassNames(new ArrayList<>(8));
}
pluginConfigModel.getClassNames().add(packageElementText);
}
}
/**
* 端口解析
* @param portElement 端口
* @param pluginConfigModel 配置引用
*/
private static void portParse(Element portElement,PluginConfigModel pluginConfigModel){
String portStr = portElement.getText();
try {
int port = Integer.parseInt(portStr.trim());
pluginConfigModel.setPort(port);
}catch (Exception e){
e.printStackTrace();
throw new LauncherException(LauncherEnum.XML_PORT_SET_ERROR.getMsg());
}
}
public static void main(String[] args) {
parse("/plugin.xml",new PluginConfigModel());
}
}
...@@ -12,7 +12,7 @@ import java.util.concurrent.TimeUnit; ...@@ -12,7 +12,7 @@ import java.util.concurrent.TimeUnit;
* @author: huangfu * @author: huangfu
* @date: 2019/11/20 12:40 * @date: 2019/11/20 12:40
**/ **/
@JobHandler("addJob")
public class DemoJob extends BaseJobHandler { public class DemoJob extends BaseJobHandler {
@Override @Override
public ReturnResult<String> execute(String s) throws Exception { public ReturnResult<String> execute(String s) throws Exception {
......
...@@ -10,7 +10,7 @@ import com.byit.executor.handler.BaseJobHandler; ...@@ -10,7 +10,7 @@ import com.byit.executor.handler.BaseJobHandler;
* @author: huangfu * @author: huangfu
* @date: 2019/11/20 12:40 * @date: 2019/11/20 12:40
**/ **/
@JobHandler("END")
public class EndNode extends BaseJobHandler { public class EndNode extends BaseJobHandler {
@Override @Override
public ReturnResult<String> execute(String s) throws Exception { public ReturnResult<String> execute(String s) throws Exception {
......
...@@ -4,7 +4,7 @@ import com.byit.task.annotations.JobHandler; ...@@ -4,7 +4,7 @@ import com.byit.task.annotations.JobHandler;
import com.byit.dto.web.ReturnResult; import com.byit.dto.web.ReturnResult;
import com.byit.executor.handler.BaseJobHandler; import com.byit.executor.handler.BaseJobHandler;
@JobHandler("start")
public class StartNode extends BaseJobHandler { public class StartNode extends BaseJobHandler {
@Override @Override
public ReturnResult<String> execute(String param) throws Exception { public ReturnResult<String> execute(String param) throws Exception {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment