1.主要是配置文件:如下:(這里說明一下主要是看紅色部分的配置,其他的可以根據自己的實際情況修改,這里只是個思路。)
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xmlns:security="http://www.springframework.org/schema/security"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"><!-- 分散配置 --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 組件掃描,此處的實現原理是遞歸掃描所有包,效率較差。開發模式寫法如下。后期調優可以將具體包名全部列到下面,以逗號隔開 --><context:component-scan base-package="cn.gov.csrc.*"/><!-- 配置c3p0數據源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverclass}" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><property name="maxPoolSize" value="${c3p0.pool.size.max}" /><property name="minPoolSize" value="${c3p0.pool.size.min}" /><property name="initialPoolSize" value="${c3p0.pool.size.ini}" /><property name="acquireIncrement" value="${c3p0.pool.size.increment}" /><property name="maxIdleTime" value="${cpool.maxIdleTime}" /></bean><!-- 本地回話工廠bean,spring整合hibernate的核心入口 --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><!-- 數據源 --><property name="dataSource" ref="dataSource" /><!-- hibernate自身屬性 --><property name="hibernateProperties"><props><prop key="hibernate.dialect">${hibernate.dialect}</prop><!-- 根據Bean自動生成數據庫表,MySql5.5以后要使用org.hibernate.dialect.MySQL5InnoDBDialect才能創建成功! --><prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop><prop key="hibernate.format_sql">false</prop><prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop><!-- 開啟二級緩存,其實hibernate默認就是開啟的,這里顯示的指定一下 --><prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.autoReconnect">true</prop><!-- 指定二級緩存產品的提供商 --> <!-- <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop><prop key="hibernate.cache.use_query_cache">true</prop> --></props></property><property name="packagesToScan"><list><value>cn.gov.csrc.base.systemmanager.model</value><value>cn.gov.csrc.report.model</value><value>cn.gov.csrc.noreal.report.model</value></list></property></bean><!-- hibernate事務管理器,在service層上實現事務管理,達到平臺無關性 --><bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /><property name="globalRollbackOnParticipationFailure" value="false" /></bean><!-- 事務通知 --><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/><tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/><tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/><tx:method name="batch*" propagation="REQUIRED" isolation="DEFAULT"/><tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/><tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/><tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/><tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/></tx:attributes></tx:advice><bean id="genericLogger" class="cn.gov.csrc.base.log.GenericLogger"/><!-- aop配置 --><aop:config><!-- 切入點通知 --><aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.gov.csrc.base.security..*.*(..))"/><aop:advisor advice-ref="txAdvice" pointcut="execution(* *..*Service.*(..))"/></aop:config><aop:config> <aop:pointcut id="logger" expression="execution(* *..*Dao.save*(..))or execution(* *..*Dao.update*(..))or execution(* *..*Dao.delete*(..))or execution(* *..*Dao.batch*(..))" /> <aop:aspect id="loggerAspect" ref="genericLogger"> <aop:around pointcut-ref="logger" method="invoke" /> </aop:aspect> </aop:config> <!-- 任務計劃 --><!-- 要調用的工作 --><bean id="timerAction" class="cn.gov.csrc.report.action.TimerAction"></bean><bean id="praseXmlAction" class="cn.gov.csrc.report.action.PraseXmlAction"></bean><!-- 定義調用對象和調用對象的方法 --><bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><!-- 調用的類 --><property name="targetObject"><ref bean="timerAction"/></property><!-- 調用類中的方法 --><property name="targetMethod"><value>start</value></property><!-- 作業不并發調度 --><property name="concurrent" value="false"/></bean><!-- 定義調用對象和調用對象的方法 --><bean id="jobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><!-- 調用的類 --><property name="targetObject"><ref bean="praseXmlAction"/></property><!-- 調用類中的方法 --><property name="targetMethod"><value>parseXml</value></property><!-- 作業不并發調度 --><property name="concurrent" value="false"/></bean><!-- 定義導出數據到xml的觸發時間 --><bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail"><ref bean="jobtask"/></property><!-- cron表達式 --><property name="cronExpression"><!-- 每天晚上11點59分鐘59秒執行一次 --><!-- <value>0 59 23 * * ?</value> --><value>0 23 14 * * ?</value></property></bean><!-- 定義解析xml的觸發時間 --><bean id="doPraseXml" class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail"><ref bean="jobTask"/></property><!-- cron表達式 --><property name="cronExpression"><value>0 24 14 * * ?</value></property></bean><!-- 總管理類,如果將lazy-init='false'那么容器啟動就會執行調度程序 --><bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref bean="doTime"/><ref bean="doPraseXml"/></list></property></bean></beans>
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;}@Overrideprotected 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目錄下)
<?xml version="1.0" encoding="GBK"?>
<crsc> <data><舉報信息反饋><R index="1"><舉報編號>1</舉報編號><狀態>1</狀態><答復意見>填寫答復意見</答復意見></R><R index="2"><舉報編號>2</舉報編號><狀態>2</狀態><答復意見>填寫答復意見</答復意見></R><R index="3"><舉報編號>3</舉報編號><狀態>3</狀態><答復意見>填寫答復意見</答復意見></R><R index="4"><舉報編號>4</舉報編號><狀態>1</狀態><答復意見>填寫答復意見</答復意見></R></舉報信息反饋></data>
</crsc>
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() {}@Overrideprotected void executeInternal(JobExecutionContext arg0)throws JobExecutionException {// TODO Auto-generated method stubSystem.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<Element> elementList = getElementList();for (Element element : elementList) {Element nameElement = element.getChild("舉報信息反饋");List<Element> 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<Element> 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<Element> elementList = rootElement.getChildren();return elementList;}}