Spring 定时器

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>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值