2 * Copyright 2020 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.holmes.engine.manager.status;
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;
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;
32 import static java.util.concurrent.TimeUnit.SECONDS;
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;
43 public EntityStatusRefreshTask(EngineEntityService engineEntityService) {
44 this.engineEntityService = engineEntityService;
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));
56 timer.schedule(this, SECONDS.toMillis(1), INTERVAL);
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!");
65 if (engineEntity == null) {
66 logger.warn("No engine entity is specified. The status refresh task will return immediately.");
70 engineEntity.setLastModified(System.currentTimeMillis());
73 if (engineEntityService.getEntity(engineEntity.getId()) == null) {
74 engineEntityService.insertEntity(engineEntity);
76 engineEntityService.updateEntity(engineEntity);
78 } catch (Exception e) {
79 logger.warn("Failed to update engine instance information.", e);