Fixed MSB Invocation Issues
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / controller / EngineInstanceController.java
1 /**
2  * Copyright 2020 ZTE Corporation.
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
17 package org.onap.holmes.rulemgt.controller;
18
19 import org.jvnet.hk2.annotations.Service;
20 import org.onap.holmes.common.engine.entity.EngineEntity;
21 import org.onap.holmes.common.engine.service.EngineEntityService;
22
23 import javax.annotation.PostConstruct;
24 import javax.inject.Inject;
25 import javax.inject.Named;
26 import java.util.List;
27 import java.util.Timer;
28 import java.util.TimerTask;
29
30 import static java.util.concurrent.TimeUnit.MINUTES;
31 import static java.util.concurrent.TimeUnit.SECONDS;
32
33 @Service
34 public class EngineInstanceController extends TimerTask {
35     private static final long INTERVAL = SECONDS.toMillis(15);
36     private static final long THRESHOLD = 3 * INTERVAL;
37     private Timer timer = new Timer("EngineInstanceController", true);
38
39     @Inject
40     private EngineEntityService engineEntityService;
41
42     @PostConstruct
43     public void initialize() {
44         timer.schedule(this, MINUTES.toMillis(1), INTERVAL);
45     }
46
47     @Override
48     public void run() {
49         List<EngineEntity> entityList = engineEntityService.getAllEntities();
50         for (EngineEntity entity : entityList) {
51             if (System.currentTimeMillis() - entity.getLastModified() > THRESHOLD) {
52                 engineEntityService.deleteEntity(entity.getId());
53             }
54         }
55     }
56 }