quartz石英任务

定时任务:
      quartz:的几大核心概念

Job:任务(我们需要完成的事情);【要炸大本营】
JobDetail:任务详情(任务怎么做,谁来做);
        【执行任务需要的对象,数据信息等】【张三,50吨TNT】
    quartz:为了并发执行;
    Job(定义任务怎么执行的类)---JobDetail(当次执行的实例);

Trigger:触发器;用来执行任务的;【炸药的引信】
Scheduler:调度器;调度任务;【帮我们在指定时间触发trigger】【中控台】
                scheduler.scheduleJob(job, trigger);

步骤:
    1)、导入相关依赖;

        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
        </dependency>
   2.1)配置版

@Service
public class QuartzService {
    
    public void hello(){
        System.out.println("hello world...");
    }
    
    public void msg(){
        System.out.println("hello msg...");
    }
}


<!-- 1、定义一个JobDetail -->
    <bean id="jobDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- targetObject目标对象;ref指向目标对象的id -->
        <property name="targetObject" ref="quartzService" />
        <!-- targetMethod目标方法 -->
        <property name="targetMethod" value="hello" />
    </bean>
    <!-- 2、创建一个触发器关联任务 -->
    <bean id="cronTrigger"
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!-- jobDetail:传入需要执行的任务的jobDetail -->
        <property name="jobDetail" ref="jobDetail" />
        <!-- 指定cronExpression;cron表达式来指定什么时候执行 0(秒) 0(分) 6(时) *(日) *(月) ?(周) *(年【可以省略】) -->
        <property name="cronExpression" value="* * * * * ?" />
    </bean>
    <!--3、创建Scheduler(调度中心),负责调度每一个触发器 -->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <!-- 可以使用多个触发器同时生效 -->
                <ref bean="cronTrigger" />
            </list>
        </property>
    </bean>

2.2)注解版

@EnableScheduling  //开启调度功能;
//@EnableXXX只需要出现一次即可
@Service
public class QuartzService {
    
    //配置版会进行任务调度
    public void hello(){
        System.out.println("hello world...");
    }
    
    //这是一个定时任务
    //注解版的任务调度起作用必须开启注解版的任务调度功能;
    @Scheduled(cron="* * * * * ?")
    public void msg(){
        System.out.println("hello msg...");
    }
}

3)写法实例

(cron="7 8 9 10 11 ?"):11月10日9点8分7秒启动一次这个任务
0 0 10,14,16 * * ? 每天上午10点,下午2点,4 
0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时 
0 0 12 ? * WED 表示每个星期三中午12 
"0 0 12 * * ?" 每天中午12点触发 
"0 15 10 ? * *" 每天上午10:15触发 
"0 15 10 * * ?" 每天上午10:15触发 
"0 15 10 * * ? *" 每天上午10:15触发 
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发 
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发 
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发 
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:102:44触发 
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发 
"0 15 10 15 * ?" 每月15日上午10:15触发 
"0 15 10 L * ?" 每月最后一日的上午10:15触发 
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发 
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发


如果觉得麻烦还有专门的转换器进行下载

链接:https://pan.baidu.com/s/1bcmTdT7Th7B1DAcfr0M3Iw 密码:lh15

阅读更多

SSH + Quartz石英定时器

09-28

原先的SSH 配置都可以 Tomcat启动一切都OK 但是当我加上 Quartz石英定时器 后启动Tomcat 报错 , 以前对Quartz石英定时器 没有了解 希望高手指点下。 下面是错误信息 :rnrnrn 2012-9-28 11:43:15 org.apache.catalina.startup.HostConfig checkResourcesrn信息: Reloading context [/crm]rn2012-9-28 11:43:15 org.apache.catalina.loader.WebappClassLoader validateJarFilern信息: validateJarFile(D:\tomcat\apache-tomcat-6.0.10\webapps\crm\WEB-INF\lib\servlet-api.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.classrn2012-9-28 11:43:15 org.apache.catalina.core.ApplicationContext logrn信息: Initializing Spring root WebApplicationContextrn11:43:17,975 ERROR org.springframework.web.context.ContextLoader:215 - Context initialization failedrnorg.springframework.beans.factory.BeanCreationException: Error creating bean with name '_filterChainProxy': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_filterChainList': Cannot resolve reference to bean '_rememberMeFilter' while setting bean property 'filters' with key [6]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_rememberMeFilter': Cannot resolve reference to bean '_rememberMeServices' while setting bean property 'rememberMeServices'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_rememberMeServices': Initialization of bean failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.scheduling.quartz.SchedulerFactoryBean] for bean with name 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org/springframework/context/SmartLifecyclern at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)rn at java.security.AccessController.doPrivileged(Native Method)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)rn at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)rn at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)rn at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)rn at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)rn at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)rn at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)rn at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)rn at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)rn at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)rn at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)rn at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)rn at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3827)rn at org.apache.catalina.core.StandardContext.start(StandardContext.java:4336)rn at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1105)rn at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1203)rn at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:293)rn at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)rn at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1307)rn at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1571)rn at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1580)rn at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1560)rn at java.lang.Thread.run(Thread.java:619)rnCaused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_filterChainList': Cannot resolve reference to bean '_rememberMeFilter' while setting bean property 'filters' with key [6]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_rememberMeFilter': Cannot resolve reference to bean '_rememberMeServices' while setting bean property 'rememberMeServices'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_rememberMeServices': Initialization of bean failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.scheduling.quartz.SchedulerFactoryBean] for bean with name 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: problem with class file or dependent class; nested exception is

没有更多推荐了,返回首页