Spring 定时处理任务(触发器)

1.在applicationContext.xml中配置如下:
//任务调度类
 <bean id="InsertUser" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
       <property name="targetObject">
         <bean class="org.service.UpdateErpUser"/>//定义要执行的类
       </property>
      <property name="targetMethod">
          <value>insertUser</value>//指定此类的方法
       </property>
 </bean>
//触发器,并设置每天凌晨2点执行任务
<bean id="cronSynTmTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="InsertUser" />
<property name="cronExpression" value="0 0 2 * * ?"/>
</bean>
//调度器
<bean id="start" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
    <list>
          <ref bean="cronSynTmTrigger" /> 
          <ref bean="...."/>//这里可以配置N个调度任务
    </list>
    </property>
</bean>


java Class:
package org.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbcp.BasicDataSource;

public class UpdateErpUser {

 private static final String DBDriver = "com.mysql.jdbc.Driver";
 private static final String url = "jdbc:mysql://192.168.18.231:3306/user?useUnicode=true&amp;characterEncoding=UTF-8";
 private static final String user = "root";
 private static final String password = "root";
 /*
  * 静态加载驱动类
  */
 static {
  try {
   Class.forName(DBDriver);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public static Connection getConnection() {
  Connection conn = null;
  try {
   conn = DriverManager.getConnection(url, user, password);
   System.out.println("数据库连接成功!");
  } catch (Exception e) {
   e.printStackTrace();
   System.out.println("数据库连接失败!");
  }
  return conn;
 }

 public List<ErpUser> selectUser() {
  BasicDataSource ds = new BasicDataSource();
  // mysql连接方式
  ds.setDriverClassName("com.mysql.jdbc.Driver");
  ds.setUrl("jdbc:mysql://192.168.18.41:3306/users?useUnicode=true&amp;characterEncoding=UTF-8");
  ds.setUsername("test");
  ds.setPassword("123456");
  Connection conn = null;
  PreparedStatement state=null;
  ResultSet rs=null;
  List<ErpUser> list = new ArrayList<ErpUser>();
  ErpUser user = null;
  try {
   conn = ds.getConnection();
   state = conn
     .prepareStatement("select * from t_user");
   rs = state.executeQuery();
   while (rs.next()) {
    user = new ErpUser();
    user.setUserId(rs.getInt("user_id"));
    user.setUserName(rs.getString("userName"));
    user.setPassword(rs.getString("password"));
    user.setUemail(rs.getString("uemail"));
    list.add(user);
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (rs!= null || state!=null||conn!=null )
    try {
     rs.close();
     rs=null;
     state.close();
     state=null;
     conn.close();
     conn = null;
    } catch (SQLException e) {
     e.printStackTrace();
    }
  }
  return list;
 }
 /**
  **删除数据库中的User表
  * @return result
  */
 public static int delUser(){
  int result=0;
  Connection conn = null;
  PreparedStatement state = null;
  try {
   conn = getConnection();
   String sqlstr="delete from t_user where user_id>0;";
   state=conn.prepareStatement(sqlstr);
   result=state.executeUpdate();
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   if (state != null || conn != null) {
    try {
     state.close();
     state=null;
     conn.close();
     conn=null;
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
  }
  return result;
 }
 /**
  *定义的触发器执行此方法 
  *添加用户
  * @return
  */
 public int insertUser() {
  UpdateErpUser u = new UpdateErpUser();
  List<ErpUser> luser = u.selectUser();
  Connection conn = null;
  PreparedStatement state = null;
  int result = 0;
  try {
   delUser();
   conn=getConnection();
   if(luser.size()>0){
    for (ErpUser e : luser) {
     String sql = "insert into t_user(user_id,username,realname,password,uemail) value("
       + e.getUserId()
       + ","
       + "'"
       + e.getUserName()
       + "','"
       + e.getRealName()
       + "','"
       + e.getPassword()
       + "','"
       + e.getUemail() + "')";
     state = conn.prepareStatement(sql);
     result += state.executeUpdate();
    }
   }
   System.out.println("result="+result);
   return result;
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (state != null || conn != null) {
    try {
     state.close();
     conn.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
  }
  return 0;
 }

 

你可能感兴趣的:(Spring 定时处理任务(触发器))