最近由于公司项目需要,学习了在Windows平台导出xml文件到Linux服务器上的指定目录下的方法,(注:这里的我的Linux是在本机上装的虚拟机)现在写下来记录一下!
1.首先是项目截图:
2.主要是类:
TimerAction.java
package cn.gov.csrc.task.action;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;import org.apache.struts2.convention.annotation.Action;
import org.jdom2.Document;
import org.jdom2.Element;
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;@Controller()
@Scope("prototype")
@Action("TimerAction")
public class TimerAction extends QuartzJobBean {private static final String path = "/usr/local/datasource/zip/"+"user.xml";@Overrideprotected void executeInternal(JobExecutionContext context)throws JobExecutionException {}public void start() throws FileNotFoundException, IOException{System.out.println("计划任务开始执行......");BuildXMLDoc();System.out.println("计划任务结束执行......");}public void BuildXMLDoc() throws FileNotFoundException, IOException{Element root = new Element("list");Document doc = new Document(root);for(int i=0;i<5;i++){Element elements = new Element("user");elements.setAttribute("id",""+i);elements.addContent(new Element("name").setText("迷彩风情"));elements.addContent(new Element("age").setText("24"));elements.addContent(new Element("sex").setText("男"));root.addContent(elements);XMLOutputter outputter = new XMLOutputter();outputter.output(doc, new FileOutputStream(path));}}}
3.主要的配置文件:
configuration-base.xml
<?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><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><prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.autoReconnect">true</prop></props></property><property name="packagesToScan"><list><value>cn.gov.csrc.base.systemmanager.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="timerAction" class="cn.gov.csrc.task.action.TimerAction"></bean><!-- 定义调用对象和调用对象的方法 --><bean id="timerTask" 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><!-- 定义导出数据到xml的触发时间 --><bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail"><ref bean="timerTask"/></property><!-- cron表达式 --><property name="cronExpression"><value>0 35 10 * * ?</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"/></list></property></bean></beans>
4.最后附上源码地址:下载请点击这里: 下载地址