Quartz在Spring中的使用

本文介绍了一个基于SSH框架的系统任务实现方法,通过Spring整合Quartz完成定时任务的配置与执行。示例中定义了SSHJob类及其实现的work方法,并在applicationContext-quartz.xml中配置了任务调度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Quartz项目主页http://www.quartz-scheduler.org/

 

SSHJob.java

package ssh.core;

import org.apache.log4j.Logger;

/**
 * SSH系统任务
 * @author gary
 *
 */
public class SSHJob {
	Logger log = Logger.getLogger(this.getClass());

	public void work(){   
		log.debug("SSH System job");   
    }
}

 

applicationContext-quartz.xml

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-2.5.xsd	
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context-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
						http://cxf.apache.org/jaxws 
						http://cxf.apache.org/schemas/jaxws.xsd"
	default-lazy-init="true">
	
	<!-- 调用的类 -->
    <bean id="sshJob" class="ssh.core.SSHJob" />
    
    <bean id="sshJobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 调用的类 -->
        <property name="targetObject">
            <ref bean="sshJob"/>
        </property>
        <!-- 调用类中的方法 -->   
        <property name="targetMethod">
            <value>work</value>
        </property>
    </bean>
    
    <bean id="sshJobDoTime" class="org.springframework.scheduling.quartz.CronTriggerBean">  
        <!-- 要触发的任务 -->  
        <property name="jobDetail">
            <ref bean="sshJobTask"/>  
        </property>
        <!-- cron表达式,详见 http://www.quartz-scheduler.org/docs/tutorials/crontrigger.html -->
        <property name="cronExpression">
            <value>0 0 1 ? * *</value>  
        </property>
    </bean>
    
   	<!-- lazy-init="false"则容器启动就会执行调度程序  -->
   	<bean id="startQuartz" lazy-init="true" autowire="no" 
   			class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <property name="triggers">  
            <list>
                <ref bean="sshJobDoTime"/>
            </list>
        </property>
    </bean>
    
</beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值