A tutorial on configuring Quartz in the Java Spring Framework _java

Source: Internet
Author: User

The process of configuring Quartz in spring:

1. Import Jar Package

The jar package that quartz needs is already contained in spring, located under the \lib\quartz of the spring uncompressed directory, Quartz-all-1.6.1.jar,

Copy it to the web-inf/lib of the project.


2. Configure Web.xml to load quartz configuration files on spring startup

<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.5" xmlns= "http://java.sun.com/xml/ns/" 
Java ee " 
  xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " 
  xsi:schemalocation=" http://java.sun.com/ Xml/ns/javaee  
  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> 
  <!--spring--> 
  < context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> classpath:application-*.xml</param-value> 
  </context-param> 
 
  <!--spring Monitor--> 
 
  <listener> 
    <listener-class> 
      org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
  </listener> 
</web-app> 


3. Write Quartz profile: Application-quartz.xml In fact, these configurations can all be written in other spring configuration files

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xsi:schemalocation=" Http://www.springframework.org/schema/beans http: Www.springframework.org/schema/beans/spring-beans-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http:// Www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http:// Www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <!--task entity--> <bean id=" testtaskbean "class=" com . Jp.task.TestTask "/> <!--Scheduled Renewal Policy Task method--> <bean id=" Testtasktimermethod "class=" Org.springframewo Rk.scheduling.quartz.MethodInvokingJobDetailFactoryBean "> <property name=" targetobject "ref=" Testtaskbean " 
  > <!--Targetmethod Configure the method names that are scheduled to execute-->  <property name= "Targetmethod" value= "executeAction"/> <property name= "concurrent" value= "false"/> </bean> <!--timing update policy triggers--> <bean id= "Testtasktrigger" class= "Org.springframework.scheduling.qu" Artz.  Crontriggerbean "> <property name=" jobdetail "ref=" Syncpolicytasktimermethod "/> <!--3 minutes per 0 sec execution 
  --> <property name= "cronexpression" value= "0 0/3 * * * *?"/> </bean> <!--custom Task List--> <bean id= "Scheduler" class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" > <property nam E= "triggers" > <list> <ref bean= "Testtasktrigger"/> </list> </property 

 > </bean> </beans>

4. Write Java class testtask to perform Tasks

Package com.jp.task; 
  
Import Org.quartz.JobExecutionContext; 
Import org.quartz.JobExecutionException; 
Import Org.springframework.scheduling.quartz.QuartzJobBean; 
 
 
public class Testtask extends quartzjobbean{ 
   
  Logger log = Logger.getlogger (Policyservicetest.class); 
     
  public void ExecuteAction () { 
    log.info ("Hello quartz"); 
  } 
 
  @Override 
  protected void executeinternal (Jobexecutioncontext arg0) 
      throws jobexecutionexception { 
     
  } 
 
} 

5. All work ready, one operation. Basically it's an error. It's about nosuchmethod such a mistake.
This is because the Commons-collections.jar version is too low. To Commons-collections-3.2.jar above.

When you look for a project, you have a commons-collections-3.2.jar. What's going on, then find a Commons-collections-2.1.1.jar in hibernate.

It's OK to delete the Commons-collections-2.1.1.jar.


description of Ps:cronexpression--cron expression

    • Seconds
    • Part
    • Hours
    • Date within the month
    • Month
    • Date in the week
    • Year (optional field)
    • Special characters

Cron triggers take advantage of a series of special characters, as follows:

The backslash (/) character represents the increment value. For example, in the second field, the "5/15" representation starts at 5th seconds, every 15 seconds.

The question mark (?) character and the letter L character are available only in the month date and week date fields. The question mark indicates that the field does not contain a specific value. So, if you specify a date within the month, you can insert "?" in the Week Date field, which means that the date value in the week does not matter. The letter L character is the abbreviation for last. Placed in the Month Date field, which is scheduled to be executed on the last day of the calendar. In the week Date field, if "L" exists alone, it equals "7", otherwise represents the last instance of the date within the week of the month. So "0L" is scheduled to be executed on the last Sunday of the month.

The letter (W) character in the Date field within the month arranges execution to the working day closest to the specified value. Put "1W" in the Month Date field, indicating that the execution is scheduled within the first working day of the month.

The pound sign (#) character specifies a specific weekday instance for a given month. Put "Mon#2" in the weekly Date field, indicating that the task is scheduled for the second Monday of the month.

The asterisk (*) character is a wildcard character that indicates that the field can accept any possible value.
Special characters allowed by field values:
Seconds 0-59,-*/
Cent 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,-*/
Meaning of expression
"0 0 12 * *" Every day 12 o'clock noon.
"0 15 10?" * * "Every 10:15 trigger
"0 15 10 * *" Every day 10:15 trigger
"0 15 10 * *? * "Every 10:15 trigger
"0 15 10 * *? 2005 "2005 Every day 10:15 triggers
"0 * 14 * *" is triggered every 1 minutes from 2 o'clock in the afternoon to 2:59 every day
"0 0/5 14 * *" is triggered every 5 minutes from 2 o'clock in the afternoon to 2:55 every day
"0 0/5 14,18 * * *" triggered every 5 minutes from 2 o'clock in the afternoon to 2:55 and 6 o'clock in the afternoon to 6:55 every day
"0 0-5 14 * *" is triggered every 1 minutes from 2 o'clock in the afternoon to 2:05 every day
"0 10,44 14?" 3 WED "Every March of Wednesday, 2:10 and 2:44 triggers
"0 15 10?" * Mon-fri "from Monday to Friday 10:15 trigger
"0 15 10 15 *" Every month 15th 10:15 trigger
"0 L *?" triggered 10:15 the last day of every month
"0 15 10?" * 6L "last Friday 10:15 trigger per month
"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 trigger per month
Every morning at 6.

0 6 * * *

Every two hours

0 */2 * * * *
Between 11 o'clock and 8 in the morning, every two hours, eight in the morning.

0 23-7/2,8 * * * *

4th a month and a week on Monday to three in the morning 11.

0 11 4 * 1-3
January 1 morning, 4.

0 4 1 1 *

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.