zoukankan      html  css  js  c++  java
  • 自定义调度器执行job

    1 准备依赖的jar

    一个需要被调取的job类

    package com.xuanyuan.petition.timer.bean;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    
    public class HelloJob {
        
        private SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
    
        public void execute() {
            System.out.println(" 执行调度业务 ....HelloJob  尼古拉斯 的当前日期是:" +  sdf.format(new Date()));
            
        }
    
    }
    

    spring配置文件。初始化一些调度器和触发器的时间设置,并绑定我们要执行的job

    <?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
        default-lazy-init="true">
    
    
    <!-- 使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法-->
    <bean id="taskJob" class="com.xuanyuan.petition.timer.bean.HelloJob"/>
    
    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="group" value="job_work"/>
        <property name="name" value="job_work_name"/>
        <!--false表示等上一个任务执行完后再开启新的任务-->
        <property name="concurrent" value="false"/>
        <property name="targetObject">
            <ref bean="taskJob"/>
        </property>
        <property name="targetMethod">
            <value>execute</value>
        </property>
    </bean>
    <!--  调度触发器 -->
    <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail" ref="jobDetail"></property>
            <property name="cronExpression" value="0 0/1 8-17 * * ? * MON-FRI"></property>
        </bean>
    
    <!-- 调度工厂 -->
    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="myTrigger"/>
            </list>
        </property>
    </bean>
    </beans>

  • 相关阅读:
    pip install
    自动更新高清电影文件中文名
    csv、excel导入oracle
    02_Jenkins配置任务
    01_Jenkins windows安装
    run_jmeter.py
    02_禅道的基本使用
    01_禅道搭建手册
    01_charles 下载安装(破解版)
    06_Linux常见的命令
  • 原文地址:https://www.cnblogs.com/sunjiqiang/p/6070130.html
Copyright © 2011-2022 走看看