import org.apache.log4j.*; public class TaskJob { public static Logger log = Logger .getLogger(TaskJob.class); public void SayHello() { // TODO Auto-generated method stub try { log.info("处理任务开始>........"); // 业务逻辑代码调用 System.out.println("时间[" + new java.util.Date().toLocaleString() + "]----->大家好啊!"); log.info("处理任务结束!"); } catch (Exception e) { log.error("处理任务出现异常", e); } } }
2. Next, configure it in spring:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean name="taskJob" class="util.TaskJob" /> <bean id="methodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="taskJob" /> </property> <property name="targetMethod"> <value>SayHello</value> </property> </bean> <!-- 配置触发器 --> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <!-- 这里不可以直接在属性jobDetail中引用taskJob,因为他要求的是一个jobDetail类型的对象,所以我们得通过MethodInvokingJobDetailFactoryBean来转一下 --> <property name="jobDetail"> <ref bean="methodInvokingJobDetail" /> </property> <!-- 每天的8点到21点每隔1分钟触发,具体说明见附录 --> <property name="cronExpression"> <value>0 * 08-21 * * ?</value> </property> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 添加触发器 --> <property name="triggers"> <list> <ref local="cronTrigger" /> </list> </property> </bean> </beans>
3. To test the execution class, you can see the scheduled tasks running as long as you load the spring configuration file.
package util; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApp { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("加载spring配置文件...."); ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println("加载配置文件完毕!"); // ApplicationContext context2 = new ClassPathXmlApplicationContext("test/timerTask/quartzTimer.xml"); } }
If you want to run it in a web project, you also need to add the following code to web. Characters
seconds 0-59, - * / minutes 0-59, - * /
hours 0-23, - * /
dates 1-31, - * ? / L W C
Month 1-12 or JAN-DEC, - * /
Week 1-7 or SUN-SAT, - * ? / L C
#Year (optional) Leave blank, 1970-2099, - * /
Expression meaning
"0 0 12 * * ?" Triggered every day at 12 noon
"0 15 10 ? * *" Triggered every day at 10:15 am
"0 15 10 * * ?" Every morning Triggered at 10:15
"0 15 10 * * ? *" Triggered every day at 10:15 AM
"0 15 10 * * ? 2005" Triggered every day at 10:15 AM in 2005
"0 * 14 * * ?" Triggers every 1 minute from 2 pm to 2:59 pm every day
"0 0/5 14 * * ?" Triggers every 5 minutes from 2 pm to 2:55 pm every day
"0 0/5 14,18 * * ?" Triggers every 5 minutes from 2pm to 2:55pm and every 5 minutes from 6pm to 6:55pm
"0 0-5 14 * * ?" " Triggers every 1 minute from 2pm to 2:05pm every day
"0 10,44 14 ? 3 WED" Triggers every Wednesday in March at 2:10pm and 2:44pm
"0 15 10 ? * MON-FRI" Triggered at 10:15 AM from Monday to Friday
"0 15 10 15 * ?" Triggered at 10:15 AM on the 15th of every month
"0 15 10 L * ?" Every Triggered at 10:15 am on the last day of the month
"0 15 10 ? * 6L" Triggered at 10:15 am on the last Friday of the month
"0 15 10 ? * 6L 2002-2005" 2002 to 2005 Triggered at 10:15 am on the last Friday of each month of the year
"0 15 10 ? * 6#3" Triggered at 10:15 am on the third Friday of each month
More spring timing in java For articles related to task implementation code, please pay attention to the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor