In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to implement integrated timed task SchedulingConfigurer". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Background:
The project needs to start a scheduled task, which can be stopped at any time and requires that the execution rules be configured in the database.
Research: using scheduling timed tasks to integrate with springboot
Example:
Step 1: create a data table and save the tasks you perform
/ * DDL information * /-CREATE TABLE `hk_sys_ task` (`id`bigint (21) NOT NULL AUTO_INCREMENT COMMENT 'primary key', `task_ uuid` varchar (50) DEFAULT NULL COMMENT 'task UUID', `task_ name` varchar (50) DEFAULT NULL COMMENT' task name', `task_ cron`varchar (50) DEFAULT NULL COMMENT 'task timing expression', `class_ name`varchar (100) DEFAULT NULL COMMENT 'task class' `method_ name` varchar (1) DEFAULT NULL COMMENT 'task method', `task_ type` int (1) DEFAULT NULL COMMENT 'task type', `remark` varchar (1) DEFAULT NULL, `remark` int (1) DEFAULT '1percent, ``int (1) DEFAULT' 1percent, `create_ user`varchar (50) DEFAULT NULL, `modify_ user` varchar (50) DEFAULT NULL, `modify_ time` timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
2. SpringUtil users who create springboot to obtain bean
/ * @ Author: Liu Yue * @ Descripition: * @ Date; Create in, 2021-5-14 11:19 * * / @ Component@Slf4jpublic class SpringUtil implements ApplicationContextAware {private static ApplicationContext applicationContext; @ Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {if (SpringUtil.applicationContext = = null) {SpringUtil.applicationContext = applicationContext } log.info ("= ApplicationContext configuration is successful. In ordinary classes, you can get the applicationContext object by calling SpringUtils.getAppContext (), applicationContext= {},", SpringUtil.applicationContext+ "=");} / / get applicationContext public static ApplicationContext getApplicationContext () {return applicationContext;} / / get Bean through name. Public static Object getBean (String name) {return getApplicationContext (). GetBean (name);} / / get Bean through class. Public static T getBean (Class clazz) {return getApplicationContext () .getBean (clazz);} / / return the specified Bean public static T getBean (String name,Class clazz) {return getApplicationContext () .getBean (name, clazz) through name and Clazz;}}
3. Configure the class SysTaskConfig that executes the task
/ * * @ Author: Liu Yue * @ Descripition: * @ Date; Create in 9:34 on 2021-5-14 * * / @ Lazy (value = false) @ Component@Slf4jpublic class SysTaskConfig implements SchedulingConfigurer {@ Resource private HkSysTaskMapper hkSysTaskMapper; private static ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler (); private static Map clazz; try {clazz = Class.forName (task.getClassName ()) String className = lowerFirstCapse (clazz.getSimpleName ()); Object bean = SpringUtil.getBean (className); Method method = ReflectionUtils.findMethod (bean.getClass (), task.getMethodName ()); ReflectionUtils.invokeMethod (method, bean);} catch (ClassNotFoundException e) {e.printStackTrace () };} private List checkDataList (List list) {String errMsg= "; for (int iTuno / I scheduledFuture = scheduledFutureMap.get (task.getId ()); if (scheduledFuture! = null & &! scheduledFuture.isCancelled ()) {scheduledFuture.cancel (Boolean.FALSE);} scheduledFutureMap.remove (task.getId ()) Log.info ("cancel scheduled task" + task.getId ());} / * * edit * @ param task * @ param * / public static void reset (HkSysTask task) {log.info ("modify scheduled task start" + task.getId ()); cancel (task); start (task) Log.info ("modify scheduled task end" + task.getId ());}}
4. Test classes executed on a regular basis
. Public void test () {iHkUserService.userTaskRun ();}.
5. Log of console output
This is the end of the content of "how to implement integrated timed task SchedulingConfigurer". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.