Quartz初试

最近需要用到Quartz,网上找了一些文章大多数都是E文,虽然E文不好,但还是将就写了一个

/*
 * Created on 2005-4-25
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package fox.rison;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class QuartzWorker implements Job {//需要继承Job接口

 /* (non-Javadoc)
  * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
  */
 private Connection conn;
 
 private Connection getConn(){  
  Connection conn =null;
  String url = "jdbc:mysql://192.168.1.20/iwapdb?user=root&password=&useUnicode=true&characterEncoding=utf-8";
  try{
   Class.forName("org.gjt.mm.mysql.Driver");
   conn = DriverManager.getConnection(url,"","");
  }catch(Exception g){
   g.printStackTrace();
  }
  return conn;
 }
 
 private String getYMD(Date d){
  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMd");    
  return String.valueOf(sdf.format(d));
 }
 
 
 private String getHHMM(Date d){
  int h = d.getHours();
  int mt = d.getMinutes();
  int hm = h * 100 + mt;
  return String.valueOf(hm);
 }
 
 private void doTimerList(){
//  ArrayList list = new ArrayList();
  try{
   if(conn==null || conn.isClosed()){
    conn = getConn();
   }
   Date d = new Date();
   String ymd = getYMD(d);
   String hm = getHHMM(d);
   String sql = "select * from time_column where Status ='0' AND ActionDate=" + ymd + " AND ActionTime=" + hm;
   System.out.println(sql);
   Statement stmt = conn.createStatement();
   ResultSet rs =stmt.executeQuery(sql);
   while(rs.next()){
    int pid = rs.getInt("PageMainID");
    String tmpsql = "";    
    if(rs.getString("ActionMode").equals("0")){
     tmpsql = "update page_main set status='1' where id = " + pid+"";
    }else{
     tmpsql = "update page_main set status='0' where id = " + pid+"";
    }
    Statement st = conn.createStatement();
    boolean count = st.execute(tmpsql);
    st.close();
    System.out.print(d.toLocaleString() + ":" + tmpsql + "  ");
    if(!count){
     System.out.println("SUCCESS");
    }else{
     System.out.println("FAILURE");
    }
   }
   stmt.close();   
  }catch(Exception g){
   g.printStackTrace();
  }
//  return list;
 }
 

//重载该函数
 public void execute(JobExecutionContext jCxt) throws JobExecutionException {
  // TODO Auto-generated method stub
  System.out.println(
    "Generating report - " +
    jCxt.getJobDetail().getJobDataMap().get("type") + new Date()  
  );
  doTimerList();

 }

 public static void main(String[] args) {
  try{
   SchedulerFactory shFact = new StdSchedulerFactory();
   Scheduler shed = shFact.getScheduler();
   shed.start();
   JobDetail jobDetail = new JobDetail("Quartz","Example",QuartzWorker.class);
   jobDetail.getJobDataMap().put("type","FULL");
   CronTrigger trigger = new CronTrigger("Quartz","Example");
   trigger.setCronExpression("0 0/5 * * * ?");//每5分钟执行一次
   shed.scheduleJob(jobDetail,trigger);   
  }catch(Exception g){
   g.printStackTrace();
  }
 }
}

然后写在同一目录下新建一个quartz.properties文件

#
# Configure Main Scheduler Properties
#

org.quartz.scheduler.instanceName = TestScheduler
org.quartz.scheduler.instanceId = one

#
# Configure ThreadPool  线程池数量
#

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount =  5
org.quartz.threadPool.threadPriority = 4

#
# Configure JobStore
#

org.quartz.jobStore.misfireThreshold = 5000

org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore  #保存在内存里,如果重新启动,那么所有的信息会消失,如果要做成持久化对象的话,可以保存到数据库里面。具体可以看

http://www.onjava.com/pub/a/onjava/2004/03/10/quartz.html?page=1

我也是照着这个写的,呵呵,可惜是E文的

你可能感兴趣的:(java,sql,mysql,quartz,Go)