Fixed MSB Invocation Issues
[holmes/engine-management.git] / engine-d / src / main / java / org / onap / holmes / engine / manager / status / EntityStatusRefreshTask.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.engine.manager.status;
18
19 import org.jvnet.hk2.annotations.Service;
20 import org.onap.holmes.common.config.MicroServiceConfig;
21 import org.onap.holmes.common.engine.entity.EngineEntity;
22 import org.onap.holmes.common.engine.service.EngineEntityService;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import javax.annotation.PostConstruct;
27 import javax.inject.Inject;
28 import java.util.Optional;
29 import java.util.Timer;
30 import java.util.TimerTask;
31
32 import static java.util.concurrent.TimeUnit.SECONDS;
33
34 @Service
35 public class EntityStatusRefreshTask extends TimerTask {
36     private static final Logger logger = LoggerFactory.getLogger(EntityStatusRefreshTask.class);
37     private final long INTERVAL = SECONDS.toMillis(15);
38     private EngineEntity engineEntity;
39     private Timer timer = new Timer("EntityStatusRefreshTimer", true);
40     private EngineEntityService engineEntityService;
41
42     @Inject
43     public EntityStatusRefreshTask(EngineEntityService engineEntityService) {
44         this.engineEntityService = engineEntityService;
45     }
46
47     @PostConstruct
48     private void initialize() {
49         String[] serviceAddrInfo = MicroServiceConfig.getMicroServiceIpAndPort();
50         if (null != serviceAddrInfo) {
51             String ip = serviceAddrInfo[0];
52             String port = Optional.ofNullable(serviceAddrInfo[1]).orElse("9201");
53             port = port.equals("80") ? "9201" : port;
54             engineEntity = new EngineEntity(ip, Integer.parseInt(port));
55
56             timer.schedule(this, SECONDS.toMillis(1), INTERVAL);
57         } else {
58             logger.warn("Failed to get the address info of current engine. " +
59                     "Problems will be caused when it comes to a multi-instance scenario!");
60         }
61     }
62
63     @Override
64     public void run() {
65         if (engineEntity == null) {
66             logger.warn("No engine entity is specified. The status refresh task will return immediately.");
67             return;
68         }
69
70         engineEntity.setLastModified(System.currentTimeMillis());
71
72         try {
73             if (engineEntityService.getEntity(engineEntity.getId()) == null) {
74                 engineEntityService.insertEntity(engineEntity);
75             } else {
76                 engineEntityService.updateEntity(engineEntity);
77             }
78         } catch (Exception e) {
79             logger.warn("Failed to update engine instance information.", e);
80         }
81     }
82 }