}
2. Next, configure it in spring:
<bean id="methodInvokingJobDetail">
<property name="targetObject">
<ref bean="taskJob" />
</property>
<property name="targetMethod">
<value>SayHello</value>
</property>
</bean>
<!-- Configure trigger-->
<bean id="cronTrigger">
<!-- TaskJob cannot be directly referenced in the attribute jobDetail here, because it requires an object of type jobDetail, so we have to transfer it through MethodInvokingJobDetailFactoryBean -->
<property name="jobDetail">
<ref bean="methodInvokingJobDetail" />
</property>
<!-- Triggered every 1 minute from 8:00 to 21:00 every day, please see the appendix for specific instructions-->
<property name="cronExpression">
<value>0 * 08-21 * * ?</value>
</property>
</bean>
<bean>
<!-- Add trigger -->
<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.
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("Loading spring configuration file....");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("Loading configuration file completed!");
//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.xml:
The following are some instructions excerpted from the Internet:
Field Allowed Values Allowed Special Characters seconds 0-59, - */
Minutes 0-59, - */
Hours 0-23, - */
Date 1-31, - * ? / LWC
Month 1-12 or JAN-DEC, - * /
Sunday 1-7 or SUN-SAT, - * ? / LC #
Year (optional) left blank, 1970-2099, - */
expression meaning
"0 0 12 * * ?" Triggers at 12 noon every day
"0 15 10 ? * *" triggers every day at 10:15 am
"0 15 10 * * ?" triggers every day at 10:15 AM
"0 15 10 * * ? *" triggers 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 2pm to 2:59pm every day
"0 0/5 14 * * ?" triggers every 5 minutes from 2pm to 2:55pm every day
"0 0/5 14,18 * * ?" triggers every 5 minutes between 2pm and 2:55pm and every 5 minutes between 6pm and 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:10 pm and 2:44 pm
"0 15 10 ? * MON-FRI" triggers 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 * ?" triggers at 10:15 am on the last day of each month
"0 15 10 ? * 6L" triggers on the last Friday of every month at 10:15 AM
"0 15 10 ? * 6L 2002-2005" Triggered at 10:15 AM on the last Friday of each month from 2002 to 2005
"0 15 10 ? * 6#3" triggers on the third Friday of every month at 10:15 AM