您的位置:首页 > 编程语言 > Java开发

Spring定时任务

2016-06-22 16:54 375 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: