大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > Java技巧 > Spring+Quartz使用详解

Spring+Quartz使用详解(2)

关键词:SpringQuartz  阅读(2729) 赞(11)

[摘要]本文是对Spring+Quartz的讲解,对学习Java编程技术有所帮助,与大家分享。
<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.garfield.garserver</groupId>
        <artifactId>garserver</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    
    <artifactId>service</artifactId>
    <name>service</name>
    <url>http://maven.apache.org</url>

    
    <dependencies>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.0-beta9</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.0-beta9</version>
        </dependency>
        
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>

        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
        </dependency>
                    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
                    
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
                    
                    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>   
        
        <dependency>
            <groupId>com.garfield.garserver</groupId>
            <artifactId>entity</artifactId>
            <version>${garserver.version}</version>
        </dependency>
        
        

    </dependencies>

</project>

需要注意的是,示例需要spring的spring-context-support和spring-tx。

Spring配置文件:

<?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.xsd">

    <!-- 工作的bean -->
    <bean id="myJob"  />

    <!-- job的配置开始 -->
    <bean id="myJobDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="myJob" />
        </property>
        <property name="targetMethod">
            <value>work</value>
        </property>
    </bean>
    <!-- job的配置结束 -->


    <!-- 调度的配置开始 -->
    <bean id="crontestJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="myJobDetail" />
        </property>
        <property name="cronExpression">
            <value>10 0/1 * * * ?</value>  <!-- Cron表达式“10 */1 * * * ?”意为:从10秒开始,每1分钟执行一次。  -->
        </property>
    </bean>
    <!-- 调度的配置结束 -->


    <!-- 启动触发器的配置开始 -->
    <bean name="startQuertz" lazy-init="false" autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="crontestJobTrigger" />

            </list>
        </property>
    </bean>
    <!-- 启动触发器的配置结束 -->


</beans>

Quartz的类:

package com.garfield.quartz;

import java.util.Date;
import java.util.Scanner;

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

public class QuartzJob{
    
    private static String[] configLocations = new String[] { "com/garfield/config/servicebeans.xml" };
    
    public void work()
    {
        System.out.println("Quartz的任务调度!!!"+(new Date()).toString());
    }

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        System.out.println("Test start.");
        ApplicationContext ctx=new ClassPathXmlApplicationContext(configLocations);
        //如果配置文件中将startQuertz bean的lazy-init设置为false 则不用实例化
        QuartzJob qj=(QuartzJob)ctx.getBean("myJob");
        //qj.work();
        System.out.println("请输入信息:");
        Scanner input=new Scanner(System.in);
        int x=input.nextInt();
        System.out.println(x);
        System.out.print("Test end..");
        

    }

}

在这里,为了测试方便,将启动写在了main中,为了不使main退出,使用了一个无实际意义的等待读取输入,只是为了使程序运行不退出,好了,运行这个程序,Quartz可以正常工作了!

«上一页12下一页»


相关评论