Spring 4 项目配置定时器

该博客主要介绍了Spring定时器的配置与使用。首先说明了引入定时器所需的Maven依赖,接着展示了定时器对应的方法类书写,最后给出了Spring配置对应的环境变量,包括不同方式的定时器配置示例,如CronTrigger和SimpleTrigger等。


1、引入定时器需要引入的maven依赖


<!-- 关于quartz 建议1.8<版本 -->

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>1.8.5</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId> 
    <artifactId>spring-context-support</artifactId> 
    <version>3.2.4.RELEASE</version>
</dependency>

2、书写定时器对应的定时器的方法类


package com.zero.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Date;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
 * 
 * @author haijie.peng
 * @time 2019年5月6日09:39:07
 * @deprecated 定时器定时调用第三方程序
 */
public class TaskOne  extends QuartzJobBean {

    private static Logger log = Logger.getLogger(TaskOne.class.getClass());
    private int timeout;
    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }
    @Override
    protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
        Properties properties = new Properties();
        ClassLoader classLoader = TaskOne.class.getClassLoader();   
        URL resource = classLoader.getResource("db.properties");  
        String path = resource.getPath();  
        InputStream resourceAsStream = classLoader.getResourceAsStream("db.properties"); 
        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String urlPath = properties.getProperty("thridApp");
        log.info("-----upLoad video-----"+new Date()+"------------------------"+properties.getProperty("thirdApp"));
        runThirdApp(urlPath);
    }

     //调用指令方法,没有用到的话请自觉注解
    public static Integer runThirdApp(String code){
        Integer num = null;
        StringBuilder sb = new StringBuilder();
        try {
            String[] cmds = {code};
            Process pro = Runtime.getRuntime().exec(cmds);
            pro.waitFor();
            InputStream in = pro.getInputStream();
            BufferedReader reade = new BufferedReader(new InputStreamReader(in));
            String lin = null;
            while ((lin = reade.readLine()) != null) {
                if(lin!=null) {
                    sb.append(lin);
                }

            }
            pro.waitFor();
            in.close();
            reade.close();
            log.info("The ThirdApp run result is"+sb.toString()+"----------"+new Date());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return num;
    }
}

 


3、spring配置对应的环境变量


    <!--定时器配置文件  -->

    <!-- 方式一-->
    <!-- <bean name="exampleJob" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="com.zero.utils.TaskOne" />
        <property name="jobDataAsMap">
            <map>
                <entry key="timeout" value="5" />
            </map>
        </property>
    </bean> -->
    <!-- 对于下列不理解请自行百度“定时器表达式”-->

  <bean id="cronTrigger"
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="exampleJob" />
        <property name="cronExpression" value="0 0 6 * * ?" />
        <property name="cronExpression" value="0 0/10 * * * ?" />
        <property name="cronExpression" value="0/120 * * * * ?" />
    </bean>
    <!-- 方式2 -->
    <!-- <bean id="exampleBusinessObject" class="com.zero.utils.QuartzJob" />
    <bean id="jobDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="exampleBusinessObject" />
        <property name="targetMethod" value="doIt" />
        <property name="concurrent" value="false" />
    </bean>
    <bean id="simpleTrigger"
        class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        see the example of method invoking job above
        <property name="jobDetail" ref="jobDetail" />
        10 seconds
        <property name="startDelay" value="5000" />
        repeat every 50 seconds
        <property name="repeatInterval" value="3000" />
    </bean> -->
 
    <!-- 总调度用于启动Spring定时器 -->
    <!-- <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTrigger" />
                <ref bean="simpleTrigger" />
            </list>
        </property>
    </bean> -->

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值