java 定时器 quartz的使用

spring 3.0.5  quartz 1.8   

刚开始用的2.2.1的quartz 因为是spring3.0.5 不支持quartz 2.2.1 。使用时需要注意版本

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<!-- 定义具体的任务类 -->
	<bean id="SpringQtzJob" class="com.cpa.quartz.ExportSQL" />
 
    <bean id="SpringQtzJobMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
    <property name="targetObject">  
        <ref bean="SpringQtzJob"/>  
    </property>  
    <property name="targetMethod">  <!-- 要执行的方法名称 -->  
        <value>export</value>  
    </property>  
</bean>  
  
<!-- ======================== 调度触发器 ======================== -->  
<bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean">  
    <property name="jobDetail" ref="SpringQtzJobMethod"></property>  
<!--每5秒执行(为了测试)-->
    <property name="cronExpression" value="0/5 * * * * ?"></property>  
</bean>  
  
<!-- ======================== 调度工厂 ======================== -->  
<bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
    <property name="triggers">  
        <list>  
            <ref bean="CronTriggerBean"/>  
        </list>  
    </property>  
</bean>    
 </beans>


ExportSQL 类

package com.cpa.quartz;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.cpa.util.ResourceUtil;


/**
 * 
 * @author vc
 *定时导出sql用于备份
 */
public class ExportSQL{
	private static final Logger logger = Logger.getLogger(ExportSQL.class);
	public void export(){
		System.err.println("------------------------------------------------------");
		String backFileString  =ResourceUtil.getBack();
		File backFile = new File(backFileString);
		if (!backFile.exists()) {
			backFile.mkdirs();
		}
		Runtime rt = Runtime.getRuntime();  
		SimpleDateFormat sdf = new  SimpleDateFormat("yyyyMMddHHmmss");
		StringBuilder sb = new StringBuilder();
		sb.append("cmd /c ");
		sb.append("mysqldump -h localhost -u");
		sb.append(ResourceUtil.getUserName());
		sb.append(" -p");
		sb.append(ResourceUtil.getPasswd());
		sb.append(" ");
		sb.append(ResourceUtil.getDataName());
		sb.append(" > ");
		sb.append(backFileString);
		sb.append("/");
		sb.append(sdf.format(new Date()));
		sb.append(".sql");
//        String cmd ="mysqldump -h localhost -u"+ResourceUtil.getUserName()+" -p"+ResourceUtil.getPasswd()+" "+ResourceUtil.getDataName()+" > "+backFileString+"/"+sdf.format(new Date())+".sql"; 
        try {
			Process process = rt.exec(sb.toString());
			InputStreamReader isr = new InputStreamReader(process.getErrorStream());  
	        LineNumberReader input = new LineNumberReader(isr);  
	        String line;  
	        while((line = input.readLine())!= null){  
	            System.out.println(line+"~~~~~~~~~~");  
	            logger.info(line);
	        }  
		} catch (IOException e) {
			e.printStackTrace();
			logger.error(e.getMessage()+"备份数据库异常");
		}
	}

	
}


你可能感兴趣的:(java 定时器 quartz的使用)