如何创建一个定时管理的页面
实现的效果:
类似这样的一个定时管理,对程序的中定时计划可以动态的控制其运行还是关闭
第一步:创建数据表:
如上图所示,创建如此数据表即可,而且关键的是最后的两个字段还没有用到
第二步,页面:
就是上面的三个页面,一个添加,一个修改,一个列表页面
第三步:后台代码的实现:
package com.hangxin.time.web;import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ScheduledFuture;import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;import com.hangxin.commons.util.PageModel;
import com.hangxin.commons.web.BasicAction;
import com.hangxin.time.model.TaskInfo;
import com.hangxin.time.service.IKpTimeService;
import com.hangxin.xxdzfp.service.PzService;
import com.hangxin.xxdzfp.service.ZpBillService;
import com.hangxin.xxdzfp.util.StrUtil;@Controller
public class BillTimeAction extends BasicAction
{private Logger logger = null;@Resourceprotected ZpBillService zpBillServiceImpl;@Resourceprotected IKpTimeService kpService;@Resourceprotected PzService pzServiceImpl;@Autowiredprivate ThreadPoolTaskScheduler threadPoolTaskScheduler;private static HashMap<String, ScheduledFuture<?>> map = new HashMap<String, ScheduledFuture<?>>();private ScheduledFuture<?> future;@Beanpublic ThreadPoolTaskScheduler threadPoolTaskScheduler() {ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();return executor;}public BillTimeAction() {super();logger = Logger.getLogger(BillTimeAction.class);}/*** @author wdg* 定时管理页面的渲染*/@PostConstructpublic void init() {List<TaskInfo> list = kpService.getTaskInfoList();for (TaskInfo taskinfo : list) {if (taskinfo.getRunstatus() == 1 && taskinfo.getRestartflag() != 1&& map.get(taskinfo.getTaskclass()) == null) {try {String taskclass = taskinfo.getTaskclass();Class<?> clazz = Class.forName(taskclass);HashMap<String, Object> hmap = new HashMap<String, Object>();hmap.put(ZpBillService.class.getName(), zpBillServiceImpl);hmap.put(IKpTimeService.class.getName(), kpService);hmap.put(PzService.class.getName(), pzServiceImpl);Constructor<?> con = clazz.getConstructor(Map.class);future = threadPoolTaskScheduler.schedule((Runnable) con.newInstance(hmap),new CronTrigger(taskinfo.getCronstr()));map.put(taskclass, future);taskinfo.setRestartflag(1);//项目启动中启动成功taskinfo.setRestarttime(new Date());kpService.updateTaskInfo(taskinfo);}catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}@RequestMapping(value = "/BillTimeAction_init")public String BillTimeAction_Init(HttpServletRequest request, HttpServletResponse response, Model model) {// Map<String, Object> map = kpService.isUpdateDrInfo();PageModel<Map<String, Object>> pageModel = new PageModel<Map<String, Object>>();pageModel.setPages(1);// 必须先比pageno设置pageModel.setPageNo(1);pageModel.setPageSize(20);Map<String, Object> map = new HashMap<String, Object>();pageModel = kpService.getTaskPageModel(pageModel, map);model.addAttribute("pageModel", pageModel);model.addAttribute("map", map);return "timemanager/tasklist";}/*** @author wdg* 新增的定时管理页面 */@RequestMapping(value = "/addTaskAction_init")public String addTaskAction_Init(HttpServletRequest request, HttpServletResponse response, Model model) {return "timemanager/addtask";}/*** @author wdg* 修改页面的初始化*/@RequestMapping(value = "/modifyTaskAction_init")public String modifyTaskAction_Init(HttpServletRequest request, HttpServletResponse response, String rowguid,Model model) {if (StrUtil.isNotBlank(rowguid)) {TaskInfo taskinfo = kpService.getTaskInfoByGuid(rowguid);model.addAttribute("taskinfo", taskinfo);}return "timemanager/modifytask";}/*** * @author wdg* 新增对应的任务*/@RequestMapping(value = "/addTaskAction")public void addTaskAction(@RequestBody Map<String, Object> reqMap, HttpServletResponse response) {response.setCharacterEncoding("utf-8");//传递一个当前开票的比例PrintWriter out = null;try {out = response.getWriter();}catch (IOException e) {logger.info(e.getMessage());}String msg = "0";TaskInfo taskinfo = new TaskInfo();taskinfo.setRowguid(UUID.randomUUID().toString());taskinfo.setAddtime(new Date());taskinfo.setTaskname(reqMap.get("taskname") == null ? "" : reqMap.get("taskname").toString());taskinfo.setTaskclass(reqMap.get("taskclass") == null ? "" : reqMap.get("taskclass").toString());taskinfo.setCronstr(reqMap.get("cornstr") == null ? "" : reqMap.get("cornstr").toString());taskinfo.setRunstatus("1".equals(reqMap.get("runstatus")) ? 1 : 0);taskinfo.setRemark(reqMap.get("remark") == null ? "" : reqMap.get("remark").toString());//数据检验if (StrUtil.isNotBlank(taskinfo.getTaskname())) {int num = kpService.isExistTaskName(taskinfo.getTaskname());if (num > 0) {msg = "1";out.print(msg);return;}}if (StrUtil.isNotBlank(taskinfo.getTaskclass())) {int num = kpService.isExistTaskClass(taskinfo.getTaskclass());if (num > 0) {msg = "2";out.print(msg);return;}}msg = kpService.addTaskInfo(taskinfo) + "";out.print(msg);}/*** @author wdg* 修改任务*/@RequestMapping(value = "/modifyTaskAction")public void modifyTaskAction(@RequestBody Map<String, Object> reqMap, HttpServletResponse response) {response.setCharacterEncoding("utf-8");//传递一个当前开票的比例PrintWriter out = null;try {out = response.getWriter();}catch (IOException e) {logger.info(e.getMessage());}String msg = "0";String rowguid = reqMap.get("rowguid") == null ? "" : reqMap.get("rowguid").toString();TaskInfo taskinfo = kpService.getTaskInfoByGuid(rowguid);taskinfo.setAddtime(new Date());taskinfo.setTaskname(reqMap.get("taskname") == null ? "" : reqMap.get("taskname").toString());taskinfo.setTaskclass(reqMap.get("taskclass") == null ? "" : reqMap.get("taskclass").toString());taskinfo.setCronstr(reqMap.get("cornstr") == null ? "" : reqMap.get("cornstr").toString());taskinfo.setRunstatus("1".equals(reqMap.get("runstatus")) ? 1 : 0);taskinfo.setRemark(reqMap.get("remark") == null ? "" : reqMap.get("remark").toString());if (StrUtil.isNotBlank(taskinfo.getTaskname())) {int num = kpService.isExistTaskName(taskinfo.getTaskname(), taskinfo.getRowguid());if (num > 0) {msg = "1";out.print(msg);return;}}if (StrUtil.isNotBlank(taskinfo.getTaskclass())) {int num = kpService.isExistTaskClass(taskinfo.getTaskclass(), taskinfo.getRowguid());if (num > 0) {msg = "2";out.print(msg);return;}}msg = kpService.updateTaskInfo(taskinfo) + "";out.print(msg);}/*** @author wdg* 删除任务*/@RequestMapping(value = "/delTaskAction")public void delTaskAction(@RequestBody Map<String, Object> reqMap, HttpServletResponse response, Model model) {PrintWriter out = null;try {out = response.getWriter();}catch (IOException e) {logger.info(e.getMessage());}String rowguid = reqMap.get("rowguid") + "";int num = kpService.delTaskByGuid(rowguid);out.print(num);}/*** @author wdg* 启动停止任务*/@RequestMapping(value = "/triggerTaskAction")public void triggerTaskAction(@RequestBody Map<String, Object> reqMap, HttpServletResponse response, Model model) {PrintWriter out = null;try {out = response.getWriter();}catch (IOException e) {logger.info(e.getMessage());}String rowguid = reqMap.get("rowguid") + "";TaskInfo taskinfo = kpService.getTaskInfoByGuid(rowguid);String taskclass = taskinfo.getTaskclass();if (taskinfo.getRunstatus() == 0) {Class<?> clazz = null;try {clazz = Class.forName(taskclass);HashMap<String, Object> hmap = new HashMap<String, Object>();hmap.put(ZpBillService.class.getName(), zpBillServiceImpl);hmap.put(IKpTimeService.class.getName(), kpService);hmap.put(PzService.class.getName(), pzServiceImpl);Constructor<?> con = clazz.getConstructor(Map.class);future = threadPoolTaskScheduler.schedule((Runnable) con.newInstance(hmap),new CronTrigger(taskinfo.getCronstr()));map.put(taskclass, future);taskinfo.setRunstatus(1);kpService.updateTaskInfo(taskinfo);out.print("启动成功!");}catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (ClassNotFoundException e) {out.print("启动失败ClassNotFound:" + e.getMessage());e.printStackTrace();}}else {try {future = map.get(taskclass);while (!future.isCancelled()) {future.cancel(true);}if (future.isCancelled()) {taskinfo.setRunstatus(0);kpService.updateTaskInfo(taskinfo);out.println("暂停成功");}}catch (NullPointerException e) {taskinfo.setRunstatus(0);kpService.updateTaskInfo(taskinfo);}}}@RequestMapping("/stopCron")public void stopCron(HttpServletResponse response) {response.setCharacterEncoding("utf-8");PrintWriter out = null;try {out = response.getWriter();}catch (IOException e) {logger.info(e.getMessage());}threadPoolTaskScheduler.getScheduledExecutor().shutdown();threadPoolTaskScheduler.destroy();threadPoolTaskScheduler.initialize();if (map.size() > 0) {map.clear();}List<TaskInfo> list = kpService.getTaskInfoList();for (TaskInfo taskinfo : list) {if (taskinfo.getRunstatus() == 1) {taskinfo.setRunstatus(0);kpService.updateTaskInfo(taskinfo);}}out.print("全部服务关闭");}
}
然后是Runnable:
package com.hangxin.time.job;import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;import org.apache.log4j.Logger;
import org.dom4j.Element;import com.hangxin.commons.util.PageModel;
import com.hangxin.system.util.GlobalUtil;
import com.hangxin.time.service.IKpTimeService;
import com.hangxin.xxdzfp.service.PzService;
import com.hangxin.xxdzfp.service.ZpBillService;
import com.hangxin.xxdzfp.util.Ikp;
import com.hangxin.xxdzfp.util.KpNeg;
import com.hangxin.xxdzfp.util.KpPos;public class KPRunnable implements Runnable
{private Logger logger;private LinkedList<Map<String, Object>> linkedList = null;private int wknum = 0;private ZpBillService zpBillServiceImpl;private IKpTimeService kpService;private PzService pzServiceImpl;public KPRunnable(Map<String, Object> conmap) {logger = Logger.getLogger(KPRunnable.class);zpBillServiceImpl = (ZpBillService) conmap.get(ZpBillService.class.getName());kpService = (IKpTimeService) conmap.get(IKpTimeService.class.getName());pzServiceImpl = (PzService) conmap.get(PzService.class.getName());wknum = kpService.getNoKpNum();if (wknum != 0) {String taxno = GlobalUtil.getProperty("taxno");PageModel<Map<String, Object>> pageModel = new PageModel<Map<String, Object>>();pageModel.setPages(1);pageModel.setPageNo(1);pageModel.setPageSize(wknum);Map<String, Object> map = new HashMap<String, Object>();map.put("invoicetype", "1");map.put("nsrsbh", taxno);pageModel = zpBillServiceImpl.findXxfpList(pageModel, map);List<Map<String, Object>> list = pageModel.getList();linkedList = new LinkedList<Map<String, Object>>(list);}}@Overridepublic void run() {System.out.println("开票服务正在进行......");if (linkedList != null) {Map<String, Object> linkmap = linkedList.getFirst();try {String data = "";Ikp ikp = null;if (!"".equals(linkmap.get("YFP_DM").toString())) {ikp = new KpNeg();}else {ikp = new KpPos();}Element res = zpBillServiceImpl.kp(Long.valueOf(Long.parseLong(linkmap.get("id").toString())), ikp);if ("0000".equals(res.element("RETURNCODE").getText())) {zpBillServiceImpl.backResult(res, Long.valueOf(Long.parseLong(linkmap.get("id").toString())));logger.info("NSRDZDAH:" + linkmap.get("NSRDZDAH") + "开票成功!");linkedList.removeFirst();}else {data = res.element("RETURNCODE").getText() + ":" + res.element("RETURNMSG").getText();logger.debug(linkmap.get("id").toString() + ":" + data);logger.info("NSRDZDAH:" + linkmap.get("NSRDZDAH") + "开票失败:" + data);linkedList.removeFirst();}}catch (Exception e) {e.printStackTrace();}}}}
上面就是主要的代码了,希望对你有所帮助!
如何创建一个定时管理的页面相关推荐
- HQChart使用教程1- 如何快速创建一个K线图页面
快速创建一个K线图页面 HQChart介绍 demo页面代码 Option的配置项说明 Type Symbol IsAutoUpdate IsShowRightMenu IsShowCorssCurs ...
- 如果创建一个符合SEO的页面?
如果你的网站,主要的核心目的是为了提高企业商业产品的转化,那么,你一定希望,它的上线,可以获得源源不断的精准流量,我们知道,流量获取的方式有很多,比如: ①PPC竞价排名 ②搜索引擎自然排名 ③社交媒 ...
- [原创] OneThink模型管理详解-10分钟创建一个团队管理后台
本文为原创,根据自己在使用onethink创建web应用时的真实经历撰写而成.请支持本人版权,转载时注明出处.仅为经验分享,不代表权威,不完善不准确之处欢迎纠正,有任何问题可对本文留言. 1.1.1 ...
- 创建一个员工信息管理界面,页面如下
代码如下: package demotest; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; ...
- 如何为您的插件创建一个很棒的WordPress.org页面
You've just developed an awesome plugin for WordPress and you want to share it with as many people a ...
- Android 系统定时管理器AlarmManager的使用
2019独角兽企业重金招聘Python工程师标准>>> 1.什么是AlarmManager? 按照Alarm+Manager的拼写可知,这是一个定时管理器,不能称为定时器,或者闹钟之 ...
- 【Django快速开发实战】(1~29)使用Django创建一个基础应用:职位管理系统
1.总体描述 1.1产品需求: 1.2职位管理系统-建模 1.3 Django项目代码结构 新增recruitment项目 django-admin startproject recruitment ...
- WordPress主题制作进阶#3创建一个WordPress主题
创建WordPress主题 1.修改修改style.css 2.引用样式表 3.修改index.php文件 修改头文件 修改标题栏 修改导航菜单nav 4.后台增加页面 1.修改修改style.css ...
- 使用Divi创建在线学习管理 (LMS) 网站
我喜欢在线学习课程.开始一个设计良好.组织良好.内容丰富的线上课程是有道理的.如果做得好,这些线上课程可以让您充满成就感.但是学习一门很棒的线上课程和创建一个线上课程是两种完全不同的体验.根据我的经验 ...
最新文章
- LinearLayout测量原理解析:onMeasure(int,int)
- C语言经典例68-数组元素循环偏移
- draw.io项目本地部署
- CKMLCP前期未结算_报错
- Riot美术师经验分享:好设计是改出来的
- github --- 多个项目的管理方式
- Ubuntu安装搭建Clion环境
- (acm)C++加速输入的几种方法
- 利用好手头的资源解决海量语料资源收集以及利用哈工大的LTP云平台解决依存句法和语义依存分析
- 01-09 Linux三剑客-sed
- oracle创建默认序列号,PLS-00103:为序列号oracle创建触发器(PLS-00103: Create trigger for sequence number oracle)...
- Java 9 : 从零开始实现模块化(一)
- java中的IO整理(上)(微信文章)
- Hyper-V虚拟化——在Hyper-V上创建虚拟服务器
- 如何证明Coq标准库中filter引理
- keras导入weights
- 未来教育1级计算机基础及ms,未来教育.全国计算机等级考试一本通一级计算机基础及MS Office应用...
- 如何安装鸿蒙应用,华为鸿蒙OS系统手机怎么安装第三方的应用程序?
- ps磁性套索工具如何抠图
- 全球及中国环绕声扬声器行业投资前景预测与发展战略规划分析报告2022-2028年