class SchedulerService {

    public void addCronJob(TaskInfo info) {
        String jobName = info.getJobName();
        String jobClassName = info.getJobClassName();
        String jobGroupName = info.getJobGroupName();
        String jobDescription = info.getJobDescription();
        String cronExpression = info.getCronExpression();
        Date createTime = new Date();
        JobDataMap dataMap = new JobDataMap();
        if (info.getData() != null) {
            dataMap.putAll(info.getData());
        }
        dataMap.put("createTime", createTime);
        try {
            if (checkExists(jobName, jobGroupName)) {
                throw new OpenAlertException(String.format("任务已经存在, jobName:[%s],jobGroup:[%s]", jobName, jobGroupName));
            }
            TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroupName);
            JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
            CronScheduleBuilder schedBuilder = CronScheduleBuilder
                    .cronSchedule(cronExpression)
                    .withMisfireHandlingInstructionDoNothing();
            CronTrigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity(triggerKey)
                    .withSchedule(schedBuilder).build();

            Class<? extends Job> clazz = (Class<? extends Job>) Class
                    .forName(jobClassName);
            JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(jobKey)
                    .withDescription(jobDescription).usingJobData(dataMap).build();
            scheduler.scheduleJob(jobDetail, trigger);
        } catch (SchedulerException | ClassNotFoundException e) {
            throw new OpenAlertException("任务添加失败");
        }
    }

}

class SchedulerServiceTest  {

    @Autowired
    private SchedulerService schedulerService;
    @Autowired
    private EmailServiceClient emailServiceClient;

    @Test
    public void addCronJob() {

        String cron = "3 * * * * ? *";
        TaskInfo taskInfo = new TaskInfo();
        Map data = Maps.newHashMap();
        String serviceId = "upm-server";
        data.put("serviceId", serviceId);
        data.put("method", "get");
        data.put("path", "/test");
        data.put("contentType", "application/x-www-form-urlencoded");
        data.put("alarmMail", "515608851@qq.com");
        taskInfo.setData(data);
        taskInfo.setJobName("定时任务测试");
        taskInfo.setJobDescription("定时任务描述3秒执行一次");
        taskInfo.setJobClassName(HttpExecuteJob.class.getName());
        taskInfo.setJobGroupName(Scheduler.DEFAULT_GROUP);
        taskInfo.setCronExpression(cron);
        schedulerService.addCronJob(taskInfo);
    }

}