posts - 13,comments - 19,trackbacks - 0
现在想要获得在特定时间或者指定时间执行相应功能
有两种做法
在applicationContext.xml中写入如下Bean
<bean id="repeatingTrigger"
   class="org.springframework.scheduling.timer.ScheduledTimerTask">
   <!--启动一秒后执行 -->
   <property name="delay">
    <value>1000</value>
   </property>
   <!--每隔一小时执行一次 -->
   <property name="period">
    <value>3600000</value>
   </property>
   <!--注入要监控的javaBean -->
   <property name="timerTask">
    <ref bean="task" />
   </property>
   <!--类型是否为fixedRate型,默认为fixedDelay-->
   <property name="fixedRate">
    <value>true</value>
   </property>


</bean>
<bean id="scheduler"
   class="org.springframework.scheduling.timer.TimerFactoryBean">
   <property name="scheduledTimerTasks">
    <list>
     <ref bean="repeatingTrigger" />
    </list>
   </property>
</bean>

<bean id="task" class="com.css.wam.portlet.SampleTask">
   <property name="workService">
    <ref bean="workService" />
   </property>
   <property name="work">
    <ref bean="work" />
   </property>
</bean>

然后写SampleTask类,如下:

 

package com.css.wam.portlet;

 

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.TimerTask;

 

 

import javax.servlet.ServletContext; 字串2

import org.apache.jetspeed.security.SecurityException; 字串9

import com.css.wam.service.WorkService; 字串1

@SuppressWarnings("unused")
class SampleTask extends TimerTask{    字串2

    private static final int C_SCHEDULE_HOUR = 23;//设置指定时间
    private WorkService workService;
    private List users;
    private List teams;
    private WorkPortlet work;
   
    public void setWorkService(WorkService workService) {
   this.workService = workService;
} 字串7

public void setWork(WorkPortlet work) {
   this.work = work;
} 字串4

    public SampleTask(){
    }
   
    @SuppressWarnings("unchecked")
public void run() {
   
        Calendar cal = Calendar.getInstance();
        try {
          users = work.getUsers();
          teams = new ArrayList();
       for(Iterator it = users.iterator(); it.hasNext();)
       {
        String teamname = work.getGroupsByUser((String)it.next());
        teams.add(teamname);
       }
            //查看当前时间与指定是否一致,一致则执行任务
             if (C_SCHEDULE_HOUR == cal.get(Calendar.HOUR_OF_DAY) ) 字串6
                 workService.autoWorkOff(users, teams);    字串8

        } catch (SecurityException e) {
     e.printStackTrace();
    }
      }
}

posted on 2008-06-25 17:14 南山隐士 阅读(1390) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: