xml配置 spring 中设置定时任务

  1. //Test.java
  2. import java.io.IOException;
  3. public class Test {
  4.         /**
  5.          * @param args
  6.          */
  7.         public static void main(String[] args) {
  8.                 try {
  9. Runtime.getRuntime().exec("sh /fxmweb/test/start.sh");
  10.                 } catch (IOException e) {
  11.                         // TODO Auto-generated catch block
  12.                         e.printStackTrace();
  13.                 }
  14.         }
  15. }
  1. //start.sh
  2. ///fxmweb/good/   删除改目录下三天前的文件
  3.  find /fxmweb/domains/tstDomain/cgServer -type f ! -ctime -3 -exec rm -f {} /;

 

以下是xml配置 spring 中设置定时任务

    1. <bean id="forceClearService"
    2.         class="nl.base.service.impl.ForceClearServiceImpl">
    3.         <property name="systemCommands">
    4.             <list>
    5.                 <value>
    6.                 sh /mts/properties/start.sh
    7.                 </value>
    8.             </list>
    9.         </property>
    10.     </bean>
    11.     <bean id="forceClearTrigger"
    12.         class="org.springframework.scheduling.quartz.CronTriggerBean">
    13.         <property name="jobDetail">
    14.             <ref bean="forceClearJob" />
    15.         </property>
    16.         <property name="cronExpression">
    17.             <value>0 * * * * ?</value>
    18.         </property>
    19.     </bean>
    20.     <bean id="scheduler"
    21.         class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    22.         <property name="triggers">
    23.             <list>
    24.                 <ref local="forceClearTrigger" />
    25.             </list>
    26.         </property>
    27.     </bean>
    28.     <bean id="forceClearJob"
    29.         class="org.springframework.scheduling.quartz.JobDetailBean">
    30.         <property name="jobClass">
    31.             <value>nl.base.service.impl.Force</value>
    32.         </property>
    33.         <property name="jobDataAsMap">
    34.             <map>
    35.                 <entry key="forceClear">
    36.                     <ref bean="forceClearService" />
    37.                 </entry>
    38.             </map>
    39.         </property>
    40.     </bean>
  1.  
  2. //Force.java
    1. import org.quartz.JobExecutionContext;
    2. import org.quartz.JobExecutionException;
    3. import org.springframework.scheduling.quartz.QuartzJobBean;
    4. /**
    5.  * 执行系统命令任务类
    6.  * @author cyhgo
    7.  * @since 2008-9-16
    8.  */
    9. public class Force extends QuartzJobBean {
    10.     private ForceClearServiceImpl forceClear;
    11.      protected void executeInternal(JobExecutionContext context)  throws JobExecutionException 
    12.      {
    13.          forceClear.executeComm();
    14.      }
    15.     public ForceClearServiceImpl getForceClear() {
    16.         return forceClear;
    17.     }
    18.     public void setForceClear(ForceClearServiceImpl forceClear) {
    19.         this.forceClear = forceClear;
    20.     }
    21. }
    1. //ForceClearServiceImpl .java
    2. import java.io.IOException;
    3. import java.util.Iterator;
    4. import java.util.List;
    5. /**
    6.  * 执行系统命令任务类
    7.  * @author cyhgo
    8.  * @since 2008-9-16
    9.  */
    10. public class ForceClearServiceImpl{
    11.     /**
    12.      * 系统命令集
    13.      * 清除三天前的所有强平信息
    14.      */
    15.     private List systemCommands;
    16.      public void executeComm()
    17.      {
    18.          if(systemCommands==null) System.out.println("---------");
    19.         for (Iterator it = systemCommands.iterator(); it.hasNext();) {
    20.             String cmd = (String) it.next();
    21.             System.out.println(cmd);
    22.             try {
    23.                 Runtime.getRuntime().exec(cmd);
    24.             } catch (IOException e) {
    25.                 e.printStackTrace();
    26.             }
    27.         }
    28.     }
    29.     public List getSystemCommands() {
    30.         return systemCommands;
    31.     }
    32.     public void setSystemCommands(List systemCommands) {
    33.         this.systemCommands = systemCommands;
    34.     }
    35. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值