Quartz定时任务--HelloWorld

1、新建Dynamic Web project工程(Maven管理)(也可以是其他可执行项目)
web项目打成war包,部署到tomcat服务器中,也可以是可执行jar包,让项目跑起来。
2、配置web.xml(如果采用war包部署到Tomcat容器中)

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext*.xml</param-value>
</context-param>

3、pom.xml文件添加依耐

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>3.1.4.RELEASE</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>1.8.6</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.1.0.RELEASE</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.1.0.RELEASE</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>

4、编写定时任务Job

package com.dima.test.quartz;

import java.text.SimpleDateFormat;
import java.util.Date;

public class QuartzJob {
    public static int count = 1;
    public void excute() {
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        System.out.println("第" + count++ + "次执行定时任务:" + date);
    }
}

5、applicationContext-quartz.xml文件配置Quartz

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    <!-- 1、定时任务具体实现类作业-->
    <bean id="TestJob" class="com.dima.test.quartz.QuartzJob"/>

    <!-- 2、定义一个任务JobDetail -->
    <bean id="TestJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="TestJob" />
        <property name="targetMethod" value="excute" />
        <property name="concurrent" value="false" />
    </bean>

    <!-- 3、调度定时任务的触发器JobTrigger-->
    <bean id="TestJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="TestJobDetail" />
        <property name="startDelay" value="1000" />  <!-- 延迟1秒执行 -->
        <property name="cronExpression">
            <!-- 每隔2秒触发一次 -->
            <value>*/2 * * * * ?</value>
        </property>
    </bean>

    <!-- 4、启动定时任务的调度器 -->
    <bean id="taskScheduler" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="TestJobTrigger" /><!-- 可以配置多个 -->
            </list>
        </property>
    </bean>

</beans>

6、测试方法启动(不适用Tomcat容器)

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import junit.framework.TestCase;

public class QuartzJobTest extends TestCase {

    public void testQuartzJob() {
        AbstractApplicationContext appContext = new ClassPathXmlApplicationContext("classpath*:applicationContext-*.xml");
        appContext.start();//可以不用这行代码
        synchronized (QuartzJobTest.class) {
            try {
                QuartzJobTest.class.wait();
            } catch (Throwable e) {
                System.out.println(e);
            }
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值