Spring's support for quartz

Source: Internet
Author: User
Notice: spring3.1.1 does not seem to support quartz 2.x. This article is for quartz1.x.1. view code of the required software package (the following is the maven dependency configuration)

<dependency>            <groupId>${spring.groupId}</groupId>            <artifactId>spring-core</artifactId>        </dependency>        <dependency>            <groupId>${spring.groupId}</groupId>            <artifactId>spring-context</artifactId>        </dependency>        <dependency>            <groupId>${spring.groupId}</groupId>            <artifactId>spring-beans</artifactId>        </dependency>        <dependency>            <groupId>org.opensymphony.quartz</groupId>            <artifactId>quartz-all</artifactId>            <version>1.6.1</version>        </dependency>        <dependency>            <groupId>${spring.groupId}</groupId>            <artifactId>spring-context-support</artifactId>        </dependency>        <dependency>            <groupId>commons-collections</groupId>            <artifactId>commons-collections</artifactId>            <version>3.2.1</version>        </dependency>        <dependency>            <groupId>${spring.groupId}</groupId>            <artifactId>spring-tx</artifactId>        </dependency>

 

2. Support for the org. Quartz. Job Interface

Spring provides an abstract class org. springframework. Scheduling. Quartz. quartzjobbean to simplify the implementation of the job interface. The following code list:

Public class tensecondjob extends quartzjobbean {Private Static int times = 0; @ override protected void executeinternal (jobexecutioncontext arg0) throws jobexecutionexception {int seconds = (times ++) * 10; system. out. println ("10 seconds trigger:" + seconds + "seconds have passed! ");}}
3. Support for the org. Quartz. jobdetail class

Spring supports jobdetail in two ways,

3.1 provides a convenient Bean -- org. springframework. Scheduling. Quartz. jobdetailbean inherits jobdetail. The class declaration of this class is as follows:
public class JobDetailBean extends JobDetail        implements BeanNameAware, ApplicationContextAware, InitializingBean {}

Therefore, we can use jobdetailbean in the context configuration file to configure jobdetail:

<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">        <property name="jobClass"            value="com.sin90lzc.train.train_spring_quartz.TenSecondJob"></property>    </bean>

 

3.2 provides a factory Bean -- org. springframework. Scheduling. Quartz. methodinvokingjobdetailfactorybean to generate the jobdetail object.

Methodinvokingjobdetailfactorybean uses a common JavaBean method to implement the job function. This is actually a proxy object that generates a job interface.

First, create a common JavaBean -- jobbean:

Public class jobbean {private int second = 0; Public void printpersecond () {system. Out. println ("second trigger:" + (second ++) + "passed! ");}}

The printpersecond method provides the function that a job should complete. You can configure methodinvokingjobdetailfactorybean to generate a jobdetail object.

  <bean id="jobDetailCreateByFactaryBean"        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">        <property name="targetObject">            <bean class="com.sin90lzc.train.train_spring_quartz.JobBean" />        </property>        <property name="targetMethod" value="printPerSecond"></property>        <property name="concurrent" value="true"></property>    </bean>

 

4. Spring's support for the org. Quartz. Trigger interface 4.1's support for org. Quartz. simpletrigger provides a convenient Bean -- org. springframework. Scheduling. Quartz. simpletriggerbean.
<bean id="simpleTrigger"        class="org.springframework.scheduling.quartz.SimpleTriggerBean">        <property name="jobDetail" ref="jobDetail"></property>        <property name="startDelay" value="0"></property>        <property name="repeatInterval" value="10000"></property>    </bean>
4.2 The support for org. Quartz. crontrigger provides a convenient Bean -- org. springframework. Scheduling. Quartz. crontriggerbean
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">        <property name="jobDetail" ref="jobDetailCreateByFactaryBean"></property>        <property name="cronExpression" value="* * * * * ? *"></property>    </bean>
5. Spring support for the org. Quartz. scheduler Interface

Spring provides a factory bean to generate the scheduler object. The following is the declaration of the factory Bean:

public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBean<Scheduler>, BeanNameAware,        ApplicationContextAware, InitializingBean, DisposableBean, SmartLifecycle {}

The following is an example of the configuration of schedulerfactorybean:

  <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">        <property name="triggers">            <list>                <ref bean="simpleTrigger" />                <ref bean="cronTrigger" />            </list>        </property>    </bean>
6. Test the code and configuration above.
public static void main(String[] args) throws Exception{        ApplicationContext context = new ClassPathXmlApplicationContext(                "applicationContext.xml");        //TimeUnit.MINUTES.sleep(10);    }

 

 

 

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.