@ -1,10 +1,12 @@
package com.xxl.job.admin.service.impl ;
package com.xxl.job.admin.service.impl ;
import com.xxl.job.admin.core.cron.CronExpression ;
import com.xxl.job.admin.core.model.XxlJobGroup ;
import com.xxl.job.admin.core.model.XxlJobGroup ;
import com.xxl.job.admin.core.model.XxlJobInfo ;
import com.xxl.job.admin.core.model.XxlJobInfo ;
import com.xxl.job.admin.core.cron.CronExpression ;
import com.xxl.job.admin.core.model.XxlJobLogReport ;
import com.xxl.job.admin.core.model.XxlJobLogReport ;
import com.xxl.job.admin.core.route.ExecutorRouteStrategyEnum ;
import com.xxl.job.admin.core.route.ExecutorRouteStrategyEnum ;
import com.xxl.job.admin.core.scheduler.MisfireStrategyEnum ;
import com.xxl.job.admin.core.scheduler.ScheduleTypeEnum ;
import com.xxl.job.admin.core.thread.JobScheduleHelper ;
import com.xxl.job.admin.core.thread.JobScheduleHelper ;
import com.xxl.job.admin.core.util.I18nUtil ;
import com.xxl.job.admin.core.util.I18nUtil ;
import com.xxl.job.admin.dao.* ;
import com.xxl.job.admin.dao.* ;
@ -58,39 +60,66 @@ public class XxlJobServiceImpl implements XxlJobService {
@Override
@Override
public ReturnT < String > add ( XxlJobInfo jobInfo ) {
public ReturnT < String > add ( XxlJobInfo jobInfo ) {
// valid
// valid base
XxlJobGroup group = xxlJobGroupDao . load ( jobInfo . getJobGroup ( ) ) ;
XxlJobGroup group = xxlJobGroupDao . load ( jobInfo . getJobGroup ( ) ) ;
if ( group = = null ) {
if ( group = = null ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "system_please_choose" ) + I18nUtil . getString ( "jobinfo_field_jobgroup" ) ) ) ;
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "system_please_choose" ) + I18nUtil . getString ( "jobinfo_field_jobgroup" ) ) ) ;
}
}
if ( ! CronExpression . isValidExpression ( jobInfo . getJobCron ( ) ) ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , I18nUtil . getString ( "jobinfo_field_cron_unvalid" ) ) ;
}
if ( jobInfo . getJobDesc ( ) = = null | | jobInfo . getJobDesc ( ) . trim ( ) . length ( ) = = 0 ) {
if ( jobInfo . getJobDesc ( ) = = null | | jobInfo . getJobDesc ( ) . trim ( ) . length ( ) = = 0 ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "system_please_input" ) + I18nUtil . getString ( "jobinfo_field_jobdesc" ) ) ) ;
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "system_please_input" ) + I18nUtil . getString ( "jobinfo_field_jobdesc" ) ) ) ;
}
}
if ( jobInfo . getAuthor ( ) = = null | | jobInfo . getAuthor ( ) . trim ( ) . length ( ) = = 0 ) {
if ( jobInfo . getAuthor ( ) = = null | | jobInfo . getAuthor ( ) . trim ( ) . length ( ) = = 0 ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "system_please_input" ) + I18nUtil . getString ( "jobinfo_field_author" ) ) ) ;
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "system_please_input" ) + I18nUtil . getString ( "jobinfo_field_author" ) ) ) ;
}
}
if ( ExecutorRouteStrategyEnum . match ( jobInfo . getExecutorRouteStrategy ( ) , null ) = = null ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "jobinfo_field_executorRouteStrategy" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
// valid trigger
ScheduleTypeEnum scheduleTypeEnum = ScheduleTypeEnum . match ( jobInfo . getScheduleType ( ) , null ) ;
if ( scheduleTypeEnum = = null ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "schedule_type" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
}
if ( ExecutorBlockStrategyEnum . match ( jobInfo . getExecutorBlockStrategy ( ) , null ) = = null ) {
if ( scheduleTypeEnum = = ScheduleTypeEnum . CRON ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "jobinfo_field_executorBlockStrategy" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
if ( jobInfo . getScheduleConf ( ) = = null | | ! CronExpression . isValidExpression ( jobInfo . getScheduleConf ( ) ) ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , I18nUtil . getString ( "jobinfo_field_cron_unvalid" ) ) ;
}
}
} else if ( scheduleTypeEnum = = ScheduleTypeEnum . FIX_RATE | | scheduleTypeEnum = = ScheduleTypeEnum . FIX_DELAY ) {
if ( jobInfo . getScheduleConf ( ) = = null ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "schedule_type" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
try {
int fixSecond = Integer . valueOf ( jobInfo . getScheduleConf ( ) ) ;
if ( fixSecond < 1 ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "schedule_type" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
} catch ( Exception e ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "schedule_type" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
}
// valid job
if ( GlueTypeEnum . match ( jobInfo . getGlueType ( ) ) = = null ) {
if ( GlueTypeEnum . match ( jobInfo . getGlueType ( ) ) = = null ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "jobinfo_field_gluetype" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "jobinfo_field_gluetype" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
}
if ( GlueTypeEnum . BEAN = = GlueTypeEnum . match ( jobInfo . getGlueType ( ) ) & & ( jobInfo . getExecutorHandler ( ) = = null | | jobInfo . getExecutorHandler ( ) . trim ( ) . length ( ) = = 0 ) ) {
if ( GlueTypeEnum . BEAN = = GlueTypeEnum . match ( jobInfo . getGlueType ( ) ) & & ( jobInfo . getExecutorHandler ( ) = = null | | jobInfo . getExecutorHandler ( ) . trim ( ) . length ( ) = = 0 ) ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "system_please_input" ) + "JobHandler" ) ) ;
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "system_please_input" ) + "JobHandler" ) ) ;
}
}
// 》fix "\r" in shell
// fix "\r" in shell
if ( GlueTypeEnum . GLUE_SHELL = = GlueTypeEnum . match ( jobInfo . getGlueType ( ) ) & & jobInfo . getGlueSource ( ) ! = null ) {
if ( GlueTypeEnum . GLUE_SHELL = = GlueTypeEnum . match ( jobInfo . getGlueType ( ) ) & & jobInfo . getGlueSource ( ) ! = null ) {
jobInfo . setGlueSource ( jobInfo . getGlueSource ( ) . replaceAll ( "\r" , "" ) ) ;
jobInfo . setGlueSource ( jobInfo . getGlueSource ( ) . replaceAll ( "\r" , "" ) ) ;
}
}
// ChildJobId valid
// valid advanced
if ( ExecutorRouteStrategyEnum . match ( jobInfo . getExecutorRouteStrategy ( ) , null ) = = null ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "jobinfo_field_executorRouteStrategy" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
if ( MisfireStrategyEnum . match ( jobInfo . getMisfireStrategy ( ) , null ) = = null ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "misfire_strategy" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
if ( ExecutorBlockStrategyEnum . match ( jobInfo . getExecutorBlockStrategy ( ) , null ) = = null ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "jobinfo_field_executorBlockStrategy" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
// 》ChildJobId valid
if ( jobInfo . getChildJobId ( ) ! = null & & jobInfo . getChildJobId ( ) . trim ( ) . length ( ) > 0 ) {
if ( jobInfo . getChildJobId ( ) ! = null & & jobInfo . getChildJobId ( ) . trim ( ) . length ( ) > 0 ) {
String [ ] childJobIds = jobInfo . getChildJobId ( ) . split ( "," ) ;
String [ ] childJobIds = jobInfo . getChildJobId ( ) . split ( "," ) ;
for ( String childJobIdItem : childJobIds ) {
for ( String childJobIdItem : childJobIds ) {
@ -140,24 +169,49 @@ public class XxlJobServiceImpl implements XxlJobService {
@Override
@Override
public ReturnT < String > update ( XxlJobInfo jobInfo ) {
public ReturnT < String > update ( XxlJobInfo jobInfo ) {
// valid
// valid base
if ( ! CronExpression . isValidExpression ( jobInfo . getJobCron ( ) ) ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , I18nUtil . getString ( "jobinfo_field_cron_unvalid" ) ) ;
}
if ( jobInfo . getJobDesc ( ) = = null | | jobInfo . getJobDesc ( ) . trim ( ) . length ( ) = = 0 ) {
if ( jobInfo . getJobDesc ( ) = = null | | jobInfo . getJobDesc ( ) . trim ( ) . length ( ) = = 0 ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "system_please_input" ) + I18nUtil . getString ( "jobinfo_field_jobdesc" ) ) ) ;
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "system_please_input" ) + I18nUtil . getString ( "jobinfo_field_jobdesc" ) ) ) ;
}
}
if ( jobInfo . getAuthor ( ) = = null | | jobInfo . getAuthor ( ) . trim ( ) . length ( ) = = 0 ) {
if ( jobInfo . getAuthor ( ) = = null | | jobInfo . getAuthor ( ) . trim ( ) . length ( ) = = 0 ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "system_please_input" ) + I18nUtil . getString ( "jobinfo_field_author" ) ) ) ;
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "system_please_input" ) + I18nUtil . getString ( "jobinfo_field_author" ) ) ) ;
}
}
// valid trigger
ScheduleTypeEnum scheduleTypeEnum = ScheduleTypeEnum . match ( jobInfo . getScheduleType ( ) , null ) ;
if ( scheduleTypeEnum = = null ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "schedule_type" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
if ( scheduleTypeEnum = = ScheduleTypeEnum . CRON ) {
if ( jobInfo . getScheduleConf ( ) = = null | | ! CronExpression . isValidExpression ( jobInfo . getScheduleConf ( ) ) ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , I18nUtil . getString ( "jobinfo_field_cron_unvalid" ) ) ;
}
} else if ( scheduleTypeEnum = = ScheduleTypeEnum . FIX_RATE | | scheduleTypeEnum = = ScheduleTypeEnum . FIX_DELAY ) {
if ( jobInfo . getScheduleConf ( ) = = null ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "schedule_type" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
try {
int fixSecond = Integer . valueOf ( jobInfo . getScheduleConf ( ) ) ;
if ( fixSecond < 1 ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "schedule_type" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
} catch ( Exception e ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "schedule_type" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
}
// valid advanced
if ( ExecutorRouteStrategyEnum . match ( jobInfo . getExecutorRouteStrategy ( ) , null ) = = null ) {
if ( ExecutorRouteStrategyEnum . match ( jobInfo . getExecutorRouteStrategy ( ) , null ) = = null ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "jobinfo_field_executorRouteStrategy" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "jobinfo_field_executorRouteStrategy" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
}
if ( MisfireStrategyEnum . match ( jobInfo . getMisfireStrategy ( ) , null ) = = null ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "misfire_strategy" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
if ( ExecutorBlockStrategyEnum . match ( jobInfo . getExecutorBlockStrategy ( ) , null ) = = null ) {
if ( ExecutorBlockStrategyEnum . match ( jobInfo . getExecutorBlockStrategy ( ) , null ) = = null ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "jobinfo_field_executorBlockStrategy" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "jobinfo_field_executorBlockStrategy" ) + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
}
// ChildJobId valid
// 》 ChildJobId valid
if ( jobInfo . getChildJobId ( ) ! = null & & jobInfo . getChildJobId ( ) . trim ( ) . length ( ) > 0 ) {
if ( jobInfo . getChildJobId ( ) ! = null & & jobInfo . getChildJobId ( ) . trim ( ) . length ( ) > 0 ) {
String [ ] childJobIds = jobInfo . getChildJobId ( ) . split ( "," ) ;
String [ ] childJobIds = jobInfo . getChildJobId ( ) . split ( "," ) ;
for ( String childJobIdItem : childJobIds ) {
for ( String childJobIdItem : childJobIds ) {
@ -197,24 +251,27 @@ public class XxlJobServiceImpl implements XxlJobService {
// next trigger time (5s后生效, 避开预读周期)
// next trigger time (5s后生效, 避开预读周期)
long nextTriggerTime = exists_jobInfo . getTriggerNextTime ( ) ;
long nextTriggerTime = exists_jobInfo . getTriggerNextTime ( ) ;
if ( exists_jobInfo . getTriggerStatus ( ) = = 1 & & ! jobInfo . getJobCron ( ) . equals ( exists_jobInfo . getJobCron ( ) ) ) {
boolean scheduleDataNotChanged = jobInfo . getScheduleType ( ) . equals ( exists_jobInfo . getScheduleType ( ) ) & & jobInfo . getScheduleConf ( ) . equals ( exists_jobInfo . getScheduleConf ( ) ) ;
if ( exists_jobInfo . getTriggerStatus ( ) = = 1 & & ! scheduleDataNotChanged ) {
try {
try {
Date nextValidTime = new CronExpression ( jobInfo . getJobCron ( ) ) . getNextValidTimeAfter ( new Date ( System . currentTimeMillis ( ) + JobScheduleHelper . PRE_READ_MS ) ) ;
Date nextValidTime = JobScheduleHelper . generateNextValidTime ( jobInfo , new Date ( System . currentTimeMillis ( ) + JobScheduleHelper . PRE_READ_MS ) ) ;
if ( nextValidTime = = null ) {
if ( nextValidTime = = null ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , I18nUtil . getString ( " jobinfo_field_cron_never_fire") ) ;
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( " schedule_type") + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
}
nextTriggerTime = nextValidTime . getTime ( ) ;
nextTriggerTime = nextValidTime . getTime ( ) ;
} catch ( Parse Exception e ) {
} catch ( Exception e ) {
logger . error ( e . getMessage ( ) , e ) ;
logger . error ( e . getMessage ( ) , e ) ;
return new ReturnT < String > ( ReturnT . FAIL_CODE , I18nUtil . getString ( " jobinfo_field_cron_unvalid") + " | " + e . getMessage ( ) ) ;
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( " schedule_type") + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
}
}
}
exists_jobInfo . setJobGroup ( jobInfo . getJobGroup ( ) ) ;
exists_jobInfo . setJobGroup ( jobInfo . getJobGroup ( ) ) ;
exists_jobInfo . setJobCron ( jobInfo . getJobCron ( ) ) ;
exists_jobInfo . setJobDesc ( jobInfo . getJobDesc ( ) ) ;
exists_jobInfo . setJobDesc ( jobInfo . getJobDesc ( ) ) ;
exists_jobInfo . setAuthor ( jobInfo . getAuthor ( ) ) ;
exists_jobInfo . setAuthor ( jobInfo . getAuthor ( ) ) ;
exists_jobInfo . setAlarmEmail ( jobInfo . getAlarmEmail ( ) ) ;
exists_jobInfo . setAlarmEmail ( jobInfo . getAlarmEmail ( ) ) ;
exists_jobInfo . setScheduleType ( jobInfo . getScheduleType ( ) ) ;
exists_jobInfo . setScheduleConf ( jobInfo . getScheduleConf ( ) ) ;
exists_jobInfo . setMisfireStrategy ( jobInfo . getMisfireStrategy ( ) ) ;
exists_jobInfo . setExecutorRouteStrategy ( jobInfo . getExecutorRouteStrategy ( ) ) ;
exists_jobInfo . setExecutorRouteStrategy ( jobInfo . getExecutorRouteStrategy ( ) ) ;
exists_jobInfo . setExecutorHandler ( jobInfo . getExecutorHandler ( ) ) ;
exists_jobInfo . setExecutorHandler ( jobInfo . getExecutorHandler ( ) ) ;
exists_jobInfo . setExecutorParam ( jobInfo . getExecutorParam ( ) ) ;
exists_jobInfo . setExecutorParam ( jobInfo . getExecutorParam ( ) ) ;
@ -248,17 +305,23 @@ public class XxlJobServiceImpl implements XxlJobService {
public ReturnT < String > start ( int id ) {
public ReturnT < String > start ( int id ) {
XxlJobInfo xxlJobInfo = xxlJobInfoDao . loadById ( id ) ;
XxlJobInfo xxlJobInfo = xxlJobInfoDao . loadById ( id ) ;
// valid
ScheduleTypeEnum scheduleTypeEnum = ScheduleTypeEnum . match ( xxlJobInfo . getScheduleType ( ) , ScheduleTypeEnum . NONE ) ;
if ( ScheduleTypeEnum . NONE = = scheduleTypeEnum ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( "schedule_type_none_limit_start" ) ) ) ;
}
// next trigger time (5s后生效, 避开预读周期)
// next trigger time (5s后生效, 避开预读周期)
long nextTriggerTime = 0 ;
long nextTriggerTime = 0 ;
try {
try {
Date nextValidTime = new CronExpression ( xxlJobInfo . getJobCron ( ) ) . getNextValidTimeAfter ( new Date ( System . currentTimeMillis ( ) + JobScheduleHelper . PRE_READ_MS ) ) ;
Date nextValidTime = JobScheduleHelper . generateNextValidTime ( xxlJobInfo , new Date ( System . currentTimeMillis ( ) + JobScheduleHelper . PRE_READ_MS ) ) ;
if ( nextValidTime = = null ) {
if ( nextValidTime = = null ) {
return new ReturnT < String > ( ReturnT . FAIL_CODE , I18nUtil . getString ( "jobinfo_field_cron_never_fire" ) ) ;
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( " schedule_type") + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
}
nextTriggerTime = nextValidTime . getTime ( ) ;
nextTriggerTime = nextValidTime . getTime ( ) ;
} catch ( ParseException e ) {
} catch ( ParseException e ) {
logger . error ( e . getMessage ( ) , e ) ;
logger . error ( e . getMessage ( ) , e ) ;
return new ReturnT < String > ( ReturnT . FAIL_CODE , I18nUtil . getString ( " jobinfo_field_cron_unvalid") + " | " + e . getMessage ( ) ) ;
return new ReturnT < String > ( ReturnT . FAIL_CODE , ( I18nUtil . getString ( " schedule_type") + I18nUtil . getString ( "system_unvalid" ) ) ) ;
}
}
xxlJobInfo . setTriggerStatus ( 1 ) ;
xxlJobInfo . setTriggerStatus ( 1 ) ;