quartz
官网地址:http://www.quartz-scheduler.org/documentation/quartz-2.3.0/quick-start.html#starting-a-sample-application
logback.xml
<?xml version="1.0" encoding="UTF-8" ?>
<configuration debug="false" scan="false">
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<root lever="WARN">
<appender ref="console"/>
</root>
</configuration>
根据官方文档提供案例
public class QuartzTest {
public static void main(String[] args) {
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
scheduler.shutdown();
} catch (Exception se) {
se.printStackTrace();
}
}
}
打印结果
22:24:46: Executing task 'QuartzTest.main()'...
> Task :compileJava UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :compileTestJava
> Task :processTestResources UP-TO-DATE
> Task :testClasses
> Task :QuartzTest.main()
22:24:48,645 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
22:24:48,645 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/E:/javaTools/workspace/share-quartz/build/resources/test/logback.xml]
22:24:48,805 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
22:24:48,807 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
22:24:48,814 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [console]
22:24:48,825 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
22:24:48,903 |-ERROR in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Could not find an appropriate class for property [appender]
22:24:48,903 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
22:24:48,907 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@4b6995df - Registering current configuration as safe fallback point
Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
See https://docs.gradle.org/7.4.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 2s
5 actionable tasks: 2 executed, 3 up-to-date
22:24:49: Task execution finished 'QuartzTest.main()'.
此时根据官网描述,创建简单的任务实例
public class QuartzTest {
public static void main(String[] args) {
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity("job1", "group1")
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5)
.repeatForever())
.build();
scheduler.scheduleJob(job, trigger);
TimeUnit.SECONDS.sleep(20);
scheduler.shutdown();
} catch (Exception se) {
se.printStackTrace();
}
}
}
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Hello.execute!!!" + DFUtil.format(new Date()) + " " + Thread.currentThread().getName());
}
}
运行打印结果 主线程休眠20s,运行四次
22:27:34: Executing task 'QuartzTest.main()'...
> Task :compileJava UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :compileTestJava
> Task :processTestResources UP-TO-DATE
> Task :testClasses
> Task :QuartzTest.main()
22:27:36,648 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
22:27:36,648 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/E:/javaTools/workspace/share-quartz/build/resources/test/logback.xml]
22:27:36,790 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
22:27:36,791 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
22:27:36,798 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [console]
22:27:36,812 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
22:27:36,885 |-ERROR in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Could not find an appropriate class for property [appender]
22:27:36,885 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
22:27:36,887 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@61443d8f - Registering current configuration as safe fallback point
Hello.execute!!!2022.05.29 10:27:37 DefaultQuartzScheduler_Worker-1
Hello.execute!!!2022.05.29 10:27:42 DefaultQuartzScheduler_Worker-2
Hello.execute!!!2022.05.29 10:27:47 DefaultQuartzScheduler_Worker-3
Hello.execute!!!2022.05.29 10:27:52 DefaultQuartzScheduler_Worker-4
Hello.execute!!!2022.05.29 10:27:57 WorkerThread-LastJob
Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
See https://docs.gradle.org/7.4.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 22s
5 actionable tasks: 2 executed, 3 up-to-date
22:27:57: Task execution finished 'QuartzTest.main()'.