From c9c2296d6091d941f2add41b2d14d97446226bc7 Mon Sep 17 00:00:00 2001
From: zhc077 <565291854>
Date: Wed, 12 Apr 2023 15:53:37 +0800
Subject: [PATCH] =?UTF-8?q?'=E8=B0=83=E5=BA=A6=E4=BF=AE=E6=94=B9'?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../controller/NewsWebMagicController.java | 29 +-
.../webmagic/service/NewsPageProcessor.java | 2 +-
.../cms/webmagic/task/NewsAsyncJob.java | 53 ++++
.../cms/webmagic/task/NewsWebMagicTask.java | 140 ++++-----
.../cms/webmagic/task/SampleJobTest.java | 22 ++
.../controller/QuartzJobController.java | 283 ++++++++++++++++++
.../modules/quartz/entity/QuartzJob.java | 84 ++++++
.../jeecg/modules/quartz/job/AsyncJob.java | 35 +++
.../jeecg/modules/quartz/job/SampleJob.java | 23 ++
.../modules/quartz/job/SampleParamJob.java | 32 ++
.../quartz/mapper/QuartzJobMapper.java | 25 ++
.../quartz/mapper/xml/QuartzJobMapper.xml | 9 +
.../quartz/service/IQuartzJobService.java | 67 +++++
.../service/impl/QuartzJobServiceImpl.java | 183 +++++++++++
14 files changed, 908 insertions(+), 79 deletions(-)
create mode 100644 jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/task/NewsAsyncJob.java
create mode 100644 jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/task/SampleJobTest.java
create mode 100644 jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/quartz/controller/QuartzJobController.java
create mode 100644 jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/quartz/entity/QuartzJob.java
create mode 100644 jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/quartz/job/AsyncJob.java
create mode 100644 jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/quartz/job/SampleJob.java
create mode 100644 jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/quartz/job/SampleParamJob.java
create mode 100644 jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/quartz/mapper/QuartzJobMapper.java
create mode 100644 jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/quartz/mapper/xml/QuartzJobMapper.xml
create mode 100644 jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/quartz/service/IQuartzJobService.java
create mode 100644 jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/quartz/service/impl/QuartzJobServiceImpl.java
diff --git a/jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/controller/NewsWebMagicController.java b/jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/controller/NewsWebMagicController.java
index 60497e5..565a87b 100644
--- a/jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/controller/NewsWebMagicController.java
+++ b/jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/controller/NewsWebMagicController.java
@@ -29,13 +29,26 @@ public class NewsWebMagicController {
String page1 = "http://sie.huanghuai.edu.cn/index.php/item-list-category-13151.shtml";
String pageOther = "http://sie.huanghuai.edu.cn/index.php/item-list-category-13151-page-";
String url = "";
- for (int i = 60; i <= 65; i++) {
- if (i == 1) {
- url = page1;
- } else {
- url = pageOther + (i + 1) + ".html";
- }
- log.info("the page {} url:{}", i, url);
+ try {
+ Spider.create(pageProcessor)
+ .addUrl(page1)
+ // 抓取到的数据存数据库
+ .addPipeline(pipeline)
+ // 开启2个线程抓取
+ .thread(1)
+ // 异步启动爬虫
+ .start();
+ } catch (Exception ex) {
+ log.error("数据抓取异常:", ex.getMessage());
+ log.error("定时抓取数据线程执行异常", ex);
+ }
+// for (int i = 60; i <= 65; i++) {
+// if (i == 1) {
+// url = page1;
+// } else {
+// url = pageOther + (i + 1) + ".html";
+// }
+// log.info("the page {} url:{}", i, url);
// try {
// Spider.create(pageProcessor)
// .addUrl(url)
@@ -49,6 +62,6 @@ public class NewsWebMagicController {
// log.error("第[{}]页数据抓取异常:", i, ex.getMessage());
// log.error("定时抓取数据线程执行异常", ex);
// }
- }
+// }
}
}
diff --git a/jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/service/NewsPageProcessor.java b/jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/service/NewsPageProcessor.java
index 7b56a2c..c5b3af7 100644
--- a/jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/service/NewsPageProcessor.java
+++ b/jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/service/NewsPageProcessor.java
@@ -24,7 +24,7 @@ public class NewsPageProcessor implements PageProcessor {
@Override
public void process(Page page) {
-// log.info("page.url = " + page.getUrl());
+ log.info("the page.url ****** = " + page.getUrl());
// log.info("page= " + page.getHtml().toString());
/**
diff --git a/jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/task/NewsAsyncJob.java b/jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/task/NewsAsyncJob.java
new file mode 100644
index 0000000..d977409
--- /dev/null
+++ b/jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/task/NewsAsyncJob.java
@@ -0,0 +1,53 @@
+package org.jeecg.modules.cms.webmagic.task;
+
+import lombok.extern.slf4j.Slf4j;
+import org.jeecg.common.util.DateUtils;
+import org.jeecg.modules.cms.webmagic.service.NewsPageProcessor;
+import org.jeecg.modules.cms.webmagic.service.NewsPipeline;
+import org.quartz.*;
+import org.springframework.beans.factory.annotation.Autowired;
+import us.codecraft.webmagic.Spider;
+
+/**
+ * @Description: 同步定时任务
+ *
+ * 此处的同步是指 当定时任务的执行时间大于任务的时间间隔时
+ * 会等待第一个任务执行完成才会走第二个任务
+ * @author: taoyan
+ * @date: 2020年06月19日
+ */
+@PersistJobDataAfterExecution
+@DisallowConcurrentExecution
+@Slf4j
+public class NewsAsyncJob implements Job {
+
+ @Autowired
+ private NewsPipeline pipeline;
+
+ @Autowired
+ private NewsPageProcessor pageProcessor;
+
+ @Override
+ public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
+ log.info(" NewsAsyncJob Execution key:" + jobExecutionContext.getJobDetail().getKey());
+ log.info(String.format("NewsAsyncJob-开始时间:" + DateUtils.getTimestamp()));
+
+ String urlPage1 = "http://sie.huanghuai.edu.cn/index.php/item-list-category-13151.shtml";
+// String urlPageOther = "http://sie.huanghuai.edu.cn/index.php/item-list-category-13151-page-";
+// String url = "";
+ try {
+ Spider.create(pageProcessor)
+ .addUrl(urlPage1)
+ // 抓取到的数据存数据库
+ .addPipeline(pipeline)
+ // 开启2个线程抓取
+ .thread(1)
+ // 异步启动爬虫
+ .start();
+ } catch (Exception ex) {
+ log.error("数据抓取异常:", ex.getMessage());
+ log.error("定时抓取数据线程执行异常", ex);
+ }
+ }
+
+}
diff --git a/jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/task/NewsWebMagicTask.java b/jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/task/NewsWebMagicTask.java
index e5e16ae..95a236a 100644
--- a/jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/task/NewsWebMagicTask.java
+++ b/jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/task/NewsWebMagicTask.java
@@ -1,71 +1,71 @@
-package org.jeecg.modules.cms.webmagic.task;
-
-import lombok.extern.slf4j.Slf4j;
-import org.jeecg.modules.cms.webmagic.service.NewsPageProcessor;
-import org.jeecg.modules.cms.webmagic.service.NewsPipeline;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.scheduling.annotation.Scheduled;
-import org.springframework.stereotype.Component;
-import us.codecraft.webmagic.Spider;
-
-@Slf4j
-@Component
-public class NewsWebMagicTask {
-
- @Autowired
- private NewsPipeline pipeline;
-
- @Autowired
- private NewsPageProcessor pageProcessor;
-
- // 每天23点执行一次
- @Scheduled(cron = "0 0 23 * * ?")
- //@Scheduled(cron = "0 40 15 * * ?")
- // 每隔1分钟执行一次
-// @Scheduled(cron = "0 */3 * * * ?")
- public void crawl() {
- //第1页 http://sie.huanghuai.edu.cn/index.php/item-list-category-13151.shtml
- //第2页 http://sie.huanghuai.edu.cn/index.php/item-list-category-13151-page-2.html
- //第3页 http://sie.huanghuai.edu.cn/index.php/item-list-category-13151-page-3.html
- // ......
- //第65页 http://sie.huanghuai.edu.cn/index.php/item-list-category-13151-page-65.html
-
- String urlPage1 = "http://sie.huanghuai.edu.cn/index.php/item-list-category-13151.shtml";
- String urlPageOther = "http://sie.huanghuai.edu.cn/index.php/item-list-category-13151-page-";
- String url = "";
- try {
- Spider.create(pageProcessor)
- .addUrl(urlPage1)
- // 抓取到的数据存数据库
- .addPipeline(pipeline)
- // 开启2个线程抓取
- .thread(3)
- // 异步启动爬虫
- .start();
- } catch (Exception ex) {
- log.error("数据抓取异常:", ex.getMessage());
- log.error("定时抓取数据线程执行异常", ex);
- }
-// for (int i = 1; i <= 9; i++) {
-// if (i == 1) {
-// url = urlPage1;
-// } else {
-// url = urlPageOther + (i + 1) + ".html";
-// }
-// log.info("the page {} url:{}", i, url);
-// try {
-// Spider.create(pageProcessor)
-// .addUrl(url)
-// // 抓取到的数据存数据库
-// .addPipeline(pipeline)
-// // 开启2个线程抓取
-// .thread(3)
-// // 异步启动爬虫
-// .start();
-// } catch (Exception ex) {
-// log.error("第[{}]页数据抓取异常:", i, ex.getMessage());
-// log.error("定时抓取数据线程执行异常", ex);
-// }
+//package org.jeecg.modules.cms.webmagic.task;
+//
+//import lombok.extern.slf4j.Slf4j;
+//import org.jeecg.modules.cms.webmagic.service.NewsPageProcessor;
+//import org.jeecg.modules.cms.webmagic.service.NewsPipeline;
+//import org.springframework.beans.factory.annotation.Autowired;
+//import org.springframework.scheduling.annotation.Scheduled;
+//import org.springframework.stereotype.Component;
+//import us.codecraft.webmagic.Spider;
+//
+//@Slf4j
+//@Component
+//public class NewsWebMagicTask {
+//
+// @Autowired
+// private NewsPipeline pipeline;
+//
+// @Autowired
+// private NewsPageProcessor pageProcessor;
+//
+// // 每天23点执行一次
+//// @Scheduled(cron = "0 0 23 * * ?")
+// //@Scheduled(cron = "0 40 15 * * ?")
+// // 每隔1分钟执行一次
+//// @Scheduled(cron = "0 */3 * * * ?")
+// public void crawl() {
+// //第1页 http://sie.huanghuai.edu.cn/index.php/item-list-category-13151.shtml
+// //第2页 http://sie.huanghuai.edu.cn/index.php/item-list-category-13151-page-2.html
+// //第3页 http://sie.huanghuai.edu.cn/index.php/item-list-category-13151-page-3.html
+// // ......
+// //第65页 http://sie.huanghuai.edu.cn/index.php/item-list-category-13151-page-65.html
+//
+// String urlPage1 = "http://sie.huanghuai.edu.cn/index.php/item-list-category-13151.shtml";
+// String urlPageOther = "http://sie.huanghuai.edu.cn/index.php/item-list-category-13151-page-";
+// String url = "";
+// try {
+// Spider.create(pageProcessor)
+// .addUrl(urlPage1)
+// // 抓取到的数据存数据库
+// .addPipeline(pipeline)
+// // 开启2个线程抓取
+// .thread(1)
+// // 异步启动爬虫
+// .start();
+// } catch (Exception ex) {
+// log.error("数据抓取异常:", ex.getMessage());
+// log.error("定时抓取数据线程执行异常", ex);
// }
- }
-}
+//// for (int i = 1; i <= 9; i++) {
+//// if (i == 1) {
+//// url = urlPage1;
+//// } else {
+//// url = urlPageOther + (i + 1) + ".html";
+//// }
+//// log.info("the page {} url:{}", i, url);
+//// try {
+//// Spider.create(pageProcessor)
+//// .addUrl(url)
+//// // 抓取到的数据存数据库
+//// .addPipeline(pipeline)
+//// // 开启2个线程抓取
+//// .thread(3)
+//// // 异步启动爬虫
+//// .start();
+//// } catch (Exception ex) {
+//// log.error("第[{}]页数据抓取异常:", i, ex.getMessage());
+//// log.error("定时抓取数据线程执行异常", ex);
+//// }
+//// }
+// }
+//}
diff --git a/jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/task/SampleJobTest.java b/jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/task/SampleJobTest.java
new file mode 100644
index 0000000..0dad758
--- /dev/null
+++ b/jeecg-boot/jeecg-boot-module-cms/src/main/java/org/jeecg/modules/cms/webmagic/task/SampleJobTest.java
@@ -0,0 +1,22 @@
+package org.jeecg.modules.cms.webmagic.task;
+
+import lombok.extern.slf4j.Slf4j;
+import org.jeecg.common.util.DateUtils;
+import org.quartz.Job;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+
+/**
+ * 示例不带参定时任务
+ *
+ * @Author Scott
+ */
+@Slf4j
+public class SampleJobTest implements Job {
+
+ @Override
+ public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
+ log.info(" >>>>>>>>SampleJobTest Execution key:"+jobExecutionContext.getJobDetail().getKey());
+ log.info(String.format(" 普通定时任务 SampleJobTest,时间:" + DateUtils.getTimestamp()));
+ }
+}
diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/quartz/controller/QuartzJobController.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/quartz/controller/QuartzJobController.java
new file mode 100644
index 0000000..34b2bcb
--- /dev/null
+++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/quartz/controller/QuartzJobController.java
@@ -0,0 +1,283 @@
+package org.jeecg.modules.quartz.controller;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.shiro.SecurityUtils;
+import org.jeecg.common.api.vo.Result;
+import org.jeecg.common.constant.CommonConstant;
+import org.jeecg.common.system.query.QueryGenerator;
+import org.jeecg.common.system.vo.LoginUser;
+import org.jeecg.common.util.ImportExcelUtil;
+import org.jeecg.modules.quartz.entity.QuartzJob;
+import org.jeecg.modules.quartz.service.IQuartzJobService;
+import org.jeecgframework.poi.excel.ExcelImportUtil;
+import org.jeecgframework.poi.excel.def.NormalExcelConstants;
+import org.jeecgframework.poi.excel.entity.ExportParams;
+import org.jeecgframework.poi.excel.entity.ImportParams;
+import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
+import org.quartz.Scheduler;
+import org.quartz.SchedulerException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.multipart.MultipartHttpServletRequest;
+import org.springframework.web.servlet.ModelAndView;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @Description: 定时任务在线管理
+ * @Author: jeecg-boot
+ * @Date: 2019-01-02
+ * @Version:V1.0
+ */
+@RestController
+@RequestMapping("/sys/quartzJob")
+@Slf4j
+@Api(tags = "定时任务接口")
+public class QuartzJobController {
+ @Autowired
+ private IQuartzJobService quartzJobService;
+ @Autowired
+ private Scheduler scheduler;
+
+ /**
+ * 分页列表查询
+ *
+ * @param quartzJob
+ * @param pageNo
+ * @param pageSize
+ * @param req
+ * @return
+ */
+ @RequestMapping(value = "/list", method = RequestMethod.GET)
+ public Result> queryPageList(QuartzJob quartzJob, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
+ @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
+ QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(quartzJob, req.getParameterMap());
+ Page page = new Page(pageNo, pageSize);
+ IPage pageList = quartzJobService.page(page, queryWrapper);
+ return Result.ok(pageList);
+
+ }
+
+ /**
+ * 添加定时任务
+ *
+ * @param quartzJob
+ * @return
+ */
+ //@RequiresRoles("admin")
+ @RequestMapping(value = "/add", method = RequestMethod.POST)
+ public Result> add(@RequestBody QuartzJob quartzJob) {
+ quartzJobService.saveAndScheduleJob(quartzJob);
+ return Result.ok("创建定时任务成功");
+ }
+
+ /**
+ * 更新定时任务
+ *
+ * @param quartzJob
+ * @return
+ */
+ //@RequiresRoles("admin")
+ @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
+ public Result> eidt(@RequestBody QuartzJob quartzJob) {
+ try {
+ quartzJobService.editAndScheduleJob(quartzJob);
+ } catch (SchedulerException e) {
+ log.error(e.getMessage(), e);
+ return Result.error("更新定时任务失败!");
+ }
+ return Result.ok("更新定时任务成功!");
+ }
+
+ /**
+ * 通过id删除
+ *
+ * @param id
+ * @return
+ */
+ //@RequiresRoles("admin")
+ @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
+ public Result> delete(@RequestParam(name = "id", required = true) String id) {
+ QuartzJob quartzJob = quartzJobService.getById(id);
+ if (quartzJob == null) {
+ return Result.error("未找到对应实体");
+ }
+ quartzJobService.deleteAndStopJob(quartzJob);
+ return Result.ok("删除成功!");
+
+ }
+
+ /**
+ * 批量删除
+ *
+ * @param ids
+ * @return
+ */
+ //@RequiresRoles("admin")
+ @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
+ public Result> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
+ if (ids == null || "".equals(ids.trim())) {
+ return Result.error("参数不识别!");
+ }
+ for (String id : Arrays.asList(ids.split(","))) {
+ QuartzJob job = quartzJobService.getById(id);
+ quartzJobService.deleteAndStopJob(job);
+ }
+ return Result.ok("删除定时任务成功!");
+ }
+
+ /**
+ * 暂停定时任务
+ *
+ * @param id
+ * @return
+ */
+ //@RequiresRoles("admin")
+ @GetMapping(value = "/pause")
+ @ApiOperation(value = "停止定时任务")
+ public Result