我是靠谱客的博主 独特小兔子,最近开发中收集的这篇文章主要介绍关于Quartz的疑问——已解决,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

问题:

使用Quartz,在服务器上使用shutdown命令无法完全关闭进程,必须使用skil命令杀死进程。

本地使用时,使用restart命令不会关闭原tomcat,而是另起一个tomcat,console Selected会有两个

必须使用terminate命令关闭

(猜测是tomcat关闭时,没有销毁定时器)问题已解决,写在最下面

 

需求:可以随时更改的定时器(定时器可以关闭,即初始时可能关闭,可能开启)

实现:

创建一个QuartzManagerUtil用于关联定时任务添删改;(网上很多)

考虑到,这个定时器不能默认加载,所有创建了一个实现 InitializingBean接口,并重写afterPropertiesSet方法来执行定时器。

代码:

InitServlet——用于初始化定时器(使用其它方法无法在程序加载完后启动定时器--)

@Component
public class InitServlet implements InitializingBean {
@Autowired
private ActionSetvice actionSetvice;
@Autowired
private ActionCommService actionCommService;
@Override
public void afterPropertiesSet() throws Exception {
actionCommService.initSupUser();
actionSetvice.initSupUser();
System.out.println("定时器执行");
}
}


ActionCommService——两个定时任务里的一个控制核心同时实现job接口,用于对定时器控制

