quartz web定时应用

1.web.xml配置
<servlet>   
    <servlet-name>QuartzInitializer</servlet-name>   
       <display-name>Quartz Initializer Servlet</display-name>   
       <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>   
       <load-on-startup>1</load-on-startup>   
       <init-param>   
         <param-name>config-file</param-name>   
         <param-value>/quartz.properties</param-value>   
       </init-param>   
       <init-param>   
         <param-name>shutdown-on-unload</param-name>   
         <param-value>true</param-value>   
       </init-param>   
</servlet>   

2.在/WEB-INF/classes配置quartz所需配置文件
org.quartz.scheduler.instanceName = TestScheduler
org.quartz.scheduler.instanceId = one
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
#启用两个线程
org.quartz.threadPool.threadCount = 2
#线程优先级别,一般为5
org.quartz.threadPool.threadPriority = 5
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
#调度工作配置文件
org.quartz.plugin.jobInitializer.fileNames=updateWeb.xml    
#如果updateWeb.xml中存在调度器中已经有的job,true为覆盖  
org.quartz.plugin.jobInitializer.overWriteExistingJobs = false
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown = true

<?xml version='1.0' encoding='utf-8'?>
<quartz> 
  <job>
    <job-detail>
      <name>update_web</name>
      <group>myWebUpdate</group>
      <description>Job for update web</description>
      <job-class>com.trade.updateTimeWeb.UpdateWebAll</job-class>
      <volatility>false</volatility>
      <durability>false</durability>
      <recover>false</recover>
    </job-detail>
    <trigger>
      <cron>
        <name>myWebUpdateTrigger</name>
        <group>myWebUpdate</group>
        <description>Trigger for update web</description>
        <job-name>update_web</job-name>
        <job-group>myWebUpdate</job-group>
        <cron-expression>34 22 23 1/20 * ?</cron-expression>
      </cron>
    </trigger>
  </job>
</quartz>


3.指定执行的java类
package com.trade.updateTimeWeb;

import java.util.ArrayList;
import java.util.HashMap;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.sys.dbm.DbexecuteDAO;
import com.sys.exp.ApplicationException;
import com.trade.custChannel.custChannelInfo;
import com.trade.search.UpdateAllSearch;
import com.updateWeb.updateHtml.CreateChannel;
import com.updateWeb.updateHtml.CreateIndex;
public class UpdateWebAll implements Job{
	SwithConfig sConfig=new SwithConfig();
    public String swith(String key){
    	HashMap<String, String> map=sConfig.getSwitchConfig();
    	if(map!=null&&map.size()>0){
    		if(map.containsKey(key))
    			 return map.get(key);
    	}
        return "";
    }
	public ArrayList getChAllChLevelFirst() {
		ArrayList list = new ArrayList();
		DbexecuteDAO dbExcecute = new DbexecuteDAO();
		String sql = "SELECT ch_id FROM td_b_channel WHERE ch_level=1";
		list = dbExcecute.selBizQuery(sql);
		return list;
	}

	public void updateAllWeb() {
		CreateIndex index = new CreateIndex();
		CreateChannel channel = new CreateChannel();
		if(swith("indexSwitch").equals("0")){
			
			System.out.println("<----------home start---------->");
			index.CreateIndex("/systemplate/default/templates/index.html", "/","index.html");
			System.out.println("<----------home end---------->");
		}
		if(swith("channelSwitch").equals("0")){
			
				System.out.println("<----------channel start---------->");
				/*ArrayList list = getChAllChLevelFirst();
				String ch_id = "";
				for (int i = 0; i < list.size(); i++) {
					HashMap map = (HashMap) list.get(i);
					if (map.get("ch_id") != null) {
						ch_id = map.get("ch_id").toString();
					}
					try {
					if (!ch_id.equals(""))
						channel.CreateChannelIndexSelf(ch_id);
					} catch (ApplicationException e) {
						System.out.println(ch_id+"...error...");
					}
				}*/
				try {
					  channel.CreateChannelIndexSelf("6858808037");
					  channel.CreateChannelIndexSelf("1777148108");
				  } catch (ApplicationException e) {
						System.out.println("....error....");
			    }
			System.out.println("<----------channel end---------->");
		 }
		if(swith("enterpriseSwitch").equals("0")){
			
			System.out.println("<----------member shop start---------->");
			custChannelInfo CustChannelInfo = new custChannelInfo();
 
			if(!swith("updateNum").equals("")) 
			CustChannelInfo.updateAllCustHtml(swith("updateNum"));
			System.out.println("<---------member stop end---------->");
		}
		if(swith("searchSwitch").equals("0")){
			
			System.out.println("<----------search start---------->");
			UpdateAllSearch upsearch = new UpdateAllSearch();
			upsearch.updateAllSearch();
			System.out.println("<----------search end---------->");
		}

	}

	public void execute(JobExecutionContext context)throws JobExecutionException {
		if(swith("masterSwitch").equals("0")){
			System.out.println("<----------job start---------->");
			updateAllWeb();
			System.out.println("<----------job end---------->");
		}else{
			System.out.println("job close");
		}
	}

}

你可能感兴趣的:(sql,Web,xml,quartz,servlet)