<!-- applicationContext.xml 中配置 --> <!-- 任务调度 --> <bean name="TaskJob" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="cn.gov.bjkepu.www.manage.base.task.job.TaskJob" /> </bean> <!-- 定义任务调度 触发时间 --> <bean id="doTaskTime" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="TaskJob"/> </property> <!-- cron表达式 --> <property name="cronExpression"> <value>0 0 2 * * ?</value> <!--<value>0 0/1 * * * ?</value> --> </property> </bean> <!-- 调度器 --> <bean id="startQuertz" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="doTaskTime"/> </list> </property> </bean>
/** *继承 QuartzJobBean 类 重写 executeInternal 方法 * */ package cn.gov.bjkepu.www.manage.base.task.job; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.sql.DriverManager; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Properties; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.quartz.QuartzJobBean; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; import cn.gov.bjkepu.www.manage.base.task.model.TTask; import cn.gov.bjkepu.www.util.dao.UtilDao; import cn.gov.bjkepu.www.util.service.UtilService; /** * 业务任务Job * @author zxj */ @SuppressWarnings("all") public class TaskJob extends QuartzJobBean { private Connection getConnection(){ try { Properties pro = new Properties(); InputStream ipt = new FileInputStream(new File(this.getClass().getResource("/jdbc.properties").toURI())); pro.load(ipt); // 装入数据库特定的驱动器 Class.forName(pro.getProperty("jdbc.driverClassName")); String user = pro.getProperty("jdbc.username"); String password = pro.getProperty("jdbc.password"); String url = pro.getProperty("jdbc.url"); // 与数据库建立连接 return (Connection)DriverManager.getConnection(url ,user ,password); } catch (Exception e) { e.printStackTrace(); return null; } } @Override protected void executeInternal(JobExecutionContext arg0)throws JobExecutionException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date currentTime = new Date(); System.out.println(sdf.format(System.currentTimeMillis())+"任务调度开始!"); Connection conn = this.getConnection(); PreparedStatement pst = null; try { if (conn != null){ pst = (PreparedStatement) conn.prepareStatement("update t_task set task_state = 2 where now() > end_time and is_dele = 0 and task_state != 2"); pst.execute(); System.out.println(sdf.format(System.currentTimeMillis())+"成功结束"+pst.getUpdateCount()+"条任务"); pst = (PreparedStatement) conn.prepareStatement("update t_task set task_state = 1 where now() > start_time and now() < end_time and is_dele = 0 and task_state != 1"); pst.execute(); System.out.println(sdf.format(System.currentTimeMillis())+"成功开启"+pst.getUpdateCount()+"条任务"); pst = (PreparedStatement) conn.prepareStatement("update d_pro_type set state=2 where now()>end_time and is_dele=0 and state!=2"); pst.execute(); System.out.println(sdf.format(System.currentTimeMillis())+"成功结束"+pst.getUpdateCount()+"条任务"); pst = (PreparedStatement) conn.prepareStatement("update d_pro_type set state=1 where now()>start_time and now()<end_time and is_dele=0 and state!=1"); pst.execute(); System.out.println(sdf.format(System.currentTimeMillis())+"成功开启"+pst.getUpdateCount()+"条任务"); } } catch (Exception e) { e.printStackTrace(); } finally{ try { pst.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } System.out.println(sdf.format(System.currentTimeMillis())+"任务调度结束!"); } }
这种比较灵活 配置文件直接找到类中的方法实现调度 配置文件中-------->> <!-- 任务调度对象 --> <bean id="taskQuertz" autowire="no" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="taskService"/> </property> <property name="targetMethod"> <value>startOrEnd</value> </property> </bean> <!-- 定义任务调度 触发时间 --> <bean id="doTaskTime" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="taskQuertz"/> </property> <!-- cron表达式 --> <property name="cronExpression"> <value>0 0 23 * * ?</value> </property> </bean>
<!-- 调度器 -->
<bean id="startQuertz" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTaskTime"/>
<ref bean="doAssessmentTimeOne"/>
<ref bean="doAssessmentTimeTwo"/>
<ref bean="doAssessmentTimeThree"/>
<ref bean="doXwqybzhService"/>
<!-- 统计分析r语言出图 -->
<!--首页统计数据 -->
<ref bean="doIndexDataService"/>
<ref bean="doIndexDataService2"/>
</list>
</property>
</bean>
调度方法 ------->> package com.xcaj.www.manage.taskSet.service.impl; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.xcaj.www.manage.assessment.assessmentOne.model.TAssessmentOne; import com.xcaj.www.manage.assessment.assessmentOne.model.TOfficeScoreOne; import com.xcaj.www.manage.assessment.assessmentOne.model.TStandardScoreOne; import com.xcaj.www.manage.assessment.assessmentThree.model.TAssessmentThree; import com.xcaj.www.manage.assessment.assessmentThree.model.TStandardScoreThree; import com.xcaj.www.manage.assessment.assessmentTwo.model.TAssessmentTwo; import com.xcaj.www.manage.assessment.assessmentTwo.model.TOfficeScoreTwo; import com.xcaj.www.manage.assessment.assessmentTwo.model.TStandardScoreTwo; import com.xcaj.www.manage.dictionary.annual.model.DAnnual; import com.xcaj.www.manage.performanceAppraisal.model.TCheckTask; import com.xcaj.www.manage.performanceAppraisal.model.TSafetyInformation; import com.xcaj.www.manage.taskSet.model.TTask; import com.xcaj.www.manage.taskSet.service.TaskService; import com.xcaj.www.manage.weekCheck.model.Ledger; import com.xcaj.www.manage.workCount.model.TAccidentDetail; import com.xcaj.www.manage.workCount.model.TAccidentSituation; import com.xcaj.www.manage.workCount.model.TEnforceCheck; import com.xcaj.www.manage.workCount.model.TStatisticsForm; import com.xcaj.www.util.dao.UtilDao; /** * 任务接口实现类 * @author zhaohu * @date 2014年11月12日 15:45:19 */ @Service("taskService") public class TaskServiceImpl implements TaskService { @Autowired private UtilDao utilDao; /** * 任务保存方法 */ @Transactional(propagation = Propagation.REQUIRED, rollbackFor = java.lang.Exception.class) public void saveTask(String taskTypeFlag, TTask task, String feyTaskId, String yearId,String batch, String startTimeStr, String endTimeStr,String weekNameStart,String weekNameEnd,String weeks,String planReportStr)throws Exception { SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); task.setTaskType(Integer.parseInt(taskTypeFlag.trim())); if("9".equals(taskTypeFlag) || "10".equals(taskTypeFlag)){ if(feyTaskId != null && !"".equals(feyTaskId)){ TTask fkTask = (TTask) this.utilDao.getObjectById(TTask.class, feyTaskId); if(fkTask != null && !"".equals(fkTask)){ task.setFkTaskId(fkTask); } } } DAnnual year = (DAnnual) this.utilDao.getObjectById(DAnnual.class, yearId.trim()); if(year != null && !"".equals(year)){ task.setFkYearId(year); if(batch != null && !"".equals(batch)){ task.setTaskFlag(year.getAnnualCode()+batch);//taskFlag task.setMonth(Integer.parseInt(batch)); }else { task.setTaskFlag(year.getAnnualCode()+"01");//taskFlag } } //当前时间 Date currentTime = new Date(); //开始时间 Date startTime = format.parse(startTimeStr); //结束时间 Date endTime = format.parse(endTimeStr); if(startTime != null && endTime != null){ if(currentTime.before(startTime)){ task.setTaskState(0);//未开始 } if(currentTime.after(endTime)){ task.setTaskState(2);//已结束 } if(currentTime.after(startTime)&¤tTime.before(endTime)){ task.setTaskState(1);//进行中 } task.setStartTime(startTime); task.setEndTime(endTime); } task.setState(0); task.setCreateTime(new Date()); task.setIsDele(0); //周报任务 //判断任务类型 if("11".equals(taskTypeFlag) || "12".equals(taskTypeFlag) || "13".equals(taskTypeFlag)){ StringBuffer taskName = new StringBuffer(""); taskName.append(weekNameStart); taskName.append("-"); taskName.append(weekNameEnd); taskName.append("周报任务"); task.setTaskName(taskName.toString()); task.setWeekWeek(weeks); } //一日一报 if("15".equals(taskTypeFlag) || "16".equals(taskTypeFlag) || "17".equals(taskTypeFlag)){ task.setTaskName(weekNameStart+"一日一报任务"); task.setWeekWeek(weeks); task.setPlanReport(sdf.parse(planReportStr)); task.setTaskFlag(planReportStr); } this.utilDao.save(task); } /** * 任务修改方法 */ @Transactional(propagation = Propagation.REQUIRED, rollbackFor = java.lang.Exception.class) public void updateTask(String taskId, TTask task, String feyTaskId, String yearId,String batch, String startTimeStr, String endTimeStr,String weekNameStart,String weekNameEnd,String weeks,String planReportStr)throws Exception { SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); TTask taskOld = (TTask) this.utilDao.getObjectById(TTask.class, taskId.trim()); if(taskOld != null && !"".equals(taskOld)){ DAnnual year = (DAnnual) this.utilDao.getObjectById(DAnnual.class, yearId.trim()); if(year != null && !"".equals(year)){ taskOld.setFkYearId(year); if(batch!= null && !"".equals(batch)){ taskOld.setTaskFlag(year.getAnnualCode()+batch); taskOld.setMonth(Integer.parseInt(batch)); }else { taskOld.setTaskFlag(year.getAnnualCode()+"01"); } } if("11".equals(taskOld.getTaskType().toString()) || "12".equals(taskOld.getTaskType().toString()) || "13".equals(taskOld.getTaskType().toString())){ StringBuffer taskName = new StringBuffer(""); taskName.append(weekNameStart); taskName.append("-"); taskName.append(weekNameEnd); taskName.append("周报任务"); taskOld.setTaskName(taskName.toString()); taskOld.setWeekWeek(weeks); }else if("15".equals(taskOld.getTaskType().toString()) || "16".equals(taskOld.getTaskType().toString()) || "17".equals(taskOld.getTaskType().toString())){ taskOld.setTaskName(weekNameStart+"一日一报任务"); taskOld.setWeekWeek(weeks); taskOld.setPlanReport(sdf.parse(planReportStr)); taskOld.setTaskFlag(planReportStr); }else{ taskOld.setTaskName(task.getTaskName()); } if("9".equals(taskOld.getTaskType()) || "10".equals(taskOld.getTaskType())){ if(feyTaskId != null && !"".equals(feyTaskId)){ TTask fkTask = (TTask) this.utilDao.getObjectById(TTask.class, feyTaskId); if(fkTask != null && !"".equals(fkTask)){ task.setFkTaskId(fkTask); } } } //当前时间 Date currentTime = new Date(); //开始时间 Date startTime = format.parse(startTimeStr); //结束时间 Date endTime = format.parse(endTimeStr); if(startTime != null && endTime != null){ if(currentTime.before(startTime)){ taskOld.setTaskState(0);//未开始 } if(currentTime.after(endTime)){ taskOld.setTaskState(2);//已结束 } if(currentTime.after(startTime)&¤tTime.before(endTime)){ taskOld.setTaskState(1);//进行中 } taskOld.setStartTime(startTime); taskOld.setEndTime(endTime); } taskOld.setUpdateTime(new Date()); this.utilDao.update(taskOld); } } /** * 设置任务管理端状态 */ @Transactional(propagation = Propagation.REQUIRED, rollbackFor = java.lang.Exception.class) public void setState(String taskId, String stateVal) throws Exception { TTask taskOld = (TTask) this.utilDao.getObjectById(TTask.class, taskId.trim()); if(taskOld != null && !"".equals(taskOld)){ if(stateVal != null && "0".equals(stateVal)){ taskOld.setState(0); }else if(stateVal != null && "1".equals(stateVal)){ taskOld.setState(1); } taskOld.setUpdateTime(new Date()); this.utilDao.update(taskOld); } } /** * 任务删除方法 */ @SuppressWarnings("all") @Transactional(propagation = Propagation.REQUIRED, rollbackFor = java.lang.Exception.class) public void delTask(String idStr,String taskTypeFlag) throws Exception { TTask taskOld = null; String[] ids = idStr.split(","); if(ids != null && ids.length > 0){ for (String id : ids) { taskOld = (TTask) this.utilDao.getObjectById(TTask.class, id.trim()); if(taskOld != null && !"".equals(taskOld)){ taskOld.setIsDele(1); this.utilDao.update(taskOld); } //删除工作统计任务 if("1".equals(taskTypeFlag) || "2".equals(taskTypeFlag) || "3".equals(taskTypeFlag)){ //删除所属任务的统计单 List<TStatisticsForm> formList = (List<TStatisticsForm>) utilDao.getHqlAll(" from TStatisticsForm t left join fetch t.fkTaskId f where t.isDele = 0 and f.id = '"+id+"' "); if(formList.size()>0){ for(TStatisticsForm form : formList){ //删除所属统计单的执法检查表 List<TEnforceCheck> checkList = (List<TEnforceCheck>) utilDao.getHqlAll(" from TEnforceCheck t left join fetch t.fkStatisticsFormId f where t.isDele = 0 and f.id = '"+form.getId()+"' "); if(checkList != null && checkList.size()>0){ for(TEnforceCheck check : checkList){ utilDao.delete(check); } } List<TAccidentSituation> accidentSituationList = (List<TAccidentSituation>) utilDao.getHqlAll(" from TAccidentSituation t left join fetch t.fkStatisticsFormId where t.isDele = 0 and t.fkStatisticsFormId.id = '"+form.getId()+"' "); if(accidentSituationList != null && accidentSituationList.size()>0){ for(TAccidentSituation situ : accidentSituationList){ List<TAccidentDetail> detailList = (List<TAccidentDetail>) utilDao.getHqlAll(" from TAccidentDetail t left join fetch t.fkAccidentSituationId where t.isDele = 0 and t.fkAccidentSituationId.id = '"+situ.getId()+"' "); //删除事故详细表 for(TAccidentDetail detail :detailList){ utilDao.delete(detail); } //删除事故情况表 utilDao.delete(situ); } } utilDao.delete(form); } } } //周报 if("11".equals(taskTypeFlag) || "12".equals(taskTypeFlag) || "13".equals(taskTypeFlag)){ List<Ledger> ledList = (List<Ledger>) utilDao.getHqlAll("from Ledger d where d.fkWeekId.fkTask.id = '"+id+"'"); for (Ledger ledger : ledList) { utilDao.delete(ledger); } utilDao.deleteAll("update TWeek set isDele = 1 where fkTask.id = '"+id+"'"); } //一日一报 if("15".equals(taskTypeFlag) || "16".equals(taskTypeFlag) || "17".equals(taskTypeFlag)){ utilDao.deleteAll("update TDaily set isDele = 1 where fkTaskId.id = '"+id+"'"); } //删除街道对委办局考评任务 if("5".equals(taskTypeFlag)){ //循环删除 List<TStandardScoreThree> stanList = (List<TStandardScoreThree>) utilDao.getHqlAll("from TStandardScoreThree d where d.fkAssessmentId.fkTaskId.id = '"+id+"'"); for (TStandardScoreThree sst : stanList) { utilDao.delete(sst); } List<TAssessmentThree> assThree = (List<TAssessmentThree>) utilDao.getHqlAll("from TAssessmentThree where fkTaskId.id = '"+id+"'"); for (TAssessmentThree item : assThree) { utilDao.delete(item); } } //删除街道年度自评任务 || 安委会对街道考评 if("8".equals(taskTypeFlag) || "7".equals(taskTypeFlag)){ //Type(2); 自评任务 List<TStandardScoreOne> stanOneList = (List<TStandardScoreOne>) utilDao.getHqlAll("from TStandardScoreOne d where d.isDele = 0 and d.fkAssessmentId.fkTaskId.id = '"+id+"'"); for (TStandardScoreOne tStandardScoreOne : stanOneList) { utilDao.delete(tStandardScoreOne); } List<TOfficeScoreOne> offOneList = (List<TOfficeScoreOne>) utilDao.getHqlAll("from TOfficeScoreOne d where d.isDele = 0 and d.fkTaskId.id = '"+id+"'"); for (TOfficeScoreOne tOfficeScoreOne : offOneList) { utilDao.delete(tOfficeScoreOne); } List<TAssessmentOne> assOneList = (List<TAssessmentOne>) utilDao.getHqlAll("from TAssessmentOne d where d.isDele = 0 and d.fkTaskId.id = '"+id+"'"); for (TAssessmentOne tAssessmentOne : assOneList) { utilDao.delete(tAssessmentOne); } } //委办局自评任务 || 安委会对委办局考评 if("6".equals(taskTypeFlag) || "4".equals(taskTypeFlag)){ List<TStandardScoreTwo> stanTwoList = (List<TStandardScoreTwo>) utilDao.getHqlAll("from TStandardScoreTwo d where d.isDele = 0 and d.fkAssessmentId.fkTaskId.id = '"+id+"'"); for (TStandardScoreTwo stan : stanTwoList) { utilDao.delete(stan); } List<TOfficeScoreTwo> offTwoList = (List<TOfficeScoreTwo>) utilDao.getHqlAll("from TOfficeScoreTwo d where d.isDele = 0 and d.fkTaskId.id = '"+id+"'"); for (TOfficeScoreTwo offTwo : offTwoList) { utilDao.delete(offTwo); } List<TAssessmentTwo> assTwoList = (List<TAssessmentTwo>) utilDao.getHqlAll("from TAssessmentTwo d where d.fkTaskId.id = '"+id+"'"); for (TAssessmentTwo item : assTwoList) { utilDao.delete(item); } } if( "18".equals(taskTypeFlag) || "19".equals(taskTypeFlag)){ utilDao.deleteAll("update TSafetyInformationFileRecord set isDele = 1 where fkTaskId.id = '"+id+"'"); } } } } /** * 开始或结束任务 * 创建人:zxj * 创建时间:2015-1-28 上午10:31:18 * @version 1.0 */
public void startOrEnd() throws Exception { Date now = new Date(); List<TTask> taskList = (List<TTask>) this.utilDao.getHqlAll("from TTask d where d.isDele = 0"); if(taskList != null && taskList.size() > 0){ for(TTask task : taskList){ if(now.before(task.getStartTime())){ task.setTaskState(0); }else if(now.after(task.getEndTime())){ task.setTaskState(2); }else if(now.after(task.getStartTime()) && now.before(task.getEndTime())){ task.setTaskState(1); } this.utilDao.update(task); } } List<TCheckTask> taskList2 = (List<TCheckTask>) this.utilDao.getHqlAll("from TCheckTask d where d.isDele = 0"); if(taskList2 != null && taskList2.size() > 0){ for(TCheckTask task : taskList2){ if(task.getPlanStartTime()!=null&&task.getPlanEndTime()!=null){ if(now.before(task.getPlanStartTime())){ task.setPlanState(0); }else if(now.after(task.getPlanEndTime())){ task.setPlanState(2); }else if(now.after(task.getPlanStartTime()) && now.before(task.getPlanEndTime())){ task.setPlanState(1); } } if(task.getSummaryStartTime()!=null&&task.getSummaryEndTime()!=null){ if(now.before(task.getSummaryStartTime())){ task.setSummaryState(0); }else if(now.after(task.getSummaryEndTime())){ task.setSummaryState(2); }else if(now.after(task.getSummaryStartTime()) && now.before(task.getSummaryEndTime())){ task.setSummaryState(1); } } if(task.getCheckStartTime()!=null&&task.getCheckEndTime()!=null){ if(now.before(task.getCheckStartTime())){ task.setCheckState(0); }else if(now.after(task.getCheckEndTime())){ task.setCheckState(2); }else if(now.after(task.getCheckStartTime()) && now.before(task.getCheckEndTime())){ task.setCheckState(1); } } this.utilDao.update(task); } } List<String> informationids=(List<String>) utilDao.getHqlAll("select id from TSafetyInformation where isDele=0 and isApplied=1"); if(informationids!=null&&informationids.size()>0){ for(String id:informationids){ int count=utilDao.getHqlCount("select count(*) from TMonthlyPerformanceAppraisal where isDele=0 and type=1 and state=3 and fkDafetyId.id='"+id+"'"); if(count>=2){ utilDao.updateAll("update TSafetyInformation set isApplied=0 where id='"+id+"'"); } } } }
public static void removeEvens(List<Integer> list){ for(Integer x :list){ if(x%2 == 0){ list.remove(x); } } } public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); for(int i = 0 ; i < 50 ; i++){ list.add(i); } removeEvens(list); } }