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.exceptions.MusicLockingException;
37 import org.onap.music.exceptions.MusicServiceException;
39 import com.datastax.driver.core.ResultSet;
40 import com.datastax.driver.core.Row;
43 public class CronJobManager implements ServletContextListener {
45 private ScheduledExecutorService scheduler;
46 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CronJobManager.class);
49 public void contextInitialized(ServletContextEvent event) {
50 scheduler = Executors.newSingleThreadScheduledExecutor();
51 scheduler.scheduleAtFixedRate(new CachingUtil(), 0, 24, TimeUnit.HOURS);
52 PreparedQueryObject pQuery = new PreparedQueryObject();
53 String consistency = MusicUtil.EVENTUAL;
54 pQuery.appendQueryString("CREATE TABLE IF NOT EXISTS admin.locks ( lock_id text PRIMARY KEY, ctime text)");
56 ResultType result = MusicCore.nonKeyRelatedPut(pQuery, consistency);
57 } catch (MusicServiceException e1) {
62 scheduler.scheduleAtFixedRate(new Runnable() {
67 } , 0, 24, TimeUnit.HOURS);
71 public void contextDestroyed(ServletContextEvent event) {
72 scheduler.shutdownNow();
75 public void deleteLocksFromDB() {
76 PreparedQueryObject pQuery = new PreparedQueryObject();
77 pQuery.appendQueryString(
78 "select * from admin.locks");
80 ResultSet rs = MusicCore.get(pQuery);
81 Iterator<Row> it = rs.iterator();
82 StringBuilder deleteKeys = new StringBuilder();
83 Boolean expiredKeys = false;
84 while (it.hasNext()) {
85 Row row = (Row) it.next();
86 String id = row.getString("lock_id");
87 long ctime = Long.parseLong(row.getString("ctime"));
88 if(System.currentTimeMillis() >= ctime + 24 * 60 * 60 * 1000) {
90 String new_id = id.substring(1);
92 MusicCore.deleteLock(new_id);
93 } catch (MusicLockingException e) {
94 logger.info(EELFLoggerDelegate.applicationLogger,
97 deleteKeys.append("'").append(id).append("'").append(",");
101 deleteKeys.deleteCharAt(deleteKeys.length()-1);
102 CachingUtil.deleteKeysFromDB(deleteKeys.toString());
104 } catch (MusicServiceException e) {