Java Spring Timed task implementation code _java

Source: Internet
Author: User

Copy Code code as follows:

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 ("Work with task start ...");
Business logic Code Call
System.out.println ("Time [" + New Java.util.Date (). toLocaleString ()
+ "]-----> Hello! ");
Log.info ("Process task over!");
catch (Exception e) {
Log.error ("Processing Task occurrence exception", e);
}
}

}

2. The following is configured in spring:

Copy Code code as follows:

<?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>
<!--configuration triggers-->
<bean id= "Crontrigger" class= "Org.springframework.scheduling.quartz.CronTriggerBean" >
<!--It is not possible to refer to taskjob directly in the attribute Jobdetail because he is asking for a jobdetail type of object, so we have to turn by Methodinvokingjobdetailfactorybean >
<property name= "Jobdetail" >
<ref bean= "Methodinvokingjobdetail"/>
</property>
<!--Daily 8 to 21 points every 1 minutes to trigger, specific instructions see Appendix-->
<property name= "Cronexpression" >
&LT;VALUE&GT;0 * 08-21 * *?</value>
</property>

</bean>

<bean class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<!--add triggers-->
<property name= "Triggers" >
<list>
<ref local= "Crontrigger"/>
</list>
</property>
</bean>

</beans>

3. Test execution class, you can see the timer task running as soon as you load the spring configuration file.

Copy Code code as follows:

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 ("Load Spring configuration file ...");
ApplicationContext context = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
SYSTEM.OUT.PRINTLN ("Load configuration file Complete!");
ApplicationContext context2 = new Classpathxmlapplicationcontext ("Test/timertask/quartztimer.xml");
}
}

If you want to run in a Web project, add the following code to the Web.xml:

Copy Code code as follows:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

The following are some notes from the Web:
field allows special characters allowed by the value
seconds 0-59,-*/
0-59,-*/
Hour 0-23,-*/
Date 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 * *?" every noon 1 2 point triggers
0 15 10? * * "10:15 every day to trigger
" 0 15 10 * * *? "every 10:15 trigger
" 0 15 10 * *? * "Trigger
0 15 10 * * Every day 10:15" 2005 "2005 every day 10:15 triggers
0 * 14 * *?" triggered every 1 minutes every day from 2 o'clock in the afternoon to 2:59
"0 0/5 14 * *?" triggers the
"5 0 0/5" Every 14 minutes during the Daily 2 o'clock in the afternoon to 2:55 period. , 18 * *? "triggers the
0 0-5 14 * * For every 5 minutes of the day from 2 o'clock in the afternoon to 2:55 and 6 o'clock in the afternoon to 6:55
" 1 10,44 0 in every 14 minutes of the Daily 2 o'clock in the afternoon to 2:05? " 3 WED "Wednesday 2:10 and 2:44 of each year of March triggers
0 15 10?" * Mon-fri "Monday to Friday 10:15 trigger
" 0 15 10 15 *? "15th 10:15 every month
" 0 L *? "The last day of the month 10:15 trigger
" 0 15 10? * 6L "The last Friday 10:15 of the month triggers
0 15 10? * 6L 2002-2005 "2002 to 2005 monthly last Friday 10:15 trigger
" 0 15 10? * 6#3 "The third Friday 10:15 of the month triggers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.