2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalsdk.workflow.scheduler;
40 import java.text.ParseException;
41 import java.util.Calendar;
42 import java.util.Date;
43 import java.util.HashMap;
45 import java.util.UUID;
47 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
48 import org.quartz.impl.triggers.CronTriggerImpl;
49 import org.springframework.context.annotation.DependsOn;
50 import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
51 import org.springframework.scheduling.quartz.JobDetailFactoryBean;
52 import org.springframework.stereotype.Component;
55 @DependsOn({"systemProperties"})
56 public class WorkFlowScheduleRegistry{
58 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WorkFlowScheduleRegistry.class);
60 private static final String groupName = "AppGroup";
61 private static final String jobName = "WorkflowScheduleJob";
62 private static final String triggerName = "WorkflowScheduleTrigger";
64 public WorkFlowScheduleRegistry() {
69 public JobDetailFactoryBean jobDetailFactoryBean(Map<String, ?> contextInfoMap) {
70 JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
71 jobDetailFactory.setJobClass(WorkFlowScheduleJob.class);
72 jobDetailFactory.setJobDataAsMap(contextInfoMap);
73 jobDetailFactory.setGroup(groupName);
74 jobDetailFactory.setName(jobName + "_" + UUID.randomUUID());
75 jobDetailFactory.afterPropertiesSet();
76 return jobDetailFactory;
80 public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactory, Long id,
81 String cronExpression, Date startDateTime, Date enddatetime) throws ParseException {
82 CronTriggerFactoryBean cronTriggerFactory = new CronTriggerFactoryBean();
83 cronTriggerFactory.setJobDetail(jobDetailFactory.getObject());
84 cronTriggerFactory.setStartDelay(3000);
85 cronTriggerFactory.setName(triggerName + "_" + id);
86 cronTriggerFactory.setGroup(groupName);
87 logger.debug(EELFLoggerDelegate.debugLogger, triggerName + " Scheduled: " + cronExpression);
88 cronTriggerFactory.setCronExpression( cronExpression); //"0 * * * * ? *"
89 cronTriggerFactory.afterPropertiesSet();
91 final CronTriggerImpl cronTrigger = (CronTriggerImpl) cronTriggerFactory.getObject();
92 cronTrigger.setStartTime(startDateTime == null ? Calendar.getInstance().getTime() : startDateTime);
93 cronTrigger.setEndTime(enddatetime);
94 Date fireAgainTime = cronTrigger.getFireTimeAfter(cronTrigger.getStartTime());
95 if (fireAgainTime == null)
96 throw new IllegalArgumentException("Cron not added as it may not fire again " + " Expr: " + cronExpression + " End Time: "
97 + cronTrigger.getEndTime());
98 return cronTriggerFactory;
101 public CronTriggerFactoryBean setUpTrigger(Long wfId, String serverUrl, String workflowKey, String arguments,
102 String startdatetimecron, Date startDateTime, Date enddatetime) throws ParseException {
104 Map<String, String> contextInfo = new HashMap<>();
105 contextInfo.put("serverUrl", serverUrl);
106 contextInfo.put("workflowKey", workflowKey);
107 contextInfo.put("arguments", arguments);
108 JobDetailFactoryBean jobDetailFactory = jobDetailFactoryBean(contextInfo);
109 CronTriggerFactoryBean cronTriggerFactory = cronTriggerFactoryBean(jobDetailFactory, wfId, startdatetimecron, startDateTime, enddatetime);
110 logger.debug(EELFLoggerDelegate.debugLogger, " Job to be Scheduled: " + contextInfo.get("workflowKey"));
111 return cronTriggerFactory;