论坛首页 入门技术论坛

配置Spring的定时器

浏览 1209 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (5) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-04-02   最后修改:2010-12-05
applicationContext.xml:

 <!-- 定时器的实验开始 -->
    <bean id="comrepeatingTrigger"
    	class="org.springframework.scheduling.timer.ScheduledTimerTask">
    	
    	<!--启动一秒后执行 -->
    	<property name="delay">
    		<value>61000</value>
    	</property>
    	
    	<!--每分钟执行一次 -->
    	<property name="period">
    		<value>60000</value>
    	</property>
    	
    	<!--注入要监控的javaBean -->
    	<property name="timerTask">
    		<ref bean="companyTask" />
    	</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="comrepeatingTrigger" />
    		</list>
    	</property>
    </bean>
     -->
    
    <bean id="companyTask" class="com.here.web.common.CompanySampleTask">
        <property name="hereProductService">
            <ref bean="hereProductService"/>
        </property>
        <property name="areaChangeUtil">
            <ref bean="areaChangeUtil"/>
        </property>
        <property name="hereUserService">
            <ref bean="hereUserService"/>
        </property>
        <property name="hereCorporationService">
            <ref bean="hereCorporationService"/>
        </property>
    </bean>
    <!-- 定时器的实验结束 -->

用法:

public class CompanySampleTask extends TimerTask{   
	private HereProductService hereProductService;
	private AreaChangeUtil areaChangeUtil;
	private HereUserService hereUserService;
	private HereCorporationService hereCorporationService;
	private SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");

//----------------------------------产品接口	
	public HereProductService getHereProductService() {
		return hereProductService;
	}
	public void setHereProductService(HereProductService hereProductService) {
		this.hereProductService = hereProductService;
	}
	
//----------------------------------地区接口
	public AreaChangeUtil getAreaChangeUtil() {
		return areaChangeUtil;
	}
	public void setAreaChangeUtil(AreaChangeUtil areaChangeUtil) {
		this.areaChangeUtil = areaChangeUtil;
	}

//----------------------------------用户接口
	public HereUserService getHereUserService() {
		return hereUserService;
	}
	public void setHereUserService(HereUserService hereUserService) {
		this.hereUserService = hereUserService;
	}

//----------------------------------企业接口
	public HereCorporationService getHereCorporationService() {
		return hereCorporationService;
	}
	public void setHereCorporationService(
			HereCorporationService hereCorporationService) {
		this.hereCorporationService = hereCorporationService;
	}

	public void run() {
		String HQL = "from HereUser hereUser";
		List hereUsers = this.hereUserService.queryByHQL(HQL);
		if(hereUsers.size()>0) {
			for(int i=0; i<hereUsers.size(); i++) {
				HereUser hereUser = (HereUser)hereUsers.get(i);
				HereCorporation hereCorporation = this.hereCorporationService.queryById(hereUser.getUserId(), HereCorporation.class);
				System.out.println("重新生成静态页面CompanySampleTask,时间:" + formatter.format(new Date()));
				new CompanyHtmlUtil().buildCompanyHtml(hereUser, hereCorporation, areaChangeUtil, hereProductService);
		    	
		    	//System.out.println("重新刷新一下产品列表中的类别信息!,时间:" + formatter.format(new Date()));
		    	//classRelatedTempUtil.setClassRelatedMap();
			}
		}

    }
}


论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics