您的位置:首页 > 编程语言 > Java开发

spring与Quartz整合

2016-06-06 11:10 435 查看
特别注意一点,与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;
4000

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===");

  }   

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: