cd1e4af5ac7ea17767b6e8b5943890a7b0e17e7c
[vfc/nfvo/driver/ems.git] / ems / boco / src / main / java / org / onap / vfc / nfvo / emsdriver / taskscheduler / QuartzManager.java
1 /**
2  * Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.vfc.nfvo.emsdriver.taskscheduler;
17
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.onap.vfc.nfvo.emsdriver.commons.model.CollectVo;
22 import org.quartz.*;
23 import org.quartz.impl.StdSchedulerFactory;
24
25 import java.text.ParseException;
26
27 public class QuartzManager {
28
29
30     private static Log log = LogFactory.getFactory().getInstance(QuartzManager.class);
31     private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
32     private static String jobGroupName = "EXTJWEB_JOBGROUP_NAME";
33     private static String triggerGroupName = "EXTJWEB_TRIGGERGROUP_NAME";
34
35     /**
36      * @param jobName
37      * @param job
38      * @param time
39      * @throws SchedulerException
40      * @throws ParseException
41      */
42     public static boolean addJob(String jobName, String jobClass, String time, CollectVo collectVo) {
43         boolean sucess = false;
44         try {
45             Scheduler sched = gSchedulerFactory.getScheduler();
46             JobDetail jobDetail = new JobDetail(jobName, jobGroupName, Class.forName(jobClass));
47
48             CronTrigger trigger = new CronTrigger(jobName, triggerGroupName);
49             trigger.setCronExpression(time);
50
51             jobDetail.getJobDataMap().put("collectVo", collectVo);
52             sched.scheduleJob(jobDetail, trigger);
53             if (!sched.isShutdown()) {
54                 sched.start();
55
56             }
57             sucess = true;
58             log.info("add job sucess cronExpression=" + time);
59         } catch (Exception e) {
60             log.error("add job fail cronExpression=" + time, e);
61             sucess = false;
62         }
63         return sucess;
64     }
65
66
67     /**
68      * @param jobName
69      * @param time
70      */
71     @SuppressWarnings("unchecked")
72     public static boolean modifyJobTime(String jobName, String time, CollectVo collectVo) {
73         boolean sucess = false;
74         try {
75             Scheduler sched = gSchedulerFactory.getScheduler();
76             CronTrigger trigger = (CronTrigger) sched.getTrigger(jobName, triggerGroupName);
77             if (trigger == null) {
78                 return false;
79             }
80             String oldTime = trigger.getCronExpression();
81             if (!oldTime.equalsIgnoreCase(time)) {
82                 JobDetail jobDetail = sched.getJobDetail(jobName, jobGroupName);
83
84                 Class<Job> objJobClass = jobDetail.getJobClass();
85                 String jobClass = objJobClass.getName();
86                 removeJob(jobName);
87
88                 addJob(jobName, jobClass, time, collectVo);
89             }
90             sucess = true;
91         } catch (Exception e) {
92             log.error("modifyJobTime fail cronExpression=" + time, e);
93             sucess = false;
94         }
95         return sucess;
96     }
97
98     /**
99
100
101      /**
102      * @param jobName
103      */
104     public static boolean removeJob(String jobName) {
105         boolean sucess = false;
106         try {
107             Scheduler sched = gSchedulerFactory.getScheduler();
108             sched.pauseTrigger(jobName, triggerGroupName);
109             sched.unscheduleJob(jobName, triggerGroupName);
110             sched.deleteJob(jobName, jobGroupName);
111             sucess = true;
112         } catch (Exception e) {
113             sucess = false;
114             log.error("remove job fail jobName=" + jobName, e);
115         }
116         return sucess;
117     }
118
119
120     /**
121      *
122      * @return
123      */
124     public static boolean startJobs() {
125         boolean sucess = false;
126         try {
127             Scheduler sched = gSchedulerFactory.getScheduler();
128             sched.start();
129             sucess = true;
130         } catch (Exception e) {
131             sucess = false;
132             log.error("start jobs fail", e);
133         }
134         return sucess;
135     }
136
137     /**
138      *
139      * @return
140      */
141     public static boolean shutdownJobs() {
142         boolean sucess = false;
143         try {
144             Scheduler sched = gSchedulerFactory.getScheduler();
145             if (!sched.isShutdown()) {
146                 sched.shutdown();
147             }
148             sucess = true;
149         } catch (Exception e) {
150             sucess = false;
151             log.error("shutdown jobs fail ", e);
152         }
153
154         return sucess;
155     }
156 }