2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalsdk.workflow.scheduler;
22 import java.util.Calendar;
23 import java.util.Date;
24 import java.util.HashMap;
26 import java.util.UUID;
28 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
29 import org.quartz.impl.triggers.CronTriggerImpl;
30 import org.springframework.context.annotation.DependsOn;
31 import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
32 import org.springframework.scheduling.quartz.JobDetailFactoryBean;
33 import org.springframework.stereotype.Component;
36 @DependsOn({"systemProperties"})
37 public class WorkFlowScheduleRegistry{
40 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WorkFlowScheduleRegistry.class);
43 public WorkFlowScheduleRegistry() {
47 private static final String groupName = "AppGroup";
48 private static final String jobName = "WorkflowScheduleJob";
49 private static final String triggerName = "WorkflowScheduleTrigger";
52 // private SystemProperties systemProperties;
55 public JobDetailFactoryBean jobDetailFactoryBean(Map<String, ?> contextInfoMap) {
57 JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
58 jobDetailFactory.setJobClass(WorkFlowScheduleJob.class);
59 jobDetailFactory.setJobDataAsMap(contextInfoMap);
60 jobDetailFactory.setGroup(groupName);
61 jobDetailFactory.setName(jobName + "_" + UUID.randomUUID());
62 jobDetailFactory.afterPropertiesSet();
63 return jobDetailFactory;
67 public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactory, Long id,
68 String cronExpression, Date startDateTime, Date enddatetime) throws Exception {
69 CronTriggerFactoryBean cronTriggerFactory = new CronTriggerFactoryBean();
70 cronTriggerFactory.setJobDetail(jobDetailFactory.getObject());
71 cronTriggerFactory.setStartDelay(3000);
72 cronTriggerFactory.setName(triggerName + "_" + id);
73 cronTriggerFactory.setGroup(groupName);
74 logger.debug(EELFLoggerDelegate.debugLogger, (triggerName + " Scheduled: " + cronExpression));
75 cronTriggerFactory.setCronExpression( cronExpression); //"0 * * * * ? *"
76 cronTriggerFactory.afterPropertiesSet();
78 final CronTriggerImpl cronTrigger = (CronTriggerImpl) cronTriggerFactory.getObject();
79 cronTrigger.setStartTime(startDateTime == null ? Calendar.getInstance().getTime() : startDateTime);
80 cronTrigger.setEndTime(enddatetime);
81 Date fireAgainTime = cronTrigger.getFireTimeAfter(cronTrigger.getStartTime());
82 if (fireAgainTime == null)
83 throw new Exception("Cron not added as it may not fire again " + " Expr: " + cronExpression + " End Time: "
84 + cronTrigger.getEndTime());
85 return cronTriggerFactory;
89 public CronTriggerFactoryBean setUpTrigger(Long wfId, String serverUrl, String workflowKey, String arguments,
90 String startdatetimecron, Date startDateTime, Date enddatetime) throws Exception {
92 Map<String, String> contextInfo = new HashMap<String, String>();
93 contextInfo.put("serverUrl", serverUrl);
94 contextInfo.put("workflowKey", workflowKey);
95 contextInfo.put("arguments", arguments);
96 JobDetailFactoryBean jobDetailFactory = jobDetailFactoryBean(contextInfo);
98 CronTriggerFactoryBean cronTriggerFactory = cronTriggerFactoryBean(jobDetailFactory, wfId, startdatetimecron, startDateTime, enddatetime);
100 logger.debug(EELFLoggerDelegate.debugLogger, (" Job to be Scheduled: " + contextInfo.get("workflowKey")));
102 //cronTriggerFactory.
104 return cronTriggerFactory;