package com.jy.system.task.service;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import javax.annotation.Resource;
import
org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.weixin.ctrl.TemplatMsg;
import com.jfinal.weixin.model.Template;
import com.jfinal.weixin.model.TemplateParam;
import com.jfinal.weixin.sdk.api.AccessTokenApi;
import com.jy.common.utils.DateUtil;
import com.jy.common.utils.QuartzManagerUtil;
import com.jy.system.action.entity.Action;
import com.jy.system.action.mapper.ActionMapper;
import com.jy.system.actionrecord.entity.ActionRecord;
import com.jy.system.actionrecord.mapper.ActionRecordExtMapper;
import com.jy.system.actionrecord.mapper.ActionRecordMapper;
import com.jy.system.card.entity.Card;
import com.jy.system.user.supuser.mapper.SupUserExtMapper;
import com.jy.system.user.supuser.vo.ActionChanceVO;
import com.jy.system.user.userinfo.entity.UserInfo;
import com.jy.system.user.userinfo.mapper.UserInfoMapper;
import cn.com.iotrust.common.utils.SpringContextHolder;
/**
* @author chy
* @date 2019年7月26日 下午4:14:47
* @function
* @version 1.0
* @parameter
* @since
* @return
*/
@Service("/ActionCommService")
public class ActionCommService implements Job{
private static final Logger logger = LoggerFactory.getLogger(ActionCommService.class);
@Resource
private SupUserExtMapper supUserExtMapper;
@Resource
private UserInfoMapper userInfoMapper;
@Resource
private ActionMapper actionMapper;
@Resource
private ActionRecordMapper actionRecordMapper;
@Resource
private ActionRecordExtMapper actionRecordExtMapper;
public static Integer peoples = 10;
public static String cron = "";
public static Integer dynamic = 10;
public static String MY_TASK = "MY_TASK";
public static Action action;
/**
*
* @param peopleCount
* @return
* @Description: TODO
* @author chenyao
* @data 2019年7月27日 上午11:08:39 v1.0
*/
public Set<Long> Draw(int peopleCount, int liveness) {
//任务执行
}
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
supUserExtMapper = SpringContextHolder.getBean(SupUserExtMapper.class);
userInfoMapper = SpringContextHolder.getBean(UserInfoMapper.class);
actionMapper = SpringContextHolder.getBean(ActionMapper.class);
actionRecordExtMapper = SpringContextHolder.getBean(ActionRecordExtMapper.class);
actionRecordMapper = SpringContextHolder.getBean(ActionRecordMapper.class);
//
supUserExtMapper = (SupUserExtMapper) SpringContextJobUtil.getBean("SupUserExtMapper");
//
userInfoMapper = (UserInfoMapper) SpringContextJobUtil.getBean("UserInfoMapper");
//
actionMapper = (ActionMapper) SpringContextJobUtil.getBean("ActionMapper");
//
actionRecordExtMapper = (ActionRecordExtMapper) SpringContextJobUtil.getBean("ActionRecordExtMapper");
//
actionRecordMapper = (ActionRecordMapper) SpringContextJobUtil.getBean("ActionRecordMapper");
Draw(peoples, dynamic);
}
public void initSupUser() {
try {
action = actionMapper.selectByPrimaryKey(2L);
String[] h = action.getHour().split(":");
switch (action.getAutoUnlock()) {
case 0:
return;
case 1:
cron = "0 " + h[1] + " " + h[0] + " * * ?";
break;
case 2:
cron = "0 " + h[1] + " " + h[0] + " 1 * ?";
break;
case 3:
cron = "0 " + h[1] + " " + h[0] + " L * ?";
break;
case 4:
String d = action.getDay().toString();
String m = action.getMouth().toString();
d= d.equals("0")? "*":d;
m= m.equals("0")? "*":m;
cron = "0 " + h[1] + " " + h[0] + " " + d + " " + m + " ?";
break;
}
System.out.println(cron);
QuartzManagerUtil.addJob(MY_TASK, ActionCommService.class, cron, MY_TASK);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
public static void update(Action actionl) {
action = actionl;
}
// 停止任务
public void stopSupUser(Map<String, Object> result, Action action) {
try {
update(action);
QuartzManagerUtil.pauseJob(MY_TASK);
action.setAutoUnlock(0);
actionMapper.updateByPrimaryKeySelective(action);
result.put("result", 1);
} catch (Exception e) {
result.put("result", 0);
result.put("message", e.getMessage());
logger.error(e.getMessage(), e);
}
}
// 更新或开启
public void updateSupUserr(Action action, Map<String, Object> result) {
try {
update(action);
dynamic = action.getDynamicDay();
peoples = action.getTally();
actionMapper.updateByPrimaryKeySelective(action);
String[] h = action.getHour().split(":");
switch (action.getAutoUnlock()) {
case 0:
cron = "0 15 10 * * ? 1970";
break;
case 1:
cron = "0 " + h[1] + " " + h[0] + " * * ?";
break;
case 2:
cron = "0 " + h[1] + " " + h[0] + " 1 * ?";
break;
case 3:
cron = "0 " + h[1] + " " + h[0] + " L * ?";
break;
case 4:
String d = action.getDay().toString();
String m = action.getMouth().toString();
d= d.equals("0")? "*":d;
m= m.equals("0")? "*":m;
cron = "0 " + h[1] + " " + h[0] + " " + d + " " + m + " ?";
break;
}
logger.debug("cron:" + cron);
QuartzManagerUtil.removeJob(MY_TASK);
QuartzManagerUtil.addJob(MY_TASK, ActionCommService.class , cron, MY_TASK);
result.put("result", 1);
} catch (Exception e) {
result.put("result", 0);
result.put("message", e.getMessage());
logger.error(e.getMessage(), e);
}
}
}

 ActionCommTask——定时任务的控制操作类,让前端发送请求用于修改,和InitServlet初始化启动控制器

@Component
@Controller
@RequestMapping(value = "/actioncommtask")
public class ActionCommTask {
private static final Logger logger = LoggerFactory.getLogger(ActionCommTask.class);
@Autowired
private ActionCommService actionCommService;
@Resource
private ActionRecordExtMapper actionRecordExtMapper;
public static Integer peoples = 10;
public static String cron = "";
public static Integer dynamic = 10;
public static String MY_TASK = "MY_TASK";
public static Action action;
@RequestMapping(value = "/stopTask", method = RequestMethod.POST)
@ResponseBody
@Transactional(rollbackFor = { Exception.class })
public Object stopSupUser(@RequestBody JSONObject jo) {
Map<String, Object> resultMap = new HashMap<String, Object>();
try {
Action action = jo.toJavaObject(Action.class);
actionCommService.stopSupUser(resultMap, action);
} catch (Exception e) {
resultMap.put("result", 0);
resultMap.put("message", e.getMessage());
logger.error(e.getMessage(), e);
throw e;
}
return resultMap;
}
@RequestMapping(value = "/changeCommTask", method = RequestMethod.POST)
@ResponseBody
@Transactional(rollbackFor = { Exception.class })
public Object updateSupUserr(@RequestBody JSONObject jo) {
Map<String, Object> resultMap = new HashMap<String, Object>();
try {
Action action = jo.toJavaObject(Action.class);
actionCommService.updateSupUserr(action, resultMap);
resultMap.put("result", 1);
} catch (Exception e) {
resultMap.put("result", 0);
resultMap.put("message", e.getMessage());
logger.error(e.getMessage(), e);
throw e;
}
return resultMap;
}
@RequestMapping(value = "/goCommTask", method = RequestMethod.POST)
@ResponseBody
public Object goCommTask(@RequestBody JSONObject jo) {
Map<String, Object> resultMap = new HashMap<String, Object>();
try {
Action action = jo.toJavaObject(Action.class);
String date = DateUtil.getCurDate().toString();
List<ActionRecordVO> list = actionRecordExtMapper.selectActionRecord(2L, date, 1, 999);
if (list.size() == 0) {
actionCommService.Draw(action.getTally(), action.getDynamicDay());
}
resultMap.put("result", 1);
} catch (Exception e) {
resultMap.put("result", 0);
resultMap.put("message", e.getMessage());
logger.error(e.getMessage(), e);
}
return resultMap;
}
}

 

问题解决方法:

创建一个QuartzContextListener实现ServletContextListener,注意@WebListener,在tomcat销毁时将定时器的线程关闭。

@WebListener
public class QuartzContextListener implements ServletContextListener
{
@Override
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
QuartzManagerUtil.shutdownJobs();
System.out.println("定时器关闭");
}
}

 

最后

以上就是独特小兔子为你收集整理的关于Quartz的疑问——已解决的全部内容,希望文章能够帮你解决关于Quartz的疑问——已解决所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部