已解决 【提问】Kotlin中该怎么实现定时任务?
-
搜索过很多方法,比如Hutool的CronUtil、SpringBoot。但是遇到了挺多问题。
- 在使用Hutool的CronUtil用配置文件添加定时任务时,总是找不到定时任务的实现类。
定时任务实现类代码是这样的
class getAPI { //需要定时执行的任务 object jobs { fun Test() { val nowIs = LocalDateTimeUtil.now().format(DateTimeFormatter.ofPattern("HH-mm-ss")).toString() println(nowIs) } } }
配置文件是这样的
[org.ASN70.apiExercise] #每5秒向控制台打印当前时间(测试用) getAPI.jobs.Test = */5 * * * * * *
RunMirai中运行时一切正常,但编译成插件之后在console运行会报错
2022-07-30 22:50:41 E/api功能练习插件: cn.hutool.cron.CronException: Schedule [*/5 * * * * * *] [org.ASN70.apiExercise.getAPI.jobs.Test] error! cn.hutool.cron.CronException: Schedule [*/5 * * * * * *] [org.ASN70.apiExercise.getAPI.jobs.Test] error! at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.cron.Scheduler.schedule(Scheduler.java:216) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.cron.CronUtil.schedule(CronUtil.java:106) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.cron.CronUtil.start(CronUtil.java:170) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.cron.CronUtil.start(CronUtil.java:143) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar//org.ASN70.apiExercise.ApiExerciseMain.onEnable(ApiExerciseMain.kt:65) at net.mamoe.mirai.console.internal.plugin.JvmPluginInternal.internalOnEnable$mirai_console(JvmPluginInternal.kt:131) at net.mamoe.mirai.console.internal.plugin.BuiltInJvmPluginLoaderImpl.enable(BuiltInJvmPluginLoaderImpl.kt:278) at net.mamoe.mirai.console.internal.plugin.BuiltInJvmPluginLoaderImpl.enable(BuiltInJvmPluginLoaderImpl.kt:35) at net.mamoe.mirai.console.plugin.jvm.JvmPluginLoader$BuiltIn.enable(JvmPluginLoader.kt) at net.mamoe.mirai.console.plugin.jvm.JvmPluginLoader$BuiltIn.enable(JvmPluginLoader.kt:50) at net.mamoe.mirai.console.plugin.PluginManager.enablePlugin(PluginManager.kt:173) at net.mamoe.mirai.console.internal.plugin.PluginManagerImpl.enableAllLoadedPlugins$mirai_console(PluginManagerImpl.kt:178) at net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge.doStart$mirai_console(MiraiConsoleImplementationBridge.kt:235) at net.mamoe.mirai.console.MiraiConsoleImplementation$Companion.start(MiraiConsoleImplementation.kt:464) at net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader.startAsDaemon(MiraiConsoleTerminalLoader.kt:168) at net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader.startAsDaemon$default(MiraiConsoleTerminalLoader.kt:167) at net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader.main(MiraiConsoleTerminalLoader.kt:52) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) at org.itxtech.mcl.Utility.bootJars(Utility.java:89) at org.itxtech.mcl.Utility.bootJars(Utility.java:79) at org.itxtech.mcl.Utility.bootMirai(Utility.java:101) at org.itxtech.mcl.module.builtin.Boot.boot(Boot.java:109) at org.itxtech.mcl.module.ModuleManager.phaseBoot(ModuleManager.java:148) at org.itxtech.mcl.Loader.lambda$start$4(Loader.java:189) at org.itxtech.mcl.Loader.tryCatching(Loader.java:146) at org.itxtech.mcl.Loader.start(Loader.java:189) at org.itxtech.mcl.Loader.main(Loader.java:79) Caused by: cn.hutool.core.exceptions.UtilException: ClassNotFoundException: org.ASN70.apiExercise.getAPI.jobs at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.core.util.ClassLoaderUtil.doLoadClass(ClassLoaderUtil.java:319) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.core.util.ClassLoaderUtil.lambda$loadClass$1(ClassLoaderUtil.java:202) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.core.map.ReferenceConcurrentMap.lambda$computeIfAbsent$1(ReferenceConcurrentMap.java:137) at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(Unknown Source) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.core.map.ReferenceConcurrentMap.computeIfAbsent(ReferenceConcurrentMap.java:137) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.core.util.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:202) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.core.util.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:167) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.core.util.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:148) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.cron.task.InvokeTask.<init>(InvokeTask.java:43) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.cron.Scheduler.schedule(Scheduler.java:214) ... 29 more Caused by: java.lang.ClassNotFoundException: org.ASN70.apiExercise.getAPI.jobs at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source) at java.base/java.lang.ClassLoader.loadClass(Unknown Source) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Unknown Source) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.core.util.ClassLoaderUtil.doLoadClass(ClassLoaderUtil.java:314) ... 38 more
- 在使用SpringBoot来创建定时任务时,找不到@SpringBootApplication这个注解,可能是更新之后删除或者修改了,找了半天没找到新版本这个注解该怎么用
- 在使用Hutool的CronUtil用配置文件添加定时任务时,总是找不到定时任务的实现类。
-
-
顶一下😳
-
为了定时任务引入 SpringBoot ,真够蛋疼的
-