Spring 定时器
2013-10-24 10:39
417 查看
package cn.hs.dpl.web.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Timestamp;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import cn.hs.dpl.service.IMenuService;
import cn.hs.dpl.utils.LoggerUtil;
/**
* 关于版本自动定制化的Action
* @author bWX62796
*
*/
public class TimerAction extends QuartzJobBean {
//版本定制化service
private IMenuService menuService;
public void setMenuService(IMenuService menuService) {
this.menuService = menuService;
}
/**
* 自动化的版本定制化
* @return
* @throws Exception
*/
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
System.out.println("开始版本定制化...");
LoggerUtil.tranceINFO("版本自动定制化...");
String xmlString = menuService.addAutoRule();
BufferedInputStream in = null;
BufferedOutputStream out = null;
String temp = Thread.currentThread().getContextClassLoader().getResource("").getPath() ;
temp = temp.substring(0, temp.lastIndexOf("WEB-INF")); //WEB-INF 上一級就是項目的根目錄
String uploadPath = temp + "\\auto_version";
File file = new File(uploadPath);
try {
in = new BufferedInputStream(new ByteArrayInputStream(xmlString.getBytes("UTF-8")));
//File file = new File(uploadPath);
if(!file.exists()) {
file.mkdir();
}
out = new BufferedOutputStream( new FileOutputStream( uploadPath+ File.separator + new Timestamp(System.currentTimeMillis()).toString().substring(0, 10) +"_rule.xml"));
int j = 0;
while ( ( j = in.read() ) != -1 ) {
out.write(j);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!= null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
配置文件
<?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:aop="http://www.springframework.org/schema/aop"
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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 版本定制化 -->
<bean id="reportJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="cn.hs.dpl.web.action.TimerAction" />
<property name="jobDataAsMap">
<map>
<entry key="menuService">
<ref bean="menuService"/>
</entry>
</map>
</property>
</bean>
<!--
CronTriggerBean指定工作的准确运行时间
-->
<bean id="cronReportTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="reportJob" />
<property name="cronExpression">
<!-- 设置每天晚上12点的时候,会自动进行版本的定制化 -->
<value>0 0 23 ? * *</value>
</property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronReportTrigger" />
</list>
</property>
</bean>
</beans>
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Timestamp;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import cn.hs.dpl.service.IMenuService;
import cn.hs.dpl.utils.LoggerUtil;
/**
* 关于版本自动定制化的Action
* @author bWX62796
*
*/
public class TimerAction extends QuartzJobBean {
//版本定制化service
private IMenuService menuService;
public void setMenuService(IMenuService menuService) {
this.menuService = menuService;
}
/**
* 自动化的版本定制化
* @return
* @throws Exception
*/
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
System.out.println("开始版本定制化...");
LoggerUtil.tranceINFO("版本自动定制化...");
String xmlString = menuService.addAutoRule();
BufferedInputStream in = null;
BufferedOutputStream out = null;
String temp = Thread.currentThread().getContextClassLoader().getResource("").getPath() ;
temp = temp.substring(0, temp.lastIndexOf("WEB-INF")); //WEB-INF 上一級就是項目的根目錄
String uploadPath = temp + "\\auto_version";
File file = new File(uploadPath);
try {
in = new BufferedInputStream(new ByteArrayInputStream(xmlString.getBytes("UTF-8")));
//File file = new File(uploadPath);
if(!file.exists()) {
file.mkdir();
}
out = new BufferedOutputStream( new FileOutputStream( uploadPath+ File.separator + new Timestamp(System.currentTimeMillis()).toString().substring(0, 10) +"_rule.xml"));
int j = 0;
while ( ( j = in.read() ) != -1 ) {
out.write(j);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!= null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
配置文件
<?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:aop="http://www.springframework.org/schema/aop"
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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 版本定制化 -->
<bean id="reportJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="cn.hs.dpl.web.action.TimerAction" />
<property name="jobDataAsMap">
<map>
<entry key="menuService">
<ref bean="menuService"/>
</entry>
</map>
</property>
</bean>
<!--
CronTriggerBean指定工作的准确运行时间
-->
<bean id="cronReportTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="reportJob" />
<property name="cronExpression">
<!-- 设置每天晚上12点的时候,会自动进行版本的定制化 -->
<value>0 0 23 ? * *</value>
</property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronReportTrigger" />
</list>
</property>
</bean>
</beans>
相关文章推荐
- Spring中属性注入详解
- struts2 spring整合fieldError问题
- 解析Java中如何获取Spring中配置的bean
- Spring的注解配置与XML配置之间的比较
- Java 二维码,QR码,J4L-QRCode 的资料整理
- java中 spring 定时任务 实现代码
- Java代码重构的几种模式详解
- 基于spring+hibernate+JQuery开发之电子相册(附源码下载)
- spring实例化javabean的三种方式分享
- Spring3.2.0和Quartz1.8.6集群配置
- spring security 3.2.0.M1 方法级别教程 基于注解——第一部分
- spring security 3.2.0.M1 方法级别教程 基于注解——第二部分
- Spring 加载 xml 文件机制
- 基于全注解的Spring3.1 mvc、myBatis3.1、Mysql的轻量级项目
- Spring的四种声明式事务的配置-Hibernate事务
- Spring MVC 集成 Velocity 配置
- Spring里PropertyPlaceholderConfigurer类的使用
- Spring事务传播性与隔离级别
- Easyui的datagrid结合hibernate实现数据分页