Spring定时任务
2016-06-22 16:54
375 查看
1.配置定时时间即执行时间,使用cron表达式
#定时时间,每月凌晨1点执行
task.cron=0 0 1 1 * ?
log.isWrite=true
2.在app-context.xml文件中加载配置文件
3.在app-context.xml文件注入Bean
4.为接口的实现类添加定时任务注解,注意该方法的返回值为void,没有参数
5.通过Spring上下文读取配置文件中的数据
5.
#定时时间,每月凌晨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.
相关文章推荐
- Struts2 Action学习
- Java - 基本类的使用(集合类)
- 编程之美2.11寻找最近的点对Java版二
- java代码将e.printStackTrace()写入log4j文件异常信息
- java基本算法之快速排序
- java 中断线程
- 判断一个工程是java还是web
- 看了一则广告,学会for循环~【java】
- java基本算法之冒泡排序
- 简单的邮箱开发1----Java
- java.lang.UnsatisfiedLinkError: No implementation found for long com.baidu.platform.comjni.map.commo
- OGNL表达式学习
- Mybatis类型, Java类型和PostgreSQL数据库类型的对应关系(草稿)
- Java常见异常(Runtime Exception)
- Eclipse控制台中的中文输出乱码问题
- spring 和springMVC的区别
- Exception in thread "main" java.lang.UnsupportedClassVersionError: 问题
- Java中注解Annotation的定义、使用、解析
- java mail
- 【java】Cookie实现简单浏览商品