Quartz Framework Quick Start (iv)

Source: Internet
Author: User
The quartz framework is encapsulated in Spring's Scheduling.quartz package, making it possible to implement timed tasks without having to write any quartspring code at development time. Spring Methodinvokingjobdetailfactorybean Implementation of the JOB definition through Jobdetailbean. The latter is more practical, simply by specifying the classes to run and the methods to be run in the class, and Spring will automatically generate jobdetail that meet the Quartz requirements.

In the previous article, Quartz Framework QuickStart (c), we migrated the example to the Web environment, but we used the Quartz startup mechanism, in which we let the Web server start spring, which is scheduled for tasks through the spring configuration file.

1, create a Web project, join Spring.jar,quartz-1.6.0.jar,commons-collections.jar,jta.jar, Commons-logging.jar these packages.

2, create a class, add a method execute in the class, and we will schedule the method in a timed manner.
Code highlighting produced by Actipro Codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> package Com.vista.quartz;

Import Java.util.Date;

Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;
Import Org.quartz.JobExecutionContext;
Import org.quartz.JobExecutionException;

public class HelloWorld
{
private static Log logger = Logfactory.getlog (HelloWorld. Class); Log Recorder
Public HelloWorld ()
{
}
public void Execute ()
{
Logger.info ("Kick your ass and fuck your -"+ new Date ());
}
}

2. The spring configuration file Applicationcontext.xml is modified as follows:


Code highlighting produced by Actipro Codehighlighter (freeware)
http://www.CodeHighlighter.com/

-->;? XML version= "1.0" encoding= "UTF-8"?>
< beans
xmlns = "Http://www.springframework.org/schema/beans"
Xmlns:xsi = "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation = "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.0.xsd ">

<!--the work class to invoke-->
< Bean id = "Quartzjob" class = "Com.vista.quartz.HelloWorld" ></bean >
<!--define methods for calling objects and calling objects-->
< Bean id = "Jobtask" class = "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
Class--> called by <!--
< property name = "TargetObject" >
< ref bean = "Quartzjob"/>
</Property >
<!--calling methods in a class-->
< property name = "Targetmethod" >
< value > Execute </value >
</Property >
</Bean >
<!--define trigger time-->
< Bean id = "Dotime" class = "Org.springframework.scheduling.quartz.CronTriggerBean" >
< property name = "Jobdetail" >
< ref bean = "Jobtask"/>
</Property >
<!--cron expression-->
< property name = "Cronexpression" >
< value > 10,15,20,25,30,35,40,45,50,55 * * * *? </value >
</Property >
</Bean >
<!--general Management class if the lazy-init= ' false ' then the container starts and executes the scheduler-->
< Bean id = "Startquertz" Lazy-init = "false" Autowire = "no" class = "Org.springframework.scheduling.quartz.SchedulerF Actorybean ">
< property name = "Triggers" >
< list >
< ref bean = "Dotime"/>
</list >
</Property >
</Bean >
</Beans >

3, first in the console to test the above code, all we have to do is load the spring configuration file, the code is as follows:
Code highlighting produced by Actipro Codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> package Com.vista.quartz;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test
{
public static void Main (string[] args)
{
System.out.println ("Test start.");
ApplicationContext context = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
If you set the Startquertz Bean's lazy-init to false in the configuration file, do not instantiate
Context.getbean ("Startquertz");
System.out.print ("Test end..");
}
}

4, and then modify the Web.xml as follows so that Tomcat initializes spring at startup:
Code highlighting produced by Actipro Codehighlighter (freeware)
http://www.CodeHighlighter.com/

-->;? XML version= "1.0" encoding= "UTF-8"?>
< Web-app Version = "2.4"
xmlns = "Http://java.sun.com/xml/ns/j2ee"
Xmlns:xsi = "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation = "Http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
< Context-param >
< Param-name > contextconfiglocation </param-name >
< Param-value >
/web-inf/classes/applicationcontext.xml
</param-value >
</Context-param >

< servlet >
< Servlet-name > Springcontextservlet </servlet-name >
< Servlet-class > Org.springframework.web.context.ContextLoaderServlet </servlet-class >
< Load-on-startup > 1 </load-on-startup >
</servlet >

< Welcome-file-list >
< Welcome-file > index.jsp </welcome-file >
</welcome-file-list >
</Web-app >

5, and finally start Tomcat, the test results are shown in the following illustration:

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.