读书人

Spring的定时监听应用例

发布时间: 2012-09-06 10:37:01 作者: rapoo

Spring的定时监听使用例

<!-- 查询已过期客户列表 --> <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) =0and cus.input_date<=getdate()-30]]> </select>

/** * 定时去除过期客户档案业务员 * @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))

/** * 批量修改客户档案信息 * @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; } });}

<bean id="methodInvokingJobWips" 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表达式设置定时时间。

?

读书人网 >编程

热点推荐