• 自定义调度器执行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>

  • 相关阅读:
    转+更新 .NET中实践TDD
    解决方案:.net 4.0 下 Virtual Directory下如何部署一个作为Virtual Directory的Web Service
    knockout.js在线教程
    asp.net viewstate的最新理解
    转:什么是DIP、IoC、DI
    Common Infrastructure Libraries for .NET(1)Common.Logging Framework
    用Quartz.NET实现任务调度
    Common Infrastructure Libraries for .NET(2)ELMAH
    webots自学笔记(一)软件界面和简单模型仿真
    hdu 1753 大明A+B
  • 原文地址:https://www.cnblogs.com/sunjiqiang/p/6070130.html
Copyright © 2020-2023  润新知