# dynamic-schedule-demo **Repository Path**: wb04307201/dynamic-schedule-demo ## Basic Information - **Project Name**: dynamic-schedule-demo - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2023-12-07 - **Last Updated**: 2024-10-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 动态编码动态任务调度Demo 根据cron表达式动态添加任务调度,定时执行执行groovy脚本 ### 访问 http://localhost:8081/list ![img.png](img.png) ### 测试数据 ##### 测试一 memo: 快速构造用户web页面 cron: - methodName: run groovyContext: ```java package cn.wubo.dynamic.schedule; import cn.wubo.dynamic.schedule.core.SpringContextUtils; import cn.wubo.loader.util.LoaderUtils; import cn.wubo.sql.util.web.EntityWebService; public class TestUserWeb { public void run() { EntityWebService entityWebService = SpringContextUtils.getBean(EntityWebService.class); String javaSourceCode = """ package cn.wubo.dynamic.schedule; import cn.wubo.sql.util.annotations.*; import cn.wubo.sql.util.enums.ColumnType; import cn.wubo.sql.util.enums.EditType; import cn.wubo.sql.util.enums.StatementCondition; import lombok.Data; import java.util.Date; @Data @Table(value = "test_user", desc = "用户", ds = @Ds(url = "jdbc:h2:file:./data/demo;AUTO_SERVER=TRUE", username = "sa", passowrd = "")) public class User { @Key @Column(value = "id") private String id; @Condition(value = StatementCondition.LIKE) @Column(value = "user_name", desc = "用户名", type = ColumnType.VARCHAR, length = 20, edit = @Edit(notNull = true)) private String userName; @Column(value = "department", desc = "部门", view = @View(width = 300, translatable = true, items = {@Item(value = "1", label = "部门1"), @Item(value = "2", label = "部门2"), @Item(value = "3", label = "部门3")}), edit = @Edit(type = EditType.SELECT, items = {@Item(value = "1", label = "部门1"), @Item(value = "2", label = "部门2"), @Item(value = "3", label = "部门3")})) private String department; @Column(value = "birth", desc = "生日", type = ColumnType.DATE, edit = @Edit(type = EditType.DATE)) private Date birth; @Condition(value = StatementCondition.LIKE) @Column(value = "age", desc = "年龄", type = ColumnType.NUMBER, precision = 10, scale = 0, edit = @Edit(type = EditType.NUMBER)) private Integer age; @Condition(value = StatementCondition.LIKE) @Column(value = "amount", desc = "薪酬", type = ColumnType.NUMBER, precision = 10, scale = 2, edit = @Edit(type = EditType.NUMBER)) private Float amount; @Column(value = "status", desc = "在职", type = ColumnType.VARCHAR, length = 1, view = @View(translatable = true, items = {@Item(value = "Y", label = "在职"), @Item(value = "N", label = "离职")}), edit = @Edit(type = EditType.CHECKBOX, items = {@Item(value = "Y", label = "在职"), @Item(value = "N", label = "离职")})) private String status; } """; String fullClassName = "cn.wubo.dynamic.schedule.User"; LoaderUtils.compiler(javaSourceCode, fullClassName); Class clazz = LoaderUtils.load(fullClassName); entityWebService.build("user", clazz); } } ``` 执行之后访问页面 http://localhost:8081/entity/view/user ![img_1.png](img_1.png) ##### 测试二 memo: 每10秒钟向test_table表写入记录 cron: 0/10 * * * * ? methodName: run groovyContext: ```java package cn.wubo.dynamic.schedule; import cn.wubo.sql.util.ExecuteSqlUtils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.HashMap; import java.util.Map; public class TestWriteTable { public void run() { try (Connection conn = DriverManager.getConnection("jdbc:h2:file:./data/demo;AUTO_SERVER=TRUE", "sa", "")) { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Map params = new HashMap<>(); params.put(1, sdf.format(cal.getTime())); ExecuteSqlUtils.executeUpdate(conn, "insert into test_table (id,code,name) values ('1','1',?)", params); } catch (SQLException e) { throw new RuntimeException(e); } } } ``` ##### 测试三 memo: 每10秒钟向钉钉群发送消息 cron: 0/10 * * * * ? methodName: run groovyContext: ```java package cn.wubo.dynamic.schedule; import cn.wubo.dynamic.schedule.core.SpringContextUtils; import cn.wubo.message.core.DingtalkProperties; import cn.wubo.message.core.MessageService; import cn.wubo.message.core.MessageType; import cn.wubo.message.message.RequestContent; import cn.wubo.message.message.SubLine; public class TestSendDingtalkChatbot { public void run() { MessageService messageService = SpringContextUtils.getBean(MessageService.class); DingtalkProperties.CustomRobot customRobot = new DingtalkProperties.CustomRobot(); customRobot.setAccessToken("你的钉钉群自定义机器人的accesstoken"); customRobot.setSecret("你的钉钉群自定义机器人的secret"); customRobot.setAlias("dd-1"); messageService.add(customRobot); messageService.send( RequestContent.buildMarkdown() .addAlias("dd-1") //通过配置的别名指定消息通道 .addMessageType(MessageType.DingtalkCustomRobot) //通过消息通道类型指定消息通道 .addDingtalkCustomRobot("isAtAll", Boolean.TRUE) //根据消息通道类型添加参数 .title("测试群发") .addLine(SubLine.title("这是一行标题1", 1)) .addLine(SubLine.title("这是一行标题2", 2)) .addLine(SubLine.text("这是一行文本")) .addLine(SubLine.link("这是一行链接", "https://gitee.com/wb04307201/message-spring-boot-starter")) .addLine(SubLine.quote("这是一行引用")) .addLine(SubLine.bold("这是一行加粗")) ); } } ``` ##### 测试四 memo: 每日天下午5点检测明天是否开始节假日 cron: 0 0 17 * * ? methodName: run groovyContext: ```java package cn.wubo.dynamic.schedule; import cn.wubo.dynamic.schedule.core.SpringContextUtils; import cn.wubo.message.core.DingtalkProperties; import cn.wubo.message.core.MessageService; import cn.wubo.message.message.MarkdownContent; import cn.wubo.message.message.RequestContent; import cn.wubo.message.message.SubLine; import com.alibaba.fastjson2.JSONObject; import org.springframework.web.client.RestTemplate; import java.text.SimpleDateFormat; import java.util.Calendar; public class CheckHoliday { public void run() { MessageService messageService = SpringContextUtils.getBean(MessageService.class); DingtalkProperties.CustomRobot customRobot = new DingtalkProperties.CustomRobot(); customRobot.setAccessToken("你的钉钉群自定义机器人的accesstoken"); customRobot.setSecret("你的钉钉群自定义机器人的secret"); customRobot.setAlias("dd-2"); messageService.add(customRobot); Calendar cal = Calendar.getInstance(); String today = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()); cal.add(Calendar.DATE, 1); String tomorrow = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()); RestTemplate restTemplate = new RestTemplate(); JSONObject todayJO = restTemplate.getForObject(String.format("https://apis.tianapi.com/jiejiari/index?key=你的key&date=%s&type=0", today), JSONObject.class); JSONObject tomorrowJO = restTemplate.getForObject(String.format("https://apis.tianapi.com/jiejiari/index?key=你的key&date=%s&type=0", tomorrow), JSONObject.class); if (todayJO.getInteger("code") == 200 && tomorrowJO.getInteger("code") == 200) { //日期类型,为0表示工作日、为1节假日、为2双休日、3为调休日(上班) Integer todayDaycode = todayJO.getJSONObject("result").getJSONArray("list").getJSONObject(0).getInteger("daycode"); Integer tomorrowDaycode = tomorrowJO.getJSONObject("result").getJSONArray("list").getJSONObject(0).getInteger("daycode"); if ((todayDaycode == 0 || todayDaycode == 2 || todayDaycode == 3) && tomorrowDaycode == 1) { // 今天不是节假日,明天是节假日,发送祝福 JSONObject jo = tomorrowJO.getJSONObject("result").getJSONArray("list").getJSONObject(0); MarkdownContent mc = RequestContent.buildMarkdown() .addAlias("dd-2") .title("提醒") .addLine(SubLine.text("明天就是" + jo.getString("name"))) .addLine(SubLine.text(jo.getString("tip"))) .addLine(SubLine.text("小E祝大家节日快乐")); messageService.send(mc); } } } } ``` ##### 测试五 memo: 每天5点检查极端天气预警 cron: 0 0 17 * * ? methodName: run groovyContext: ```java package cn.wubo.dynamic.schedule; import cn.wubo.dynamic.schedule.core.SpringContextUtils; import cn.wubo.message.core.DingtalkProperties; import cn.wubo.message.core.MessageService; import cn.wubo.message.message.MarkdownContent; import cn.wubo.message.message.RequestContent; import cn.wubo.message.message.SubLine; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Objects; import java.util.zip.GZIPInputStream; public class WarnWeather { public void run() { MessageService messageService = SpringContextUtils.getBean(MessageService.class); DingtalkProperties.CustomRobot customRobot = new DingtalkProperties.CustomRobot(); customRobot.setAccessToken("你的钉钉群自定义机器人的accesstoken"); customRobot.setSecret("你的钉钉群自定义机器人的secret"); customRobot.setAlias("dd-3"); messageService.add(customRobot); RestTemplate restTemplate = new RestTemplate(); Calendar cal = Calendar.getInstance(); String today = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()); JSONObject todayJO = restTemplate.getForObject(String.format("https://apis.tianapi.com/jiejiari/index?key=你的key&date=%s&type=0", today), JSONObject.class); if (todayJO.getInteger("code") == 200) { //日期类型,为0表示工作日、为1节假日、为2双休日、3为调休日(上班) Integer todayDaycode = todayJO.getJSONObject("result").getJSONArray("list").getJSONObject(0).getInteger("daycode"); if (todayDaycode == 0 || todayDaycode == 3) { ResponseEntity response = restTemplate.getForEntity("https://devapi.qweather.com/v7/warning/now?location=101060101&lang=zh-hans&key=你的key", byte[].class); try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayInputStream in = new ByteArrayInputStream(Objects.requireNonNull(response.getBody())); GZIPInputStream gunzip = new GZIPInputStream(in);) { byte[] buffer = new byte[256]; int n; while ((n = gunzip.read(buffer)) >= 0) { out.write(buffer, 0, n); } JSONObject resultJo = JSONObject.parseObject(out.toString()); if ("200".equalsIgnoreCase(resultJo.getString("code")) && resultJo.containsKey("warning") && !resultJo.getJSONArray("warning").isEmpty()) { JSONArray ja = resultJo.getJSONArray("warning"); MarkdownContent mc = RequestContent.buildMarkdown().addAlias("dd-3").title("提醒"); for (int i = 0; i < ja.size(); i++) { JSONObject jo = ja.getJSONObject(i); mc.addLine(SubLine.bold(jo.getString("title"))); mc.addLine(SubLine.quote(jo.getString("text"))); } messageService.send(mc); } } catch (IOException e) { throw new RuntimeException(e); } } } } } ``` ##### 测试六 memo: 获取菱数云场景数据 cron: - methodName: run groovyContext: ```java package cn.wubo.dynamic.schedule; import cn.wubo.sql.util.ExecuteSqlUtils; import cn.wubo.sql.util.ModelSqlUtils; import cn.wubo.sql.util.annotations.Table; import com.alibaba.fastjson2.JSONObject; import org.springframework.web.client.RestTemplate; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class TestGetDiamSceneDataToTable { public void run() { RestTemplate restTemplate = new RestTemplate(); JSONObject jo = restTemplate.getForObject("https://www.diamdata.com/data-opera/system/scene/list?pageSize=100000&pageNum=1&commodityName=&productApplyScene=&productStage=&productType=&timeSort=desc&priceSort=&sceneTypeCode=&orderByColumn=orderNum&isAsc=ascending&sceneStatus=1", JSONObject.class); if (jo.getInteger("code") == 200) { try (Connection conn = DriverManager.getConnection("jdbc:h2:file:./data/demo;AUTO_SERVER=TRUE", "sa", "")) { if (Boolean.FALSE.equals(ExecuteSqlUtils.isTableExists(conn, "data_storage_diam_scene"))) ModelSqlUtils.createSql(new DataStorage()).createTable(conn); else ExecuteSqlUtils.executeUpdate(conn, "delete data_storage_diam_scene"); jo.getJSONArray("rows").forEach(o -> { JSONObject jo1 = JSONObject.from(o); DataStorage dataStorage = new DataStorage(); dataStorage.setSceneCode(jo1.getString("sceneCode")); dataStorage.setSceneName(jo1.getString("sceneName")); dataStorage.setSceneIntroduce(jo1.getString("sceneIntroduce")); dataStorage.setSceneTypeStr(jo1.getString("sceneTypeStr")); ModelSqlUtils.insertSql(dataStorage).executeUpdate(conn); jo1.getJSONArray("sceneCommodityList").forEach(o1 -> { DataStorage dataStorage1 = new DataStorage(); dataStorage1.setSceneCode(jo1.getString("sceneCode")); JSONObject jo2 = (JSONObject) o1; dataStorage1.setCommodityCode(jo2.getString("commodityCode")); dataStorage1.setCommodityName(jo2.getString("commodityName")); dataStorage1.setCommodityIntroduce(jo2.getString("commodityIntroduce")); ModelSqlUtils.insertSql(dataStorage1).executeUpdate(conn); }); }); } catch (SQLException e) { throw new RuntimeException(e); } } } @Table(value = "data_storage_diam_scene", desc = "数据存储") public static class DataStorage { private String sceneCode; private String sceneName; private String sceneIntroduce; private String sceneTypeStr; private String commodityCode; private String commodityName; private String commodityIntroduce; public String getSceneCode() { return sceneCode; } public void setSceneCode(String sceneCode) { this.sceneCode = sceneCode; } public String getSceneName() { return sceneName; } public void setSceneName(String sceneName) { this.sceneName = sceneName; } public String getSceneIntroduce() { return sceneIntroduce; } public void setSceneIntroduce(String sceneIntroduce) { this.sceneIntroduce = sceneIntroduce; } public String getSceneTypeStr() { return sceneTypeStr; } public void setSceneTypeStr(String sceneTypeStr) { this.sceneTypeStr = sceneTypeStr; } public String getCommodityCode() { return commodityCode; } public void setCommodityCode(String commodityCode) { this.commodityCode = commodityCode; } public String getCommodityName() { return commodityName; } public void setCommodityName(String commodityName) { this.commodityName = commodityName; } public String getCommodityIntroduce() { return commodityIntroduce; } public void setCommodityIntroduce(String commodityIntroduce) { this.commodityIntroduce = commodityIntroduce; } } } ``` ##### 测试七 memo: 获取菱数云商品数据 cron: - methodName: run groovyContext: ```java package cn.wubo.dynamic.schedule; import cn.wubo.sql.util.ExecuteSqlUtils; import cn.wubo.sql.util.ModelSqlUtils; import cn.wubo.sql.util.annotations.Table; import com.alibaba.fastjson2.JSONObject; import org.springframework.web.client.RestTemplate; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class TestGetDiamCommodityDataToTable { public void run() { RestTemplate restTemplate = new RestTemplate(); JSONObject jo = restTemplate.getForObject("https://www.diamdata.com/data-opera/center/commodity/hall/search/list?pageSize=100000&pageNum=1&commodityName=&productApplyScene=&productStage=&productType=&timeSort=desc&priceSort=", JSONObject.class); if (jo.getInteger("code") == 200) { try (Connection conn = DriverManager.getConnection("jdbc:h2:file:./data/demo;AUTO_SERVER=TRUE", "sa", "")) { if (Boolean.FALSE.equals(ExecuteSqlUtils.isTableExists(conn, "data_storage_diam_commodity"))) ModelSqlUtils.createSql(new DataStorage()).createTable(conn); else ExecuteSqlUtils.executeUpdate(conn, "delete data_storage_diam_commodity"); jo.getJSONArray("rows").forEach(o -> { JSONObject jo1 = JSONObject.from(o); DataStorage dataStorage = new DataStorage(); dataStorage.setCommodityCode(jo1.getString("commodityCode")); dataStorage.setCommodityName(jo1.getString("commodityName")); dataStorage.setCommodityIntroduce(jo1.getString("commodityIntroduce")); dataStorage.setExchangeName(jo1.getString("exchangeName")); dataStorage.setPeriodUnit(jo1.getString("periodUnit")); dataStorage.setPeriodUnitName(jo1.getString("periodUnitName")); dataStorage.setProductCode(jo1.getString("productCode")); dataStorage.setProductApplyScene(jo1.getString("productApplyScene")); dataStorage.setPeriodUnit(jo1.getString("periodUnit")); dataStorage.setProductLabel(jo1.getString("productLabel")); dataStorage.setProductName(jo1.getString("productName")); dataStorage.setProductScene(jo1.getString("productScene")); dataStorage.setProductType(jo1.getString("productType")); ModelSqlUtils.insertSql(dataStorage).executeUpdate(conn); }); } catch (SQLException e) { throw new RuntimeException(e); } } } @Table(value = "data_storage_diam_commodity", desc = "数据存储") public static class DataStorage { private String commodityCode; private String commodityName; private String commodityIntroduce; private String exchangeName; private String periodUnit; private String periodUnitName; private String productApplyScene; private String productCode; private String productLabel; private String productName; private String productScene; private String productType; public String getCommodityCode() { return commodityCode; } public void setCommodityCode(String commodityCode) { this.commodityCode = commodityCode; } public String getCommodityName() { return commodityName; } public void setCommodityName(String commodityName) { this.commodityName = commodityName; } public String getCommodityIntroduce() { return commodityIntroduce; } public void setCommodityIntroduce(String commodityIntroduce) { this.commodityIntroduce = commodityIntroduce; } public String getExchangeName() { return exchangeName; } public void setExchangeName(String exchangeName) { this.exchangeName = exchangeName; } public String getPeriodUnit() { return periodUnit; } public void setPeriodUnit(String periodUnit) { this.periodUnit = periodUnit; } public String getPeriodUnitName() { return periodUnitName; } public void setPeriodUnitName(String periodUnitName) { this.periodUnitName = periodUnitName; } public String getProductApplyScene() { return productApplyScene; } public void setProductApplyScene(String productApplyScene) { this.productApplyScene = productApplyScene; } public String getProductCode() { return productCode; } public void setProductCode(String productCode) { this.productCode = productCode; } public String getProductLabel() { return productLabel; } public void setProductLabel(String productLabel) { this.productLabel = productLabel; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getProductScene() { return productScene; } public void setProductScene(String productScene) { this.productScene = productScene; } public String getProductType() { return productType; } public void setProductType(String productType) { this.productType = productType; } } } ``` ##### 测试八 memo: 获取上海数交所数商及服务数据 cron: - methodName: run groovyContext: ```java package cn.wubo.dynamic.schedule; import cn.wubo.sql.util.ExecuteSqlUtils; import cn.wubo.sql.util.ModelSqlUtils; import cn.wubo.sql.util.annotations.Table; import com.alibaba.fastjson2.JSONObject; import org.springframework.web.client.RestTemplate; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class TestGetShanghaiCompanyHallDataToTable { public void run() { RestTemplate restTemplate = new RestTemplate(); JSONObject jo = restTemplate.getForObject("https://echosystem.chinadep.com/dsps/companyHall/?pageNum=1&pageSize=100000&queryWords=", JSONObject.class); if (jo.getInteger("code") == 200) { try (Connection conn = DriverManager.getConnection("jdbc:h2:file:./data/demo;AUTO_SERVER=TRUE", "sa", "")) { if (Boolean.FALSE.equals(ExecuteSqlUtils.isTableExists(conn, "data_storage_shanghai_company_hall"))) ModelSqlUtils.createSql(new DataStorage()).createTable(conn); else ExecuteSqlUtils.executeUpdate(conn, "delete data_storage_shanghai_company_hall"); jo.getJSONObject("data").getJSONArray("list").forEach(o -> { JSONObject jo1 = JSONObject.from(o); DataStorage dataStorage = new DataStorage(); dataStorage.setCompanyId(jo1.getString("companyId")); dataStorage.setCompanyName(jo1.getString("companyName")); dataStorage.setCompanyProfile(jo1.getString("companyProfile")); dataStorage.setServiceType1(jo1.getString("serviceType")); dataStorage.setServiceTrade(jo1.getString("serviceTrade")); ModelSqlUtils.insertSql(dataStorage).executeUpdate(conn); jo1.getJSONArray("serviceExtList").forEach(o1 -> { DataStorage dataStorage1 = new DataStorage(); dataStorage1.setCompanyId(jo1.getString("companyId")); JSONObject jo2 = (JSONObject) o1; dataStorage1.setServiceNo(jo2.getString("serviceNo")); dataStorage1.setServiceName(jo2.getString("serviceName")); dataStorage1.setServiceProfile(jo2.getString("serviceProfile")); dataStorage1.setServiceType2(jo2.getString("serviceType")); dataStorage1.setServiceInfo(jo2.getString("serviceInfo")); dataStorage1.setServicePriceType(jo2.getString("servicePriceType")); dataStorage1.setServicePriceFrom(jo2.getString("servicePriceFrom")); dataStorage1.setServicePriceTo(jo2.getString("servicePriceTo")); dataStorage1.setServicePrice(jo2.getString("servicePrice")); ModelSqlUtils.insertSql(dataStorage1).executeUpdate(conn); }); }); } catch (SQLException e) { throw new RuntimeException(e); } } } @Table(value = "data_storage_shanghai_company_hall", desc = "上海数商大厅数据") public static class DataStorage { private String companyId; private String companyName; private String companyProfile; private String serviceType1; private String serviceTrade; private String serviceNo; private String serviceName; private String serviceProfile; private String serviceType2; private String serviceInfo; private String servicePriceType; private String servicePriceFrom; private String servicePriceTo; private String servicePrice; public String getCompanyId() { return companyId; } public void setCompanyId(String companyId) { this.companyId = companyId; } public String getCompanyName() { return companyName; } public void setCompanyName(String companyName) { this.companyName = companyName; } public String getCompanyProfile() { return companyProfile; } public void setCompanyProfile(String companyProfile) { this.companyProfile = companyProfile; } public String getServiceType1() { return serviceType1; } public void setServiceType1(String serviceType1) { this.serviceType1 = serviceType1; } public String getServiceTrade() { return serviceTrade; } public void setServiceTrade(String serviceTrade) { this.serviceTrade = serviceTrade; } public String getServiceNo() { return serviceNo; } public void setServiceNo(String serviceNo) { this.serviceNo = serviceNo; } public String getServiceName() { return serviceName; } public void setServiceName(String serviceName) { this.serviceName = serviceName; } public String getServiceProfile() { return serviceProfile; } public void setServiceProfile(String serviceProfile) { this.serviceProfile = serviceProfile; } public String getServiceType2() { return serviceType2; } public void setServiceType2(String serviceType2) { this.serviceType2 = serviceType2; } public String getServiceInfo() { return serviceInfo; } public void setServiceInfo(String serviceInfo) { this.serviceInfo = serviceInfo; } public String getServicePriceType() { return servicePriceType; } public void setServicePriceType(String servicePriceType) { this.servicePriceType = servicePriceType; } public String getServicePriceFrom() { return servicePriceFrom; } public void setServicePriceFrom(String servicePriceFrom) { this.servicePriceFrom = servicePriceFrom; } public String getServicePriceTo() { return servicePriceTo; } public void setServicePriceTo(String servicePriceTo) { this.servicePriceTo = servicePriceTo; } public String getServicePrice() { return servicePrice; } public void setServicePrice(String servicePrice) { this.servicePrice = servicePrice; } } } ``` ##### 测试九 memo: 百度资讯搜索_数据要素 cron: 0 0 9,13 * * ? methodName: run groovyContext: ```java package cn.wubo.dynamic.schedule; import cn.wubo.dynamic.schedule.core.SpringContextUtils; import cn.wubo.message.core.DingtalkProperties; import cn.wubo.message.core.MessageService; import cn.wubo.message.message.MarkdownContent; import cn.wubo.message.message.RequestContent; import cn.wubo.message.message.SubLine; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; public class SendDataEleBaiduNews { public void run() { MessageService messageService = SpringContextUtils.getBean(MessageService.class); DingtalkProperties.CustomRobot customRobot = new DingtalkProperties.CustomRobot(); customRobot.setAccessToken("你的钉钉群自定义机器人的accesstoken"); customRobot.setSecret("你的钉钉群自定义机器人的secret"); customRobot.setAlias("dd-111"); messageService.add(customRobot); Calendar cal = Calendar.getInstance(); String today = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()); RestTemplate restTemplate = new RestTemplate(); JSONObject todayJO = restTemplate.getForObject(String.format("https://apis.tianapi.com/jiejiari/index?key=你的key&date=%s&type=0", today), JSONObject.class); if (todayJO.getInteger("code") == 200) { //日期类型,为0表示工作日、为1节假日、为2双休日、3为调休日(上班) Integer todayDaycode = todayJO.getJSONObject("result").getJSONArray("list").getJSONObject(0).getInteger("daycode"); if (todayDaycode == 0 || todayDaycode == 3) { try { Document doc = Jsoup.connect("https://www.baidu.com/s?tn=news&rtt=1&bsst=1&cl=2&wd=%E6%95%B0%E6%8D%AE%E8%A6%81%E7%B4%A0&medium=0").get(); Elements newsHeadlines = doc.select(".c-container"); MarkdownContent mc = RequestContent.buildMarkdown().addAlias("dd-1111") //通过配置的别名指定消息通道 .title(doc.title()).addLine(SubLine.title(doc.title())); for (Element headline : newsHeadlines) { Elements elements = headline.getElementsByClass("c-color-gray2 c-font-normal c-gap-right-xsmall"); mc.addLine(SubLine.link(headline.getElementsByTag("a").get(0).text(), headline.getElementsByTag("a").get(0).absUrl("href"))); if (!elements.isEmpty()) mc.addLine(SubLine.quote(elements.get(0).text())); mc.addLine(SubLine.text(headline.getElementsByClass("c-font-normal c-color-text").get(0).text())); } messageService.send(mc); } catch (IOException e) { throw new RuntimeException(e); } } } } } ```