Spring3.1+Quertz1.8实现多个计划任务

1.主要是配置文件:如下:(这里说明一下主要是看红色部分的配置,其他的可以根据自己的实际情况修改,这里只是个思路。)



	
	
	
	
	
	
	
	
		
		
		
		
		
		
		
		
		
	
	
	
	
		
		
		
		
			
				${hibernate.dialect}
				
				${hibernate.hbm2ddl.auto}
				${hibernate.show_sql}
				false
				false
				
			    true 
			    true
			     
			    
			
		
		
			
				cn.gov.csrc.base.systemmanager.model
				cn.gov.csrc.report.model
				cn.gov.csrc.noreal.report.model
			
		
	
	
	
	
		
		
	
	
	
	
		
			
			
			
			
			
			
			
			
			
			
		
	
	
	
	
	
	
		
		
		
	
	
	   
           
           
               
           
    
	
	
    
    
    
    
    
    
    
    	
    	
    		
    	
    	
    	
    		start
    	
    	
    	
    
    
    
    	
    	
    		
    	
    	
    	
    		parseXml
    	
    	
    	
    
    
    
    
    	
    		
    	
    	
    	
    		
    		
    		0 23 14 * * ?
    	
    
    
    
    	
    		
    	
    	
    	
    		0 24 14 * * ?
    	
    
    
    
    
    	
    		
    			
    			
    		
    	
    
       


2.TimerAction.java:

package cn.gov.csrc.report.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Controller;

import cn.gov.csrc.base.systemmanager.model.User;
import cn.gov.csrc.report.model.Accessory;
import cn.gov.csrc.report.model.Case;
import cn.gov.csrc.report.model.ReportedPerson;
import cn.gov.csrc.report.service.CaseService;

@Controller()
@Scope("prototype")
@Action("TimerAction")
public class TimerAction extends QuartzJobBean {
	
	private int timeout;

	public TimerAction() {
	}

	/** 调度工厂实例化后,经过timeout时间开始执行调度 */
	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}

	@Override
	protected void executeInternal(JobExecutionContext context)
			throws JobExecutionException {
		System.out.println("定时任务执行中......");
	}

	/**
	 * 定时导出外网数据到指定目录
	 * @throws Exception
	 */
	public void start() throws Exception {	
        	System.out.println("定时任务执行成功......");
 }}

3.要解析的xml模板文件如下: (这里我的xml文件放到了D:\admin\2014-03-18目录下)


       
	
		<举报信息反馈>
			
				 <举报编号>1
				 <状态>1
				 <答复意见>填写答复意见
			

			
				 <举报编号>2
				 <状态>2
				 <答复意见>填写答复意见
			
			
				 <举报编号>3
				 <状态>3
				 <答复意见>填写答复意见
			
			
				 <举报编号>4
				 <状态>1
				 <答复意见>填写答复意见
			
		
	

4.PraseXmlAction.java:

package cn.gov.csrc.report.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Controller;

@Controller()
@Scope("prototype")
@Action("PraseXmlAction")
public class PraseXmlAction extends QuartzJobBean {

	private static final String xmlPath = "D:\\admin\\" + getFileName()
			+ "\\case.xml";

	public PraseXmlAction() {

	}

	@Override
	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
		// TODO Auto-generated method stub
		System.out.println("定时任务执行中......");
	}

	/**
	 * 获取当前时间为文件夹名称
	 * 
	 * @return
	 */
	protected static String getFileName() {
		String fileNames = null;
		Date date = new Date();
		SimpleDateFormat formatDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		fileNames = formatDateFormat.format(date);
		return fileNames;
	}

	/**
	 * JDom解析xml文件
	 */
	public void parseXml() {
		try {
			List elementList = getElementList();
			for (Element element : elementList) {
				Element nameElement = element.getChild("举报信息反馈");
				List children = nameElement.getChildren();
				for (Element element2 : children) {
					Element nameElement2 = element2.getChild("举报编号");
					if (nameElement2 != null) {
						System.out.println("   " + nameElement2.getName() + ":"
								+ nameElement2.getTextTrim());
					}
					Element valueElement = element2.getChild("状态");
					if (valueElement != null) {
						System.out.println("   " + valueElement.getName() + ":"
								+ valueElement.getTextTrim());
					}
					Element descriptElement = element2.getChild("答复意见");
					if (descriptElement != null) {
						System.out.println("   " + descriptElement.getName()
								+ ":" + descriptElement.getTextTrim());
					}
					System.out.println("--------------------");
				}
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public static List getElementList() throws FileNotFoundException,
			JDOMException, IOException {
		// 创建SAX建造者对象,该类构造方法的重载boolean类型的方法中validate表示是否验证xml文档
		SAXBuilder saxBuilder = new SAXBuilder(false);
		InputStream inputStream = new FileInputStream(new File(xmlPath));
		// 解析xml文档,返回document文档对象
		Document document = saxBuilder.build(inputStream);
		// 获取根节点
		Element rootElement = document.getRootElement();
		// 获取根节点下的第一个子节点
		List elementList = rootElement.getChildren();
		return elementList;
	}

}



你可能感兴趣的:(Spring3.1+Quertz1.8实现多个计划任务)