2 * Copyright 2016 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common;
18 import java.util.Calendar;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.TimeUnit;
25 * Base time job class.<br>
30 * @version NFVO 0.5 Sep 12, 2016
32 public abstract class BaseTimeJob implements Runnable {
34 private ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
36 private long initialDelay = 1;
38 private long period = 1;
40 private String startTime = "";
43 public abstract void run();
55 public void setInitialDelay(long initialDelay) {
56 this.initialDelay = initialDelay;
59 public void setPeriod(long period) {
70 if(startTime.length() != 0) {
71 String[] vnfTime = startTime.split(":");
72 if(vnfTime.length == 2) {
73 int minute = Integer.parseInt(vnfTime[0]) * 60 + Integer.parseInt(vnfTime[1]);
74 Calendar calendar = Calendar.getInstance();
75 int curMinute = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE);
76 if(curMinute <= minute) {
77 initialDelay = (minute - curMinute) * 60L;
79 initialDelay = (minute + 24 * 60 - curMinute) * 60L;
83 service.scheduleAtFixedRate(this, initialDelay, period, TimeUnit.SECONDS);