spring 里配置 quartz

本文介绍了一个使用Spring框架和Quartz实现的定时任务系统。该系统定义了两种类型的定时任务:一种是通过实现Job接口并注入上下文来执行自定义逻辑;另一种是通过调用特定服务类的方法来触发任务执行。配置文件中详细展示了如何设置任务细节、触发器及调度器。

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

package hvp.spring.quartz.bean;

import java.util.Map;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;

public class MyJob implements Job
{
    
    @SuppressWarnings(
"unchecked")
    
public void execute(JobExecutionContext jec) throws JobExecutionException
    
{
        Map dataMap 
= jec.getJobDetail().getJobDataMap();
        String size 
= (String) dataMap.get("size");
        
// 得到Spring的ApplicationContext,可以访问Spring里的任何Bean了.
        ApplicationContext ac = (ApplicationContext) dataMap
                .get(
"applicationContext");
        System.out.println(size 
+ ":工作里" + ac.toString());
    }

}
package hvp.spring.quartz.bean;

public class MyService
{
    
public void doJob()
    
{
        System.out.println(
"in MyService.doJob().");
    }

}
package hvp.spring.quartz.bean;

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

public class TestQuartx
{   
    
public static void main(String[] args){
        String configPath 
= "hvp/spring/quartz/bean/quartzBean.xml";
        ApplicationContext ctx 
= new ClassPathXmlApplicationContext(configPath);
        ctx.getBean(
"scheduler");
    }

}
<?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"
>
    
<bean name="jobDetail"
        class
="org.springframework.scheduling.quartz.JobDetailBean">
        
<property name="jobClass"
            value
="hvp.spring.quartz.bean.MyJob" />
        
<property name="jobDataAsMap">
            
<map>
                
<entry key="size" value="10" />
            
</map>
        
</property>
        
<property name="applicationContextJobDataKey"
            value
="applicationContext" />
    
</bean>
    
<bean id="jobDetail_1"
        class
="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        
<property name="targetObject" ref="myService" />
        
<property name="targetMethod" value="doJob" />
        
<property name="concurrent" value="false" />
    
</bean>
    
<bean id="myService" class="hvp.spring.quartz.bean.MyService" />
    
<bean id="simpleTrigger"
        class
="org.springframework.scheduling.quartz.SimpleTriggerBean">
        
<property name="jobDetail" ref="jobDetail" />
        
<property name="startDelay" value="1000" />
        
<property name="repeatInterval" value="1000" />
        
<property name="repeatCount" value="1000" />
        
<property name="jobDataAsMap">
            
<map>
                
<entry key="count" value="10" />
            
</map>
        
</property>
    
</bean>
    
<bean id="checkImagesTrigger"
        class
="org.springframework.scheduling.quartz.CronTriggerBean">
        
<property name="jobDetail" ref="jobDetail_1" />
        
<property name="cronExpression" value="0/5 * * * * ?" />
    
</bean>
    
<bean id="scheduler"
        class
="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        
<property name="triggers">
            
<list>
                
<ref bean="checkImagesTrigger" />
                
<ref bean="simpleTrigger" />
            
</list>
        
</property>
        
<property name="schedulerContextAsMap">
            
<map>
                
<entry key="timeout" value="30" />
            
</map>
        
</property>
        
<property name="quartzProperties">
            
<props>
                
<prop key="org.quartz.threadPool.class">
                    org.quartz.simpl.SimpleThreadPool
                
</prop>
                
<prop key="org.quartz.threadPool.threadCount">10</prop>
            
</props>
        
</property>
    
</bean>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值