• Quartz与Spring集成(二)


    一、获取quartz详情jar

    <!-- quartz 的jar -->
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz-jobs</artifactId>
        <version>2.2.1</version>
    </dependency>

    二、相关代码案例

    一个job可以有多个处罚

    一个触发只能有一个job

    //Main.java启动类
    public
    class Main { public static void main(String[] args) { new ClassPathXmlApplicationContext("classpath:applicationContext-scheduler.xml"); } }
    //MyJob任务类
    public
    class MyJob extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println("myJob执行了............" + context.getTrigger().getKey().getName()); ApplicationContext applicationContext = (ApplicationContext) context.getJobDetail().getJobDataMap() .get("applicationContext"); System.out.println("获取到的Spring容器是" + applicationContext); } }
    //applicationContext-scheduler.xml
    <?
    xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"
    > <!-- 定义任务bean --> <bean name="myJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <!-- 指定具体的job类 --> <property name="jobClass" value="com.bky_lzw.quartz.MyJob" /> <!-- 指定job的名称 --> <property name="name" value="myJob" /> <!-- 指定job分组 --> <property name="group" value="jobs" /> <!-- 必须设置为true,如果为false,当没有活动的触发器与之关联时会在调度器中删除该任务 --> <property name="durability" value="true" /> <!-- 指定spring容器的key,如果不设定在job中的jobmap中是获取不到spring容器的 --> <property name="applicationContextJobDataKey" value="applicationContext" /> </bean> <!-- 定义触发器 --> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="myJobDetail" /> <!-- 每一分钟执行一次 --> <property name="cronExpression" value="0/5 * * * * ?" /> </bean> <!-- 定义调度器 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger" /> </list> </property> </bean> </beans>
  • 相关阅读:
    BAM部署失败 未能加载”AdomdClient”或它的某一个依赖项。系统找不到指定的文件
    BizTalk Server 事务机制
    TSQL 访问远程数据库并对其数据表进行操作
    BAM门户聚合 – Pivot Table不显示数据
    Find max or min
    SetProperties
    Define
    Form derives from Form
    MWArrayComponent
    JAVA JDK
  • 原文地址:https://www.cnblogs.com/bky-lzw/p/8945110.html
Copyright © 2020-2023  润新知