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