2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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.
18 * ========================LICENSE_END===================================
21 package org.oransc.policyagent.configuration;
23 import io.swagger.annotations.ApiOperation;
25 import java.time.Duration;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.concurrent.ScheduledFuture;
30 import javax.annotation.PostConstruct;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.scheduling.TaskScheduler;
38 import org.springframework.scheduling.annotation.EnableScheduling;
39 import org.springframework.stereotype.Component;
40 import reactor.core.publisher.Mono;
44 class ApplicationConfigLoader {
46 private static final Logger logger = LoggerFactory.getLogger(ApplicationConfigLoader.class);
47 private static List<ScheduledFuture<?>> scheduledFutureList = new ArrayList<>();
48 private static final Duration CONFIGURATION_REFRESH_INTERVAL = Duration.ofSeconds(15);
50 private final TaskScheduler taskScheduler;
51 private final ApplicationConfig configuration;
54 public ApplicationConfigLoader(TaskScheduler taskScheduler, ApplicationConfig configuration) {
55 this.taskScheduler = taskScheduler;
56 this.configuration = configuration;
60 * Function which have to stop tasks execution.
62 * @return response entity about status of cancellation operation
64 @ApiOperation(value = "Get response on stopping task execution")
65 public synchronized Mono<ResponseEntity<String>> getResponseFromCancellationOfTasks() {
66 scheduledFutureList.forEach(x -> x.cancel(false));
67 scheduledFutureList.clear();
68 logger.info("Stopped");
69 return Mono.just(new ResponseEntity<>("Service has already been stopped!", HttpStatus.CREATED));
73 @ApiOperation(value = "Start task if possible")
74 public synchronized boolean start() {
75 logger.info("Start scheduling Datafile workflow");
76 configuration.initialize();
78 if (scheduledFutureList.isEmpty()) {
80 .add(taskScheduler.scheduleWithFixedDelay(this::refreshConfiguration, CONFIGURATION_REFRESH_INTERVAL));
87 private void refreshConfiguration() {