diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/EnableJob.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/EnableJob.java new file mode 100644 index 00000000..db697d41 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/annotation/EnableJob.java @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * @author: 启用job + * @date: 2022/9/4 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface EnableJob { + boolean value() default true; +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/Environment.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/Environment.java index c603e15b..6dbd0ef5 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/Environment.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/Environment.java @@ -55,4 +55,8 @@ public class Environment { * 应用配置 */ private ApplicationConfig config; + /** + * 启用job + */ + private boolean enableJob; } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ExtensionServiceLoader.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ExtensionServiceLoader.java index 52652223..451c4513 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ExtensionServiceLoader.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ExtensionServiceLoader.java @@ -40,6 +40,8 @@ public class ExtensionServiceLoader implements ApplicationRunner { private ApplicationConfig applicationConfig; @Autowired private SimpleBeanFactory applicationBeanFactory; + @Autowired + private Environment environment; @Override public void run(String[] args) { diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/NeutrinoLauncher.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/NeutrinoLauncher.java index ecc772e6..67ae58a2 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/NeutrinoLauncher.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/NeutrinoLauncher.java @@ -23,6 +23,7 @@ package fun.asgc.neutrino.core.context; import com.google.common.collect.Lists; +import fun.asgc.neutrino.core.annotation.EnableJob; import fun.asgc.neutrino.core.annotation.NeutrinoApplication; import fun.asgc.neutrino.core.constant.MetaDataConstant; import fun.asgc.neutrino.core.util.*; @@ -88,6 +89,10 @@ public class NeutrinoLauncher { } } log.info("scanBasePackages: {}", environment.getScanBasePackages()); + EnableJob enableJob = environment.getMainClass().getAnnotation(EnableJob.class); + if (null != enableJob && enableJob.value()) { + environment.setEnableJob(Boolean.TRUE); + } // 加载应用配置 environment.setConfig(ConfigUtil.getYmlConfig(ApplicationConfig.class)); diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/DefaultJobSource.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/DefaultJobSource.java new file mode 100644 index 00000000..9fae3b45 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/DefaultJobSource.java @@ -0,0 +1,61 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.quartz; + +import com.google.common.collect.Lists; +import fun.asgc.neutrino.core.quartz.annotation.JobHandler; +import fun.asgc.neutrino.core.util.BeanManager; +import fun.asgc.neutrino.core.util.CollectionUtil; + +import java.util.List; + +/** + * + * @author: aoshiguchen + * @date: 2022/9/4 + */ +public class DefaultJobSource implements IJobSource { + + @Override + public List list() { + List jobHandlerList = BeanManager.getBeanListBySuperClass(IJobHandler.class); + if (CollectionUtil.isEmpty(jobHandlerList)) { + return Lists.newArrayList(); + } + List jobInfoList = Lists.newArrayList(); + for (IJobHandler jobHandler : jobHandlerList) { + JobHandler handler = jobHandler.getClass().getAnnotation(JobHandler.class); + if (null == handler) { + continue; + } + jobInfoList.add(new JobInfo() + .setId(handler.name()) + .setName(handler.name()) + .setDesc(handler.desc()) + .setCron(handler.cron()) + .setParam(handler.param()) + ); + } + return jobInfoList; + } + +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/IJobCallback.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/IJobCallback.java new file mode 100644 index 00000000..1c4a232f --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/IJobCallback.java @@ -0,0 +1,37 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.quartz; + +/** + * + * @author: aoshiguchen + * @date: 2022/9/4 + */ +public interface IJobCallback { + + /** + * 执行日志 + * @param jobInfo + * @param throwable + */ + void executeLog(JobInfo jobInfo, Throwable throwable); +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/IJobExecutor.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/IJobExecutor.java new file mode 100644 index 00000000..9b7cc1e6 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/IJobExecutor.java @@ -0,0 +1,48 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.quartz; + +/** + * + * @author: aoshiguchen + * @date: 2022/9/4 + */ +public interface IJobExecutor { + + /** + * 初始化 + * @throws JobException + */ + void init() throws JobException; + + /** + * 新增job + * @param jobInfo + */ + void add(JobInfo jobInfo); + + /** + * 删除job + * @param jobId + */ + void remove(String jobId); +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/IJobHandler.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/IJobHandler.java new file mode 100644 index 00000000..d0f62cae --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/IJobHandler.java @@ -0,0 +1,37 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.quartz; + +/** + * @author: aoshiguchen + * @date: 2022/9/4 + */ +public interface IJobHandler { + + /** + * job执行 + * @param param + * @throws Exception + */ + void execute(String param) throws Exception; + +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/IJobSource.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/IJobSource.java new file mode 100644 index 00000000..6fcd28b5 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/IJobSource.java @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.quartz; + +import java.util.List; + +/** + * + * @author: aoshiguchen + * @date: 2022/9/4 + */ +public interface IJobSource { + + /** + * 获取所有job列表 + * @return + */ + List list(); +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/JobBean.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/JobBean.java new file mode 100644 index 00000000..9f47cedd --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/JobBean.java @@ -0,0 +1,46 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.quartz; + +import fun.asgc.neutrino.core.util.BeanManager; +import lombok.extern.slf4j.Slf4j; +import org.quartz.Job; +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; + +/** + * + * @author: aoshiguchen + * @date: 2022/9/4 + */ +@Slf4j +public class JobBean implements Job { + + @Override + public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { + JobExecutor jobExecutor = BeanManager.getBean(JobExecutor.class); + if (null != jobExecutor) { + jobExecutor.execute(jobExecutionContext); + } + } + +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/JobException.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/JobException.java new file mode 100644 index 00000000..b61e12d4 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/JobException.java @@ -0,0 +1,31 @@ +/** + * Copyright (C) 2018-2022 Zeyi information technology (Shanghai) Co., Ltd. + *

+ * All right reserved. + *

+ * This software is the confidential and proprietary + * information of Zeyi Company of China. + * ("Confidential Information"). You shall not disclose + * such Confidential Information and shall use it only + * in accordance with the terms of the contract agreement + * you entered into with Zeyi inc. + */ +package fun.asgc.neutrino.core.quartz; + +import fun.asgc.neutrino.core.exception.InternalException; + +/** + * + * @author: wen.y + * @date: 2022/9/4 + */ +public class JobException extends InternalException { + + public JobException(String message) { + super(message); + } + + public JobException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/JobExecutor.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/JobExecutor.java new file mode 100644 index 00000000..9d5c05b4 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/JobExecutor.java @@ -0,0 +1,157 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.quartz; + +import fun.asgc.neutrino.core.annotation.Autowired; +import fun.asgc.neutrino.core.context.ApplicationRunner; +import fun.asgc.neutrino.core.context.Environment; +import fun.asgc.neutrino.core.quartz.annotation.JobHandler; +import fun.asgc.neutrino.core.util.BeanManager; +import fun.asgc.neutrino.core.util.CollectionUtil; +import fun.asgc.neutrino.core.util.StringUtil; +import lombok.extern.slf4j.Slf4j; +import org.quartz.*; +import org.quartz.impl.StdSchedulerFactory; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ThreadPoolExecutor; + +/** + * Job执行器 + * @author: aoshiguchen + * @date: 2022/9/4 + */ +@Slf4j +public class JobExecutor implements ApplicationRunner, IJobExecutor { + @Autowired + private Environment environment; + private IJobSource jobSource; + private ThreadPoolExecutor threadPoolExecutor; + private Map jobInfoMap = new ConcurrentHashMap<>(); + private SchedulerFactory schedulerFactory; + private Scheduler scheduler; + private Map jobHandlerMap = new ConcurrentHashMap<>(); + private IJobCallback jobCallback; + + @Override + public void run(String[] args) throws JobException { + if (!environment.isEnableJob() || null == jobSource || null == threadPoolExecutor) { + return; + } + + List jobHandlerList = BeanManager.getBeanListBySuperClass(IJobHandler.class); + if (!CollectionUtil.isEmpty(jobHandlerList)) { + for (IJobHandler item : jobHandlerList) { + JobHandler jobHandler = item.getClass().getAnnotation(JobHandler.class); + if (null == jobHandler) { + continue; + } + jobHandlerMap.put(jobHandler.name(), item); + } + } + + try { + this.schedulerFactory = new StdSchedulerFactory(); + this.scheduler = schedulerFactory.getScheduler(); + this.init(); + } catch (Exception e){ + throw new JobException("job初始化异常"); + } + } + + public void setJobSource(IJobSource jobSource) { + this.jobSource = jobSource; + } + + public void setThreadPoolExecutor(ThreadPoolExecutor threadPoolExecutor) { + this.threadPoolExecutor = threadPoolExecutor; + } + + public void setJobCallback(IJobCallback jobCallback) { + this.jobCallback = jobCallback; + } + + @Override + public void init() throws JobException { + List jobInfoList = jobSource.list(); + if (CollectionUtil.isEmpty(jobInfoList)) { + return; + } + + for (JobInfo jobInfo : jobInfoList) { + add(jobInfo); + } + } + + @Override + public synchronized void add(JobInfo jobInfo) throws JobException { + if (null == jobInfo || StringUtil.isEmpty(jobInfo.getId()) || StringUtil.isEmpty(jobInfo.getCron()) || jobInfoMap.containsKey(jobInfo.getId())) { + return; + } + jobInfoMap.put(jobInfo.getId(), jobInfo); + + TriggerKey triggerKey = TriggerKey.triggerKey(jobInfo.getId()); + JobKey jobKey = new JobKey(jobInfo.getId()); + + CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?").withMisfireHandlingInstructionDoNothing(); + CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(cronScheduleBuilder).build(); + JobDetail jobDetail = JobBuilder.newJob(JobBean.class).withIdentity(jobKey).build(); + + try { + scheduler.scheduleJob(jobDetail, cronTrigger); + scheduler.start(); + } catch (Exception e) { + throw new RuntimeException(String.format("新增job[id=%s]异常", jobInfo.getId())); + } + } + + @Override + public void remove(String jobId) { + jobInfoMap.remove(jobId); + } + + public void execute(JobExecutionContext context) throws JobExecutionException { + if (null == context || null == context.getTrigger()) { + return; + } + if (!jobInfoMap.containsKey(context.getTrigger().getKey().getName()) + || !jobHandlerMap.containsKey(context.getTrigger().getKey().getName())) { + return; + } + + threadPoolExecutor.submit(() -> { + JobInfo jobInfo = jobInfoMap.get(context.getTrigger().getKey().getName()); + IJobHandler jobHandler =jobHandlerMap.get(jobInfo.getName()); + + try { + jobHandler.execute(jobInfo.getParam()); + if (null != jobCallback) { + jobCallback.executeLog(jobInfo, null); + } + } catch (Throwable e) { + jobCallback.executeLog(jobInfo, e); + } + }); + } +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/JobInfo.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/JobInfo.java new file mode 100644 index 00000000..e5ace0d2 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/JobInfo.java @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.quartz; + +import lombok.Data; +import lombok.experimental.Accessors; + +import java.util.Map; + +/** + * + * @author: aoshiguchen + * @date: 2022/9/4 + */ +@Accessors(chain = true) +@Data +public class JobInfo { + private String id; + private String name; + private String desc; + private String cron; + private String param; + private Map extension; +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/annotation/JobHandler.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/annotation/JobHandler.java new file mode 100644 index 00000000..87f83ffc --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/quartz/annotation/JobHandler.java @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.quartz.annotation; + +import java.lang.annotation.*; + +/** + * + * @author: aoshiguchen + * @date: 2022/9/4 + */ +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface JobHandler { + String name(); + String desc() default ""; + String cron(); + String param() default ""; +} diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/scheduler/test1/Launcher.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/scheduler/test1/Launcher.java index ba21a3d7..bde03eb6 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/scheduler/test1/Launcher.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/scheduler/test1/Launcher.java @@ -1,14 +1,23 @@ /** - * Copyright (C) 2018-2022 Zeyi information technology (Shanghai) Co., Ltd. - *

- * All right reserved. - *

- * This software is the confidential and proprietary - * information of Zeyi Company of China. - * ("Confidential Information"). You shall not disclose - * such Confidential Information and shall use it only - * in accordance with the terms of the contract agreement - * you entered into with Zeyi inc. + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ package fun.asgc.neutrino.core.scheduler.test1; @@ -20,7 +29,7 @@ import org.quartz.impl.StdSchedulerFactory; /** * - * @author: wen.y + * @author: aoshiguchen * @date: 2022/8/31 */ @NeutrinoApplication diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/scheduler/test1/RemoteHttpJobBean.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/scheduler/test1/RemoteHttpJobBean.java index 0b0221f7..5d97c40f 100755 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/scheduler/test1/RemoteHttpJobBean.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/scheduler/test1/RemoteHttpJobBean.java @@ -1,3 +1,24 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package fun.asgc.neutrino.core.scheduler.test1; import fun.asgc.neutrino.core.bean.BeanWrapper; diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/scheduler/test2/JobCallback.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/scheduler/test2/JobCallback.java new file mode 100644 index 00000000..520ce91c --- /dev/null +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/scheduler/test2/JobCallback.java @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.scheduler.test2; + +import fun.asgc.neutrino.core.quartz.IJobCallback; +import fun.asgc.neutrino.core.quartz.JobInfo; +import lombok.extern.slf4j.Slf4j; + +/** + * + * @author: aoshiguchen + * @date: 2022/9/4 + */ +@Slf4j +public class JobCallback implements IJobCallback { + + @Override + public void executeLog(JobInfo jobInfo, Throwable throwable) { + if (null == throwable) { + log.info("job[id={},name={}]执行完毕", jobInfo.getId(), jobInfo.getName()); + } else { + log.error("job[id={},name={}]执行异常", jobInfo.getId(), jobInfo.getName(), throwable); + } + } +} diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/scheduler/test2/Launcher.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/scheduler/test2/Launcher.java new file mode 100644 index 00000000..4d5b80ac --- /dev/null +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/scheduler/test2/Launcher.java @@ -0,0 +1,58 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.scheduler.test2; + +import fun.asgc.neutrino.core.annotation.Bean; +import fun.asgc.neutrino.core.annotation.EnableJob; +import fun.asgc.neutrino.core.annotation.NeutrinoApplication; +import fun.asgc.neutrino.core.context.NeutrinoLauncher; +import fun.asgc.neutrino.core.quartz.DefaultJobSource; +import fun.asgc.neutrino.core.quartz.IJobExecutor; +import fun.asgc.neutrino.core.quartz.JobExecutor; + +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * + * @author: aoshiguchen + * @date: 2022/9/4 + */ +@EnableJob +@NeutrinoApplication +public class Launcher { + + public static void main(String[] args) { + NeutrinoLauncher.runSync(Launcher.class, args); + } + + @Bean + public JobExecutor jobExecutor() { + JobExecutor executor = new JobExecutor(); + executor.setJobSource(new DefaultJobSource()); + executor.setThreadPoolExecutor(new ThreadPoolExecutor(5, 20, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue<>())); + executor.setJobCallback(new JobCallback()); + return executor; + } + +} diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/scheduler/test2/TestJob1.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/scheduler/test2/TestJob1.java new file mode 100644 index 00000000..7ce228a0 --- /dev/null +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/scheduler/test2/TestJob1.java @@ -0,0 +1,46 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.scheduler.test2; + +import fun.asgc.neutrino.core.annotation.Component; +import fun.asgc.neutrino.core.annotation.NonIntercept; +import fun.asgc.neutrino.core.quartz.IJobHandler; +import fun.asgc.neutrino.core.quartz.annotation.JobHandler; +import lombok.extern.slf4j.Slf4j; + +/** + * + * @author: aoshiguchen + * @date: 2022/9/4 + */ +@Slf4j +@NonIntercept +@Component +@JobHandler(name = "TestJob1", cron = "0/5 * * * * ?", param = "123") +public class TestJob1 implements IJobHandler { + + @Override + public void execute(String param) throws Exception { + log.info("TestJob1 execute param:{}", param); + } + +}