Java tutorial
/* * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.wiiyaya.provider.main.service.impl; import com.mysema.query.jpa.JPQLQuery; import com.wiiyaya.framework.common.exception.BusinessException; import com.wiiyaya.framework.common.model.easyui.EasyUiDataGrid; import com.wiiyaya.framework.common.utils.StringUtils; import com.wiiyaya.framework.provider.tools.JF; import com.wiiyaya.provider.main.constant.MainConstant; import com.wiiyaya.provider.main.entity.Batch; import com.wiiyaya.provider.main.model.BatchDto; import com.wiiyaya.provider.main.repository.BatchDao; import com.wiiyaya.provider.main.repository.querydsl.BatchQry; import com.wiiyaya.provider.main.service.BatchService; import com.wiiyaya.provider.main.utils.BatchHelper; import org.quartz.CronExpression; import org.quartz.JobDetail; import org.quartz.JobKey; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.Trigger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.scheduling.quartz.SchedulerFactoryBean; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; /** * <p></p> * <p> * <p>??</p> * <p> * <p></p> * * @author wiiyaya */ @Service public class BatchServiceImpl implements BatchService { @Autowired private BatchDao batchDao; private Scheduler scheduler; @Autowired public BatchServiceImpl(SchedulerFactoryBean schedulerFactoryBean) { scheduler = schedulerFactoryBean.getObject(); } @Override public EasyUiDataGrid<BatchDto> getAllBatchs(BatchDto batchDto, Pageable pageRequest) { List<BatchDto> batchDtoList = new ArrayList<>(); Page<Batch> batchDbs = batchDao.findAllJF(new JF() { @Override public void prepareQry(JPQLQuery qry, boolean notCountQry) { BatchQry.searchBatch(qry, notCountQry, batchDto); } }, pageRequest); for (Batch batch : batchDbs) { BatchDto temp = new BatchDto(); temp.setId(batch.getId()); temp.setTaskName(batch.getTaskName()); temp.setTaskGroup(batch.getTaskGroup()); temp.setTaskClass(batch.getTaskClass()); temp.setStartDate(batch.getStartDate()); temp.setEndDate(batch.getEndDate()); temp.setHolidayRest(batch.isHolidayRest()); temp.setSimpleTask(batch.isSimpleTask()); if (batch.isSimpleTask()) { temp.setWorkingDay(batch.getWorkingDay()); temp.setRepeatCount(batch.getRepeatCount()); temp.setRepeatInterval(batch.getRepeatInterval()); } else { temp.setCronExpression(batch.getCronExpression()); } batchDtoList.add(temp); } return new EasyUiDataGrid<>(batchDtoList, batchDbs.getTotalElements()); } @Transactional(rollbackFor = BusinessException.class) @Override public void saveBatch(BatchDto batchDto) throws BusinessException { if (batchDao.isBatchNameExist(batchDto.getTaskName()) > 0L) { throw new BusinessException(MainConstant.ERROR_BATCH_NAME_EXISTS, batchDto.getTaskName()); } Batch batch = new Batch(); prepareBatchDb(batchDto, batch); batchDao.save(batch); runBatch(batch); } @Transactional(readOnly = true) @Override public BatchDto getBatchById(Long batchId) { Batch batch = batchDao.findOne(batchId); BatchDto batchDto = new BatchDto(); batchDto.setId(batch.getId()); batchDto.setVersion(batch.getVersion()); batchDto.setTaskName(batch.getTaskName()); batchDto.setTaskGroup(batch.getTaskGroup()); batchDto.setTaskClass(batch.getTaskClass()); batchDto.setStartDate(batch.getStartDate()); batchDto.setEndDate(batch.getEndDate()); batchDto.setMisfireType(batch.getMisfireType()); batchDto.setHolidayRest(batch.isHolidayRest()); batchDto.setSimpleTask(batch.isSimpleTask()); if (batch.isSimpleTask()) { batchDto.setWorkingDay(batch.getWorkingDay()); batchDto.setRepeatCount(batch.getRepeatCount()); batchDto.setRepeatInterval(batch.getRepeatInterval()); } else { batchDto.setYearType(batch.getYearType()); batchDto.setRepeatIntervalYear(batch.getRepeatIntervalYear()); batchDto.setCronExYear(batch.getCronExYear()); batchDto.setMonthType(batch.getMonthType()); batchDto.setCronExMonth(StringUtils.split(batch.getCronExMonth(), StringUtils.COMMA)); batchDto.setDayType(batch.getDayType()); batchDto.setCronExDay(StringUtils.split(batch.getCronExDay(), StringUtils.COMMA)); batchDto.setLastDayOffset(batch.getLastDayOffset()); batchDto.setNearestDay(batch.getNearestDay()); batchDto.setCronExWeek(StringUtils.split(batch.getCronExWeek(), StringUtils.COMMA)); batchDto.setLastWeek(batch.getLastWeek()); batchDto.setWeekNo(batch.getWeekNo()); batchDto.setWeek(batch.getWeek()); batchDto.setHourType(batch.getHourType()); batchDto.setCronExHour(StringUtils.split(batch.getCronExHour(), StringUtils.COMMA)); batchDto.setMinuteType(batch.getMinuteType()); batchDto.setCronExMinute(StringUtils.split(batch.getCronExMinute(), StringUtils.COMMA)); batchDto.setSecondType(batch.getSecondType()); batchDto.setCronExSecond(StringUtils.split(batch.getCronExSecond(), StringUtils.COMMA)); } return batchDto; } @Transactional @Override public void updateBatch(BatchDto batchDto) throws BusinessException { Batch batchDb = batchDao.findOne(batchDto.getId()); prepareBatchDb(batchDto, batchDb); batchDao.save(batchDb); runBatch(batchDb); } @Override public void deleteBatch(Long batchId) throws BusinessException { Batch batch = batchDao.findOne(batchId); try { scheduler.deleteJob(new JobKey(batch.getTaskName(), batch.getTaskGroup())); } catch (SchedulerException e) { throw new BusinessException(MainConstant.ERROR_BATCH_DELETE_FAILD); } batchDao.delete(batchId); } @Override public void invokeBatch(Long batchId) throws BusinessException { Batch batch = batchDao.findOne(batchId); try { scheduler.triggerJob(new JobKey(batch.getTaskName(), batch.getTaskGroup())); } catch (SchedulerException e) { throw new BusinessException(MainConstant.ERROR_BATCH_INVOKE_FAILD); } } @Override public void pauseBatch(Long batchId) throws BusinessException { Batch batch = batchDao.findOne(batchId); try { scheduler.pauseJob(new JobKey(batch.getTaskName(), batch.getTaskGroup())); } catch (SchedulerException e) { throw new BusinessException(MainConstant.ERROR_BATCH_PAUSE_FAILD); } } @Override public void resumeBatch(Long batchId) throws BusinessException { Batch batch = batchDao.findOne(batchId); try { scheduler.resumeJob(new JobKey(batch.getTaskName(), batch.getTaskGroup())); } catch (SchedulerException e) { throw new BusinessException(MainConstant.ERROR_BATCH_RESUME_FAILD); } } private void runBatch(Batch batchDb) throws BusinessException { Trigger trigger = null; if (batchDb.isSimpleTask()) { trigger = BatchHelper.getSimpleTrigger(batchDb); } else { trigger = BatchHelper.getCronTrigger(batchDb); } JobDetail jobDetail = (JobDetail) trigger.getJobDataMap().remove("jobDetail"); try { scheduler.deleteJob(jobDetail.getKey()); scheduler.addJob(jobDetail, true); scheduler.scheduleJob(trigger); } catch (SchedulerException e) { throw new BusinessException(MainConstant.ERROR_BATCH_SCHEDULE_FAILD); } } private void prepareBatchDb(BatchDto batchDto, Batch batch) throws BusinessException { batch.setVersion(batchDto.getVersion()); batch.setTaskName(batchDto.getTaskName()); batch.setTaskGroup(Scheduler.DEFAULT_GROUP); batch.setTaskClass(batchDto.getTaskClass()); batch.setStartDate(batchDto.getStartDate()); batch.setEndDate(batchDto.getEndDate()); batch.setMisfireType(batchDto.getMisfireType()); batch.setHolidayRest(batchDto.isHolidayRest()); batch.setSimpleTask(batchDto.isSimpleTask()); if (batchDto.isSimpleTask()) { batch.setWorkingDay(batchDto.getWorkingDay()); batch.setRepeatCount(batchDto.getRepeatCount()); batch.setRepeatInterval(batchDto.getRepeatInterval()); batch.setYearType(null); batch.setRepeatIntervalYear(null); batch.setCronExYear(null); batch.setMonthType(null); batch.setCronExMonth(null); batch.setDayType(null); batch.setCronExDay(null); batch.setLastDayOffset(null); batch.setNearestDay(null); batch.setCronExWeek(null); batch.setLastWeek(null); batch.setWeekNo(null); batch.setWeek(null); batch.setHourType(null); batch.setCronExHour(null); batch.setMinuteType(null); batch.setCronExMinute(null); batch.setSecondType(null); batch.setCronExSecond(null); batch.setCronExpression(null); } else { batch.setWorkingDay(null); batch.setRepeatCount(null); batch.setRepeatInterval(null); batch.setYearType(batchDto.getYearType()); batch.setRepeatIntervalYear(batchDto.getRepeatIntervalYear()); batch.setCronExYear(batchDto.getCronExYear()); batch.setMonthType(batchDto.getMonthType()); batch.setCronExMonth(StringUtils.join(batchDto.getCronExMonth(), StringUtils.COMMA)); batch.setDayType(batchDto.getDayType()); batch.setCronExDay(StringUtils.join(batchDto.getCronExDay(), StringUtils.COMMA)); batch.setLastDayOffset(batchDto.getLastDayOffset()); batch.setNearestDay(batchDto.getNearestDay()); batch.setCronExWeek(StringUtils.join(batchDto.getCronExWeek(), StringUtils.COMMA)); batch.setLastWeek(batchDto.getLastWeek()); batch.setWeekNo(batchDto.getWeekNo()); batch.setWeek(batchDto.getWeek()); batch.setHourType(batchDto.getHourType()); batch.setCronExHour(StringUtils.join(batchDto.getCronExHour(), StringUtils.COMMA)); batch.setMinuteType(batchDto.getMinuteType()); batch.setCronExMinute(StringUtils.join(batchDto.getCronExMinute(), StringUtils.COMMA)); batch.setSecondType(batchDto.getSecondType()); batch.setCronExSecond(StringUtils.join(batchDto.getCronExSecond(), StringUtils.COMMA)); prepareCronExpression(batch); } } private void prepareCronExpression(Batch batch) throws BusinessException { StringBuilder sb = new StringBuilder(); switch (batch.getSecondType()) { case A: sb.append("*"); break; case T: sb.append(batch.getCronExSecond()); break; default: break; } sb.append(" "); switch (batch.getMinuteType()) { case A: sb.append("*"); break; case T: sb.append(batch.getCronExMinute()); break; default: break; } sb.append(" "); switch (batch.getHourType()) { case A: sb.append("*"); break; case T: sb.append(batch.getCronExHour()); break; default: break; } sb.append(" "); boolean dayOfMonth = true; switch (batch.getDayType()) { case A: sb.append("*"); break; case W: sb.append("W"); break; case O: sb.append(batch.getCronExDay()); break; case L: sb.append("L"); break; case B: sb.append("L-"); sb.append(batch.getLastDayOffset()); break; case N: sb.append(batch.getNearestDay()); sb.append("W"); break; default: dayOfMonth = false; sb.append("?"); break; } sb.append(" "); switch (batch.getMonthType()) { case A: sb.append("*"); break; case I: sb.append(batch.getCronExMonth()); break; default: break; } sb.append(" "); if (dayOfMonth) { sb.append("?"); } else { switch (batch.getDayType()) { case CW: sb.append(batch.getCronExWeek()); break; case LW: sb.append(batch.getLastWeek()); sb.append("L"); break; case DW: sb.append(batch.getWeek()); sb.append("#"); sb.append(batch.getWeekNo()); break; default: break; } } sb.append(" "); switch (batch.getYearType()) { case A: sb.append("*"); break; case E: sb.append("/"); sb.append(batch.getRepeatIntervalYear()); break; case I: sb.append(batch.getCronExYear()); break; default: break; } String cronExpression = sb.toString(); if (CronExpression.isValidExpression(cronExpression)) { batch.setCronExpression(cronExpression); } else { throw new BusinessException(MainConstant.ERROR_BATCH_CRONEXPRESSION_ERROR); } } }