Spring的定时监听使用例

需求如下:

客户档案录入一个月后,还没有签订合同的,需要提示业务经理,看是否需要更换业务员。是否更换取决于业务经理本人。

定时监听器的功能:每天定时清除已过期的客户档案对应的业务员。

 

做法如下:

springquartz实现定时作业。操作前需要准备一个jar文件和一个xml文件:

quartz-all-1.6.0.jarquartz.properties

第一步:查询出客户档案中已过期的客户档案。

以下是客户档案映射文件的查询语句:查询出未签订合同并已超过一个月的客户

<!-- 查询已过期客户列表 -->
  <select id="queryOutOfDateCustomerList" resultMap="abatorgenerated_CustomerrecordtblResult" >
  		<![CDATA[
		 select *  from CUSTOMER_RECORD_TBL cus where (
			select count(*) from contract_tbl con where cus.customer_id=con.customer_id) =0
		and cus.input_date<=getdate()-30
		]]>
  </select>

 

第二步:在Service中添加定时方法:

/**
     * 定时去除过期客户档案业务员
     * @throws Exception
     */
    public void updateOutOfDateTimer()throws Exception{
    	try {
    		 String startTimerOnCustomerRecord= Toolkit.getInstance().getSingleConfig("startTimerOnCustomerRecord");
             if("no".equalsIgnoreCase(startTimerOnCustomerRecord)){

	    		System.out.println("【"+new SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date())+"】,定时更改开始");
	    		//查询出已过期的客户
	    		List<Customerrecordtbl> customers=customerrecordtblDAO.queryOutOfDateCustomerList();
	    		for(Customerrecordtbl customer:customers){
	    			customer.setSalesman("");//除去业务员
	    		}
	    		//执行批量修改
	    		customerrecordtblDAO.updateProcessBatch(customers);
	    		System.out.println("【"+new SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date())+"】,定时更改结束!");
             }
		} catch (SQLException e) {
			logger.error("定时去除过期客户档案对应业务员:" + e.getMessage(), e);
			throw new CustomerRecordException("定时去除过期客户档案对应业务员:",e);
		}
    }


提示:该方法不必在接口中声明。

上面的方法中使用了配置文件的开关,大可不必去理会。内容如下:

String startTimerOnCustomerRecord= Toolkit.getInstance().getSingleConfig("startTimerOnCustomerRecord");
             if("no".equalsIgnoreCase(startTimerOnCustomerRecord))

 

这里还用了iBATIS的批量修改的方法,具体内容如下:

/**
     * 批量修改客户档案信息
     * @param list
     * @throws SQLException
     */
    public void updateProcessBatch(final List<Customerrecordtbl> list) throws SQLException {

		this.getSqlMapClientTemplate().execute(new SqlMapClientCallback(){
		    public Object doInSqlMapClient(SqlMapExecutor executor)
		            throws SQLException {
		    executor.startBatch();
		    int batch = 0;
		    for(Customerrecordtbl vo:list){
		    //调用获取sequence的方法。如果没有的话就去掉这行代码。
		    //inCodeVO.setInside_Number_Id(getNextId());
		    //参数1为:ibatis中需要执行的语句的id		    executor.update("CUSTOMER_RECORD_TBL.abatorgenerated_updateByPrimaryKeySelective", vo);
		    batch++;
		    //每200条批量提交一次
		    if(batch==200){
		    executor.executeBatch();
		    batch = 0;
		    }
		    }
		    executor.executeBatch();
		    return null;
		    }
		    });
	}

 

第三步:在spring配置文件中配置


 <bean id="methodInvokingJobWips" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
     		<property name="targetObject">
     			<ref bean="customerRecordMan"/>//Service注入类
     		</property>
          	<property name="targetMethod">
          		<value>updateOutOfDateTimer</value>//定时修改的方法
          	</property>
    </bean>
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    		<property name="jobDetail">
    			<ref bean="methodInvokingJobWips"/>
    		</property>
    		<property name="cronExpression">
    			<value>0 0/1 8-22 * * ?</value>//cron表达式
    		</property>
    </bean>
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
			<property name="triggers">
				<list><ref local="cronTrigger"/></list>
			</property>
	</bean>

 要注意:定时器在每次重启服务时,均会调用。其次再根据上面红色部分的cron表达式设置定时时间。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值