spring与Quartz整合

特别注意一点,与Spring3.1以下版本整合必须使用Quartz1

quartz-1.6.jar 
commons-logging.jar 
spring-core-3.0.5.RELEASE.jar 
spring-beans-3.0.5.RELEASE.jar 
spring-context-3.0.5.RELEASE.jar 
spring-context-support-3.0.5.RELEASE.jar 
spring-asm-3.0.5.RELEASE.jar 
spring-expression-3.0.5.RELEASE.jar 
spring.transaction-3.0.5.RELEASE.jar 
spring-web-3.0.5.RELEASE.jar
 

quartz-spring-config.xml配置文件:

<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">


<bean id="myScheduler"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="myTriggersA"></ref>
            </list>
        </property>
        <property name="autoStartup" value="true"></property>
    </bean>
    
    <bean id="myTriggersA"
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="myJobDetailA">
        </property>
        <property name="cronExpression">
            <value>0 0 18 * * ? *</value>
        </property>
    </bean>
    
    <bean id="myJobDetailA"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myJobA">
        </property>
        <property name="targetMethod" value="work"></property>
        <property name="concurrent" value="false" />
        <!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
    </bean>
    <bean id="myJobA" class="com.bmcc.common.tool.quartz.QuartzJob">
    </bean>
    
 </beans>

web.xml中加载配置文件:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
    classpath:applicationContext.xml,
    classpath:quartz-spring-config.xml 
    </param-value>
</context-param>


定时任务:


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


import org.hibernate.Session;
import org.hibernate.jdbc.Work;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.bmcc.common.tool.sessionfactory.HibernateSessionFactory;
import com.bmcc.ticket.dao.TetQuartzConfigDao;
import com.bmcc.ticket.model.TetQuartzConfig;









@Service
public class QuartzJob {

@Autowired
private TetQuartzConfigDao tetQuartzConfigDao;
//需要备份的正常状态
     public static int NORMAL_STATUS =0;
     
String endDate = "";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
 
  public void work()
   {
   System.out.println("Quartz的任务调度!!!");
   
Date dNow = new Date(); //当前时间
Calendar calendar = Calendar.getInstance(); //得到日历
calendar.setTime(dNow);//把当前时间赋给日历
calendar.add(Calendar.MONTH, -3); //设置为前3月
endDate = formatter.format(calendar.getTime()); //得到前3月的时间
   //查询配置表
   List configList = (List)tetQuartzConfigDao.getConfigByStatus(NORMAL_STATUS);


   if(configList!=null&&configList.size()>0){
   System.err.println("待归档的表个数:"+configList.size());
    for(int i=0;i<configList.size();i++){
    TetQuartzConfig tempObj = (TetQuartzConfig)configList.get(i);
    //备份数据并删除数据
    bakAndDeleteTable(tempObj);
    }
    }
   }
  
  //备份表数据 如果配置的条件为空,则归档三个月前数据,否则以配置的SQL为准
  private void bakAndDeleteTable(TetQuartzConfig tempObj){
System.err.println("endDate=="+endDate);   
 System.err.println("清除数据begin===");
 final StringBuffer sf1 = new StringBuffer();
 final StringBuffer sf2 = new StringBuffer();
 if(tempObj.getSqlwhere()==null||tempObj.getSqlwhere().equals("")){
 sf1.append(" insert into "+tempObj.getBaktable()+"  select * from "+tempObj.getTablename()+"  where CREATETIME<= TIMESTAMP ('"+endDate+" 23:59:59.999999')");
     sf2.append(" DELETE FROM "+tempObj.getTablename()+"  where CREATETIME<= TIMESTAMP ('"+endDate+" 23:59:59.999999')");
 }else{
 System.err.println("tempObj.getTablename()="+tempObj.getTablename()+"备份规则:"+tempObj.getSqlwhere());
 sf1.append(" insert into "+tempObj.getBaktable()+"  select * from "+tempObj.getTablename()+"  where 1=1 and "+tempObj.getSqlwhere());
     sf2.append(" DELETE FROM "+tempObj.getTablename()+" where 1=1 and "+tempObj.getSqlwhere());
 }
//Connection conn = null;
Session session = HibernateSessionFactory.currentSession();
System.err.println("HibernateSessionFactory instance: ==> " + session.getSessionFactory().toString());
session.doWork(  
 new Work() {  
   public void execute(Connection conn) {  
     // 这里已经得到connection了,可以继续你的JDBC代码。  
     // 注意不要close了这个connection。  
try 
{
PreparedStatement stmt =null;
PreparedStatement stmt2=null;
//conn = session.connection();
conn.setAutoCommit(false);
System.err.println("conn==="+conn);
stmt = conn.prepareStatement(sf1.toString());
System.err.println("sf1.toString()==="+sf1.toString());
System.err.println("sf2.toString()==="+sf2.toString());
stmt.executeUpdate();
stmt2 = conn.prepareStatement(sf2.toString());
stmt2.executeUpdate();
conn.commit();
stmt.close();
stmt2.close();
} catch (Exception e) {
e.printStackTrace();
}
   }  
 }  
);
 session.close();
 System.err.println("清除数据end===");

  }   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值