Spring定时任务

1.配置定时时间即执行时间,使用cron表达式

#定时时间,每月凌晨1点执行
task.cron=0 0 1 1 * ?
log.isWrite=true

2.在app-context.xml文件中加载配置文件

<bean id="propertyConfigurer"            
         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>/WEB-INF/config/common.properties</value>
            </list>
        </property>
</bean>

3.在app-context.xml文件注入Bean

<bean id="commonProperties" class="java.util.Properties">
      <constructor-arg>
           <props>
               <prop key="task.cron">${task.cron}</prop>
               <prop key="log.isWrite">${log.isWrite}</prop>
           </props>
       </constructor-arg>
</bean>

4.为接口的实现类添加定时任务注解,注意该方法的返回值为void,没有参数

@Scheduled(cron = "${task.cron}")

5.通过Spring上下文读取配置文件中的数据

package com.better517na.rewardCheckComputeService.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.better517na.rewardCheckComputeService.service.IScheduleTaskService;

/**
 * Spring 工具类.
 * 
 * @author peiyu
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {
    /**
     * Spring应用上下文环境
     */
    private static ApplicationContext applicationContext;
    /**
     * 
     * {@inheritDoc}.
     */
    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        SpringContextUtil.applicationContext = ctx;
        CommentedProperties properties = new CommentedProperties();
        String path = SpringContextUtil.class.getClassLoader().getResource("").getPath();
        try {
            path = URLDecoder.decode(path, "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        System.out.println("####1加载配置文件:" + path);
        path = path.substring(0, path.indexOf("classes")) + "config/common.properties";
        System.out.println("####2加载配置文件:" + path);
        try {
            properties.load(new FileInputStream(path), "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取配置文件中的值
        FileLogUtil.isWrite = properties.getProperty("log.isWrite");

        OutputStream fos = null;
        try {
             fos = new FileOutputStream(path);
             //修改配置文件中的值
             properties.setProperty("log.isWrite", "false");
             properties.store(fos);
             System.out.println("####" + computeDate + "奖励计算结束####");
         } catch (Exception e) {
             e.printStackTrace();
             System.out.println("####定时计算服务出错:" + e.getMessage());
         } finally {
             if (fos != null) {
                 try {
                     fos.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
    }
    /**
     * 设置applicationContext.
     * 
     * @return 返回applicationContext
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
    /**
     * @param clazz .
     * @return .
     */
    public static Object getBean(Class<?> clazz) {
        return applicationContext.getBean(clazz);
    }
    /**
     * @param name .
     * @return .
     */
    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }
}

5.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值