`
Donald_Draper
  • 浏览: 981069 次
社区版块
存档分类
最新评论

Spring与Quartz集成-源码分析

阅读更多
在阅读以下文章之前,如果对Quartz任务调度不是很熟悉,请看以下文章
Quartz任务调度分析:http://donald-draper.iteye.com/blog/2323118
Spring与Quartz集成详解:http://donald-draper.iteye.com/blog/2323591
ThreadLocal彻底理解:http://blog.csdn.net/lufeng20/article/details/24314381/
public class SchedulerFactoryBean extends SchedulerAccessor
    implements FactoryBean, BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean, SmartLifecycle
{

    public SchedulerFactoryBean()
    {
       //Quartz的标准调度工场类
        schedulerFactoryClass = org/quartz/impl/StdSchedulerFactory;
        jobFactorySet = false;
        autoStartup = true;
        startupDelay = 0;
        phase = 2147483647;
        exposeSchedulerInRepository = false;
        waitForJobsToCompleteOnShutdown = false;
    }
    //设置调度器工厂类
        public void setSchedulerFactoryClass(Class schedulerFactoryClass)
    {
        Assert.isAssignable(org/quartz/SchedulerFactory, schedulerFactoryClass);
        this.schedulerFactoryClass = schedulerFactoryClass;
    }
    //设置调度器Name
    public void setSchedulerName(String schedulerName)
    {
        this.schedulerName = schedulerName;
    }
    //Quartz属性
    public void setQuartzProperties(Properties quartzProperties)
    {
        this.quartzProperties = quartzProperties;
    }
    //设置任务执行器
    public void setTaskExecutor(Executor taskExecutor)
    {
        this.taskExecutor = taskExecutor;
    }
    //设置beanName
      public void setBeanName(String name)
    {
        if(schedulerName == null)
            schedulerName = name;
    }
    //设置应用上下文
    public void setApplicationContext(ApplicationContext applicationContext)
    {
        this.applicationContext = applicationContext;
    }
    //设置job工厂
    public void setJobFactory(JobFactory jobFactory)
    {
        this.jobFactory = jobFactory;
        jobFactorySet = true;
    }
    public void setAutoStartup(boolean autoStartup)
    {
        this.autoStartup = autoStartup;
    }
     public void afterPropertiesSet()
        throws Exception
    {
        SchedulerFactory schedulerFactory;
        if(dataSource == null && nonTransactionalDataSource != null)
            dataSource = nonTransactionalDataSource;
        if(applicationContext != null && resourceLoader == null)
            resourceLoader = applicationContext;
	//实例化调度器工厂
        schedulerFactory = (SchedulerFactory)BeanUtils.instantiateClass(schedulerFactoryClass);
	//初始化调度器工厂
        initSchedulerFactory(schedulerFactory);
        if(resourceLoader != null)
            configTimeResourceLoaderHolder.set(resourceLoader);
        if(taskExecutor != null)
            configTimeTaskExecutorHolder.set(taskExecutor);
        if(dataSource != null)
            configTimeDataSourceHolder.set(dataSource);
        if(nonTransactionalDataSource != null)
            configTimeNonTransactionalDataSourceHolder.set(nonTransactionalDataSource);
	//创建调度器
        scheduler = createScheduler(schedulerFactory, schedulerName);
	 //初始化调度器上下文
        populateSchedulerContext();
        if(!jobFactorySet && !(scheduler instanceof RemoteScheduler))
            jobFactory = new AdaptableJobFactory();
        if(jobFactory != null)
        {
            if(jobFactory instanceof SchedulerContextAware)
                ((SchedulerContextAware)jobFactory).setSchedulerContext(scheduler.getContext());
           //设置调度器job工厂类
            scheduler.setJobFactory(jobFactory);
        }
        if(resourceLoader != null)
            configTimeResourceLoaderHolder.remove();
        if(taskExecutor != null)
            configTimeTaskExecutorHolder.remove();
        if(dataSource != null)
            configTimeDataSourceHolder.remove();
        if(nonTransactionalDataSource != null)
            configTimeNonTransactionalDataSourceHolder.remove();
        break MISSING_BLOCK_LABEL_334;
        if(resourceLoader != null)
            configTimeResourceLoaderHolder.remove();
        if(taskExecutor != null)
            configTimeTaskExecutorHolder.remove();
        if(dataSource != null)
            configTimeDataSourceHolder.remove();
        if(nonTransactionalDataSource != null)
            configTimeNonTransactionalDataSourceHolder.remove();
	//注册监听器
        registerListeners();
	//注册job&Trriger
        registerJobsAndTriggers();
        return;
    }
    //初始化调度工厂类
     private void initSchedulerFactory(SchedulerFactory schedulerFactory)
        throws SchedulerException, IOException
    {
         //判断标准调度工厂的属性是否完整
        if(!(schedulerFactory instanceof StdSchedulerFactory))
            if(configLocation != null || quartzProperties != null || taskExecutor != null || dataSource != null)
                throw new IllegalArgumentException((new StringBuilder()).append("StdSchedulerFactory required for applying Quartz properties: ").append(schedulerFactory).toString());
        Properties mergedProps = new Properties();
        if(resourceLoader != null)
	    //设置classLoadHelper类
            mergedProps.setProperty("org.quartz.scheduler.classLoadHelper.class", org/springframework/scheduling/quartz/ResourceLoaderClassLoadHelper.getName());
        if(taskExecutor != null)
        {   //设置线程池类
            mergedProps.setProperty("org.quartz.threadPool.class", org/springframework/scheduling/quartz/LocalTaskExecutorThreadPool.getName());
        } else
        {
            mergedProps.setProperty("org.quartz.threadPool.class", org/quartz/simpl/SimpleThreadPool.getName());
            mergedProps.setProperty("org.quartz.threadPool.threadCount", Integer.toString(10));
        }
        if(configLocation != null)
        {
            PropertiesLoaderUtils.fillProperties(mergedProps, configLocation);
        }
	//调度器工厂属性合并重组
        CollectionUtils.mergePropertiesIntoMap(quartzProperties, mergedProps);
        if(dataSource != null)
            mergedProps.put("org.quartz.jobStore.class", org/springframework/scheduling/quartz/LocalDataSourceJobStore.getName());
        if(schedulerName != null)
            mergedProps.put("org.quartz.scheduler.instanceName", schedulerName);
	//标准调度工厂初始化
        ((StdSchedulerFactory)schedulerFactory).initialize(mergedProps);
    }
    //创建调度器
     protected Scheduler createScheduler(SchedulerFactory schedulerFactory, String schedulerName)
        throws SchedulerException
    {
        Thread currentThread;
        ClassLoader threadContextClassLoader;
        boolean overrideClassLoader;
        currentThread = Thread.currentThread();
        threadContextClassLoader = currentThread.getContextClassLoader();
        overrideClassLoader = resourceLoader != null && !resourceLoader.getClassLoader().equals(threadContextClassLoader);
        if(overrideClassLoader)
	    //如果资源加载器不为空,则设置当前线程上下文加载器
            currentThread.setContextClassLoader(resourceLoader.getClassLoader());
	//获取调度器仓库实例
        SchedulerRepository repository = SchedulerRepository.getInstance();
        Scheduler scheduler1;
        synchronized(repository)
        {
	    //从调度器仓库寻找schedulerName的调度器
            Scheduler existingScheduler = schedulerName == null ? null : repository.lookup(schedulerName);
	    //从调度器工厂获取调度器
            Scheduler newScheduler = schedulerFactory.getScheduler();
            if(newScheduler == existingScheduler)
                throw new IllegalStateException((new StringBuilder()).append("Active Scheduler of name '").append(schedulerName).append("' already registered ").append("in Quartz SchedulerRepository. Cannot create a new Spring-managed Scheduler of the same name!").toString());
            if(!exposeSchedulerInRepository)
	        //从仓库中移除newScheduler
                SchedulerRepository.getInstance().remove(newScheduler.getSchedulerName());
            scheduler1 = newScheduler;
        }
        if(overrideClassLoader)
            currentThread.setContextClassLoader(threadContextClassLoader);
        return scheduler1;
    }
    //初始化调度器上下文,SchedulerContext,实际上是Map
     private void populateSchedulerContext()
        throws SchedulerException
    {
        //如果schedulerContextMap不为空,则将schedulerContextMap放入调度器上下文中
        if(schedulerContextMap != null)
            scheduler.getContext().putAll(schedulerContextMap);
        //如果applicationContextSchedulerContextKey不为空,
	//则将applicationContext放入到调度器上下文中
        if(applicationContextSchedulerContextKey != null)
        {
            if(applicationContext == null)
                throw new IllegalStateException("SchedulerFactoryBean needs to be set up in an ApplicationContext to be able to handle an 'applicationContextSchedulerContextKey'");
            scheduler.getContext().put(applicationContextSchedulerContextKey, applicationContext);
        }
    }
    //启动调度器
 protected void startScheduler(final Scheduler scheduler, final int startupDelay)
        throws SchedulerException
    {
        if(startupDelay <= 0)
        {
            logger.info("Starting Quartz Scheduler now");
            scheduler.start();
        } else
        {
            if(logger.isInfoEnabled())
                logger.info((new StringBuilder()).append("Will start Quartz Scheduler [").append(scheduler.getSchedulerName()).append("] in ").append(startupDelay).append(" seconds").toString());
            Thread schedulerThread = new Thread() {

                public void run()
                {
                    try
                    {
		        //如果延迟加载,则休眠startupDelay秒钟
                        Thread.sleep(startupDelay * 1000);
                    } 
                    if(logger.isInfoEnabled())
                        logger.info((new StringBuilder()).append("Starting Quartz Scheduler now, after delay of ").append(startupDelay).append(" seconds").toString());
                    try
                    {
                        scheduler.start();
                    }
                }
                final int val$startupDelay;
                final Scheduler val$scheduler;
                final SchedulerFactoryBean this$0;           
            {
                this.this$0 = SchedulerFactoryBean.this;
                startupDelay = i;
                scheduler = scheduler1;
                super();
            }
            };
            schedulerThread.setName((new StringBuilder()).append("Quartz Scheduler [").append(scheduler.getSchedulerName()).append("]").toString());
            schedulerThread.setDaemon(true);
            schedulerThread.start();
        }
    }

    public Scheduler getScheduler()
    {
        return scheduler;
    }
   //返回bean对象为Scheduler
    public Scheduler getObject()
    {
        return scheduler;
    }

    public Class getObjectType()
    {
        return scheduler == null ? org/quartz/Scheduler : scheduler.getClass();
    }
   //是否是单例模式,是
    public boolean isSingleton()
    {
        return true;
    }
    //启动
    public void start()
        throws SchedulingException
    {
        if(scheduler != null)
            try
            {
                startScheduler(scheduler, startupDelay);
            }
            catch(SchedulerException ex)
            {
                throw new SchedulingException("Could not start Quartz Scheduler", ex);
            }
    }
   //关闭操作
    public void stop()
        throws SchedulingException
    {
        if(scheduler != null)
            try
            {
                scheduler.standby();
            }
    }
   //关闭操作
    public void stop(Runnable callback)
        throws SchedulingException
    {
        stop();
        callback.run();
    }
   //获取调度器是否运行信息
    public boolean isRunning()
        throws SchedulingException
    {
        if(scheduler == null)
            break MISSING_BLOCK_LABEL_28;
        return !scheduler.isInStandbyMode();
        return false;

    }
    //bean销毁,关闭调度器
    public void destroy()
        throws SchedulerException
    {
        logger.info("Shutting down Quartz Scheduler");
        scheduler.shutdown(waitForJobsToCompleteOnShutdown);
    }
    public volatile Object getObject()
        throws Exception
    {
        return getObject();
    }
    public static final String PROP_THREAD_COUNT = "org.quartz.threadPool.threadCount";
    public static final int DEFAULT_THREAD_COUNT = 10;
    private static final ThreadLocal configTimeResourceLoaderHolder = new ThreadLocal();
    private static final ThreadLocal configTimeTaskExecutorHolder = new ThreadLocal();
    private static final ThreadLocal configTimeDataSourceHolder = new ThreadLocal();
    private static final ThreadLocal configTimeNonTransactionalDataSourceHolder = new ThreadLocal();
    private Class schedulerFactoryClass;//调度工厂类
    private String schedulerName;//调度器名
    private Resource configLocation;
    private Properties quartzProperties;//quartz属性
    private Executor taskExecutor;//任务执行器
    private DataSource dataSource;//数据源
    private DataSource nonTransactionalDataSource;//无事务数据源
    private Map schedulerContextMap;//调度器上下文
    private ApplicationContext applicationContext;//应用上下文
    private String applicationContextSchedulerContextKey;
    private JobFactory jobFactory;//job工厂
    private boolean jobFactorySet;
    private boolean autoStartup;
    private int startupDelay;//延时加载
    private int phase;
    private boolean exposeSchedulerInRepository;//是否将调度器存储在调度器仓库
    private boolean waitForJobsToCompleteOnShutdown;//是否等job完成,才关闭调度器
    private Scheduler scheduler;//调度器

}

//SchedulerAccessor
public abstract class SchedulerAccessor
    implements ResourceLoaderAware
{
    public SchedulerAccessor()
    {
        overwriteExistingJobs = false;
        if(jobKeyClass == null && logger.isInfoEnabled())
            logger.info("Spring's Quartz 1.x support is deprecated - please upgrade to Quartz 2.0+");
    }
     protected void registerListeners()
        throws SchedulerException
    {
        Object target;
        boolean quartz2;
        try
        {
	    //调度器获取监听管理器方法
            Method getListenerManager = org/quartz/Scheduler.getMethod("getListenerManager", new Class[0]);
	    //获取调度器监听管理器
            target = ReflectionUtils.invokeMethod(getListenerManager, getScheduler());
            quartz2 = true;
        }
        catch(NoSuchMethodException ex)
        {
            target = getScheduler();
            quartz2 = false;
        }
        Class targetClass = target.getClass();
        try
        {
            if(schedulerListeners != null)
            {
                Method addSchedulerListener = targetClass.getMethod("addSchedulerListener", new Class[] {
                    org/quartz/SchedulerListener
                });
                SchedulerListener aschedulerlistener[] = schedulerListeners;
                int k = aschedulerlistener.length;
                for(int l1 = 0; l1 < k; l1++)
                {
                    SchedulerListener listener = aschedulerlistener[l1];
		    //添加调度器监听器
                    ReflectionUtils.invokeMethod(addSchedulerListener, target, new Object[] {
                        listener
                    });
                }

            }
	    //添加全局job监听器
            if(globalJobListeners != null)
            {
                Method addJobListener;
                if(quartz2)
		    //添加job监听器方法
                    addJobListener = targetClass.getMethod("addJobListener", new Class[] {
                        org/quartz/JobListener, java/util/List
                    });
                else
		   //添加全局job监听器方法
                    addJobListener = targetClass.getMethod("addGlobalJobListener", new Class[] {
                        org/quartz/JobListener
                    });
                JobListener ajoblistener1[] = globalJobListeners;
                int l = ajoblistener1.length;
                for(int i2 = 0; i2 < l; i2++)
                {
                    JobListener listener = ajoblistener1[i2];
                    if(quartz2)
                    {
                        List emptyMatchers = new LinkedList();
			//添加job监听器
                        ReflectionUtils.invokeMethod(addJobListener, target, new Object[] {
                            listener, emptyMatchers
                        });
                    } else
                    {
		        //添加全局job监听器
                        ReflectionUtils.invokeMethod(addJobListener, target, new Object[] {
                            listener
                        });
                    }
                }

            }
            if(jobListeners != null)
            {
                JobListener ajoblistener[] = jobListeners;
                int i = ajoblistener.length;
                for(int i1 = 0; i1 < i; i1++)
                {
                    JobListener listener = ajoblistener[i1];
                    if(quartz2)
                        throw new IllegalStateException("Non-global JobListeners not supported on Quartz 2 - manually register a Matcher against the Quartz ListenerManager instead");
                    //添加job监听器
		    getScheduler().addJobListener(listener);
                }

            }
	    //添加全局触发器监听器
            if(globalTriggerListeners != null)
            {
                Method addTriggerListener;
                if(quartz2)
		    //添加触发器监听器方法
                    addTriggerListener = targetClass.getMethod("addTriggerListener", new Class[] {
                        org/quartz/TriggerListener, java/util/List
                    });
                else
		    //添加全局触发器监听器方法
                    addTriggerListener = targetClass.getMethod("addGlobalTriggerListener", new Class[] {
                        org/quartz/TriggerListener
                    });
                TriggerListener atriggerlistener1[] = globalTriggerListeners;
                int j1 = atriggerlistener1.length;
                for(int j2 = 0; j2 < j1; j2++)
                {
                    TriggerListener listener = atriggerlistener1[j2];
                    if(quartz2)
                    {
                        List emptyMatchers = new LinkedList();
                         //添加触发器监听器
                        ReflectionUtils.invokeMethod(addTriggerListener, target, new Object[] {
                            listener, emptyMatchers
                        });
                    } else
                    {
		         //添加全局触发器监听器
                        ReflectionUtils.invokeMethod(addTriggerListener, target, new Object[] {
                            listener
                        });
                    }
                }

            }
            if(triggerListeners != null)
            {
                TriggerListener atriggerlistener[] = triggerListeners;
                int j = atriggerlistener.length;
                for(int k1 = 0; k1 < j; k1++)
                {
                    TriggerListener listener = atriggerlistener[k1];
                    if(quartz2)
                        throw new IllegalStateException("Non-global TriggerListeners not supported on Quartz 2 - manually register a Matcher against the Quartz ListenerManager instead");
                    //添加触发器监听器
		    getScheduler().addTriggerListener(listener);
                }

            }
        }
        catch(NoSuchMethodException ex)
        {
            throw new IllegalStateException((new StringBuilder()).append("Expected Quartz API not present: ").append(ex).toString());
        }
    }
    //添加job和trriger
    protected void registerJobsAndTriggers()
        throws SchedulerException
    {
        TransactionStatus transactionStatus = null;
        if(transactionManager != null)
            transactionStatus = transactionManager.getTransaction(new DefaultTransactionDefinition());
        try
        {
            if(jobSchedulingDataLocations != null)
            {
                ClassLoadHelper clh = new ResourceLoaderClassLoadHelper(resourceLoader);
                clh.initialize();
                XMLSchedulingDataProcessor dataProcessor = new XMLSchedulingDataProcessor(clh);
                String as[] = jobSchedulingDataLocations;
                int i = as.length;
                for(int j = 0; j < i; j++)
                {
                    String location = as[j];
                    dataProcessor.processFileAndScheduleJobs(location, getScheduler());
                }

            }
            if(jobDetails != null)
            {
                JobDetail jobDetail;
		//如果jobDetails不为空,则添加jobDetails到调度器
                for(Iterator iterator = jobDetails.iterator(); iterator.hasNext(); addJobToScheduler(jobDetail))
                    jobDetail = (JobDetail)iterator.next();

            } else
            {
                jobDetails = new LinkedList();
            }
            if(calendars != null)
            {
                String calendarName;
                Calendar calendar;
                for(Iterator iterator1 = calendars.keySet().iterator(); iterator1.hasNext(); getScheduler().addCalendar(calendarName, calendar, true, true))
                {
                    calendarName = (String)iterator1.next();
                    calendar = (Calendar)calendars.get(calendarName);
                }

            }
            if(triggers != null)
            {
	        //添加TrrigerWapprer触发任务到调度器
                Trigger trigger;
                for(Iterator iterator2 = triggers.iterator(); iterator2.hasNext(); addTriggerToScheduler(trigger))
                    trigger = (Trigger)iterator2.next();

            }
        }
        catch(Throwable ex)
        {
            if(transactionStatus != null)
                try
                {
                    transactionManager.rollback(transactionStatus);
                }
                catch(TransactionException tex)
                {
                    logger.error("Job registration exception overridden by rollback exception", ex);
                    throw tex;
                }
            if(ex instanceof SchedulerException)
                throw (SchedulerException)ex;
            if(ex instanceof Exception)
                throw new SchedulerException((new StringBuilder()).append("Registration of jobs and triggers failed: ").append(ex.getMessage()).toString(), ex);
            else
                throw new SchedulerException((new StringBuilder()).append("Registration of jobs and triggers failed: ").append(ex.getMessage()).toString());
        }
        if(transactionStatus != null)
            transactionManager.commit(transactionStatus);
    }
   //添加jobDetail到调度器
   private boolean addJobToScheduler(JobDetail jobDetail)
        throws SchedulerException
    {
        if(overwriteExistingJobs || !jobDetailExists(jobDetail))
        {
            getScheduler().addJob(jobDetail, true);
            return true;
        } else
        {
            return false;
        }
    }
    //添加TrrigerWapprer触发任务到调度器
    private boolean addTriggerToScheduler(Trigger trigger)
        throws SchedulerException
    {
        boolean triggerExists = triggerExists(trigger);
        if(!triggerExists || overwriteExistingJobs)
        {
            JobDetail jobDetail = findJobDetail(trigger);
            if(jobDetail != null && !jobDetails.contains(jobDetail) && addJobToScheduler(jobDetail))
                jobDetails.add(jobDetail);
            if(!triggerExists)
                try
                {
		    //关键在这里,调度触发任务
                    getScheduler().scheduleJob(trigger);
                }
                catch(ObjectAlreadyExistsException ex)
                {
                    if(logger.isDebugEnabled())
                        logger.debug((new StringBuilder()).append("Unexpectedly found existing trigger, assumably due to cluster race condition: ").append(ex.getMessage()).append(" - can safely be ignored").toString());
                    if(overwriteExistingJobs)
		        //如果允许重写存在的job,则重新调度触发任务
                        rescheduleJob(trigger);
                }
            else
                rescheduleJob(trigger);
            return true;
        } else
        {
            return false;
        }
    }
     //根据trigger获取JobDetail
     private JobDetail findJobDetail(Trigger trigger)
    {
        if(trigger instanceof JobDetailAwareTrigger)
            return ((JobDetailAwareTrigger)trigger).getJobDetail();
        Map jobDataMap = (Map)ReflectionUtils.invokeMethod(org/quartz/Trigger.getMethod("getJobDataMap", new Class[0]), trigger);
        return (JobDetail)jobDataMap.remove("jobDetail");
    }
    //设置JobDetail
    public transient void setJobDetails(JobDetail jobDetails[])
    {
        this.jobDetails = new ArrayList(Arrays.asList(jobDetails));
    }
    //设置Trigger
      public transient void setTriggers(Trigger triggers[])
    {
        this.triggers = Arrays.asList(triggers);
    }
    //设置SchedulerListener
    public transient void setSchedulerListeners(SchedulerListener schedulerListeners[])
    {
        this.schedulerListeners = schedulerListeners;
    }

    protected abstract Scheduler getScheduler();
    private static Class jobKeyClass;
    private static Class triggerKeyClass;
    protected final Log logger = LogFactory.getLog(getClass());
    private boolean overwriteExistingJobs;
    private String jobSchedulingDataLocations[];
    private List jobDetails;
    private Map calendars;
    private List triggers;
    private SchedulerListener schedulerListeners[];
    private JobListener globalJobListeners[];
    private JobListener jobListeners[];
    private TriggerListener globalTriggerListeners[];
    private TriggerListener triggerListeners[];
    private PlatformTransactionManager transactionManager;
    protected ResourceLoader resourceLoader;
    static 
    {
        try
        {
            jobKeyClass = ClassUtils.forName("org.quartz.JobKey", org/springframework/scheduling/quartz/SchedulerAccessor.getClassLoader());
            triggerKeyClass = ClassUtils.forName("org.quartz.TriggerKey", org/springframework/scheduling/quartz/SchedulerAccessor.getClassLoader());
        }
    }
}

//资源加载器
public interface ResourceLoaderAware
    extends Aware
{
    public abstract void setResourceLoader(ResourceLoader resourceloader);
}

//事务状态
public interface TransactionStatus
    extends SavepointManager
{
    public abstract boolean isNewTransaction();
    public abstract boolean hasSavepoint();
    public abstract void setRollbackOnly();
    public abstract boolean isRollbackOnly();
    public abstract boolean isCompleted();
}

//bean工场
public interface FactoryBean
{
    public abstract Object getObject()
        throws Exception;
    public abstract Class getObjectType();
    public abstract boolean isSingleton();
}

//beanName设置
public interface BeanNameAware
    extends Aware
{
    public abstract void setBeanName(String s);
}

//应用上下文
public interface ApplicationContextAware
    extends Aware
{
    public abstract void setApplicationContext(ApplicationContext applicationcontext)
        throws BeansException;
}

//bean初始化
public interface InitializingBean
{
    public abstract void afterPropertiesSet()
        throws Exception;
}

//bean销毁
public interface DisposableBean
{
    public abstract void destroy()
        throws Exception;
}

//生命周期管理
public interface SmartLifecycle
    extends Lifecycle, Phased
{
    public abstract boolean isAutoStartup();
    public abstract void stop(Runnable runnable);
}

//job工厂
public interface JobFactory
{
    //TriggerFiredBundle,触发任务包装类,Scheduler,调度器
    public abstract Job newJob(TriggerFiredBundle triggerfiredbundle, Scheduler scheduler)
        throws SchedulerException;
}

// JobDetailAwareTrigger-Spring
public interface JobDetailAwareTrigger
{
    public abstract JobDetail getJobDetail();
    public static final String JOB_DETAIL_KEY = "jobDetail";
}


总结:
Spring与Quartz的整合关键在于SchedulerFactoryBean,我们在XML中配SchedulerFactoryBean时候,如要需要任务持久化,我们需要配置datasource,配置Quartz属性(线程池, 调度器名,线程池优先级,持久化类等信息),配置jobDetails,配置触发任务trrigers(TrrigerWapper),延迟加载属性,监听器,重写属性;SchedulerFactoryBean实现了InitializingBean,在SchedulerFactoryBean属性初始化完毕,调用afterPropertiesSet方法,完成数据源,Quartz-schedulerFactory初始化,创建调度器,初始化调度器上下文,注册监听器,注册job,Trrigers。
0
0
分享到:
评论

相关推荐

    spring2与quartz在Web整合

    在将 Spring 2 与 Quartz 整合到 Web 应用中时,主要涉及以下几个关键知识点: 1. **Spring 2 的核心概念**:Spring 2 提供了 IoC(Inversion of Control,控制反转)和 AOP(Aspect-Oriented Programming,面向切...

    spring-context-support源码

    源码分析的过程中,我们需要注意Spring如何通过依赖注入(Dependency Injection,DI)和面向切面编程(Aspect-Oriented Programming,AOP)来实现这些功能。例如,通过`@Autowired`注解可以自动装配所需的bean,而`@...

    spring-framework-3.2.x .zip----源码

    - Spring 3.2.x 支持与各种框架的集成,如 MyBatis、Quartz、Struts 等,源码中可以看到这些集成的实现细节。 通过对 Spring Framework 3.2.x 源码的深入研究,我们可以更好地理解这个框架的工作原理,提高我们的...

    定时调度-Spring集成Quartz

    **Spring与Quartz集成** Spring通过`org.springframework.scheduling.quartz`模块提供了与Quartz的集成。集成主要涉及以下几个核心组件: 1. **SchedulerFactoryBean**:这是Spring提供的一个工厂bean,用于配置和...

    Spring整合Quartz定时发送邮件

    这个场景的应用是使用Quartz来安排任务,而Spring则提供了集成环境和邮件服务支持。 首先,Quartz是一个开源的作业调度框架,它允许开发者定义、调度和执行任务。在Java应用程序中,Quartz可以用来在特定时间执行...

    spring quartz 表达式在线生成器

    2. **Spring与Quartz集成**:介绍如何在Spring应用中配置Quartz,包括使用`@EnableScheduling`注解开启调度,以及如何定义`@Scheduled`注解的任务。 3. **Cron表达式**:详细讲解Cron表达式的组成部分和语法,以及...

    Spring-Quartz

    2. **Spring与Quartz的集成**: Spring通过`org.springframework.scheduling.quartz`包提供了对Quartz的全面支持。通过Spring的bean定义,我们可以声明式地配置Job和Trigger,避免了直接操作Quartz的API。 3. **...

    spring学习系列 -- 定时器三 两种定时器的比较

    本文将深入探讨Spring提供的两种定时器——`Quartz`和`Spring Task`,并进行对比分析。 首先,让我们了解`Quartz`。Quartz是一个开源的作业调度框架,它允许应用程序在指定的时间执行任务。Quartz提供了丰富的API,...

    ssm框架+quartz2.2Jar包

    这个压缩包包含的是SSM框架与Quartz 2.2版本的集成,用于实现定时任务管理。接下来,我们将深入探讨SSM框架的各个组件以及Quartz的相关知识点。 **Spring框架** 是一个全面的、开源的应用程序框架,主要负责依赖...

    Quartz与Spring整合练习源代码

    Quartz是一款开源的作业调度框架,它允许程序在特定时间执行特定任务,广泛应用于各种需要自动化定时任务的场景。而Spring是Java领域中一个流行的...这是一个很好的学习资源,对于理解Quartz和Spring的集成有极大帮助。

    quartz2.2.1 spring3.1.1

    Quartz 2.2.1 和 Spring 3.1.1 是两个在企业级Java应用开发中常用的开源框架。Quartz 是一个强大的、...通过源码分析,开发者可以更好地理解这两个框架的内部机制,从而更有效地利用它们的功能来满足特定的业务需求。

    Spring定时器quartz

    这篇博客可能探讨了如何在Spring框架中集成Quartz,以实现灵活、可扩展的任务调度。 Quartz是一个开源的作业调度框架,支持 cron 表达式和其他定时规则来安排任务。它具有强大的功能,例如并发性控制、持久化作业...

    spring-framework-4.2.4.RELEASE.rar

    6. **源码分析**: Spring源码是学习框架设计思想和Java高级技术的好资料。通过阅读源码,可以深入了解依赖注入、AOP、事件驱动等方面的实现。 7. **文档**: 完整的文档包括用户指南、参考手册和API文档,有助于...

    Spring Boot实战派(源码)

    《Spring Boot实战派》源码提供了丰富的学习材料,旨在帮助开发者深入理解并...通过分析《Spring Boot实战派》源码,读者不仅可以了解上述技术点,还能学习到如何在实际项目中应用这些技术,提升开发效率和代码质量。

    quartz-2.2 需要的jar以及src源代码

    - 它也可以与其他框架集成,如Spring,提供更方便的使用方式。 7. **注意事项** - 调度任务时,应合理设计触发策略,避免过多的任务导致系统负载过高。 - 对于长时间运行的任务,要考虑任务之间的隔离,防止阻塞...

    Spring quartz

    标题 "Spring Quartz" 提到的是将 Spring 框架与 Quartz 定时任务调度库集成的应用。Quartz 是一个开源的作业调度框架,允许开发者安排任务在特定时间执行,而 Spring 框架则提供了方便的依赖注入和管理组件的能力。...

    spring3.x企业应用实战开发-源码

    7. **Spring与其它框架的整合**:如Spring与Struts、Hibernate、Quartz等的协同工作。源码中可以看到如何配置和使用这些框架,实现各组件间的无缝集成。 8. **国际化(I18N)**:Spring提供支持多语言环境的功能,...

    基于Spring的Quartz动态定时任务增删改查源码.rar

    本资源"基于Spring的Quartz动态定时任务增删改查源码.rar"提供了一套完整的示例,帮助开发者了解如何在Spring环境中集成Quartz来实现动态管理定时任务。 首先,Quartz的核心概念包括Job(任务)、Trigger(触发器)...

    147-生鲜交易系统--源码.zip

    - 异步处理和定时任务,如使用Spring Task或Quartz。 - 错误处理和日志记录机制。 通过深入学习这个生鲜交易系统的源码,可以了解一个完整的电商应用是如何运作的,对Spring Boot框架有更深入的理解,并掌握如何...

    Spring中使用Quartz(二)

    2. **Spring与Quartz集成** - 首先,我们需要在项目中引入Quartz的相关依赖,通常通过Maven或Gradle进行管理。 - 创建`quartz.properties`配置文件,定义Quartz的运行参数,如数据库连接、job存储方式等。 - 在...

Global site tag (gtag.js) - Google Analytics