2 * ============LICENSE_START==========================================
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.
19 * ============LICENSE_END=============================================
20 * ====================================================================
22 package org.onap.music.main;
24 import java.util.Iterator;
26 import java.util.Map.Entry;
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.ScheduledExecutorService;
29 import java.util.concurrent.TimeUnit;
30 import javax.servlet.ServletContextEvent;
31 import javax.servlet.ServletContextListener;
32 import javax.servlet.annotation.WebListener;
34 import org.onap.music.datastore.PreparedQueryObject;
35 import org.onap.music.eelf.logging.EELFLoggerDelegate;
36 import org.onap.music.eelf.logging.format.AppMessages;
37 import org.onap.music.eelf.logging.format.ErrorSeverity;
38 import org.onap.music.eelf.logging.format.ErrorTypes;
39 import org.onap.music.exceptions.MusicLockingException;
40 import org.onap.music.exceptions.MusicServiceException;
42 import com.datastax.driver.core.ResultSet;
43 import com.datastax.driver.core.Row;
46 public class CronJobManager implements ServletContextListener {
48 private ScheduledExecutorService scheduler;
49 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CronJobManager.class);
52 public void contextInitialized(ServletContextEvent event) {
53 scheduler = Executors.newSingleThreadScheduledExecutor();
54 scheduler.scheduleAtFixedRate(new CachingUtil(), 0, 24, TimeUnit.HOURS);
55 PreparedQueryObject pQuery = new PreparedQueryObject();
56 String consistency = MusicUtil.EVENTUAL;
57 pQuery.appendQueryString("CREATE TABLE IF NOT EXISTS admin.locks ( lock_id text PRIMARY KEY, ctime text)");
59 ResultType result = MusicCore.nonKeyRelatedPut(pQuery, consistency);
60 } catch (MusicServiceException e1) {
61 logger.error(EELFLoggerDelegate.errorLogger, e1.getMessage(),ErrorSeverity.ERROR);
65 scheduler.scheduleAtFixedRate(new Runnable() {
70 } , 0, 24, TimeUnit.HOURS);
74 public void contextDestroyed(ServletContextEvent event) {
75 scheduler.shutdownNow();
78 public void deleteLocksFromDB() {
79 PreparedQueryObject pQuery = new PreparedQueryObject();
80 pQuery.appendQueryString(
81 "select * from admin.locks");
83 ResultSet rs = MusicCore.get(pQuery);
84 Iterator<Row> it = rs.iterator();
85 StringBuilder deleteKeys = new StringBuilder();
86 Boolean expiredKeys = false;
87 while (it.hasNext()) {
88 Row row = (Row) it.next();
89 String id = row.getString("lock_id");
90 long ctime = Long.parseLong(row.getString("ctime"));
91 if(System.currentTimeMillis() >= ctime + 24 * 60 * 60 * 1000) {
93 String new_id = id.substring(1);
95 MusicCore.deleteLock(new_id);
96 } catch (MusicLockingException e) {
97 logger.info(EELFLoggerDelegate.applicationLogger,
100 deleteKeys.append("'").append(id).append("'").append(",");
104 deleteKeys.deleteCharAt(deleteKeys.length()-1);
105 CachingUtil.deleteKeysFromDB(deleteKeys.toString());
107 } catch (MusicServiceException e) {
108 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),ErrorSeverity.ERROR);