2 * ============LICENSE_START=======================================================
3 * Datafile Collector Service
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6 * ================================================================================
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.onap.dcaegen2.collectors.datafile.configuration;
23 import io.swagger.annotations.ApiOperation;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.concurrent.ScheduledFuture;
27 import javax.annotation.PostConstruct;
29 import org.onap.dcaegen2.collectors.datafile.tasks.ScheduledTasks;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.context.annotation.Configuration;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.scheduling.TaskScheduler;
35 import org.springframework.scheduling.annotation.EnableScheduling;
36 import reactor.core.publisher.Mono;
39 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/13/18
43 public class SchedulerConfig extends DatafileAppConfig {
45 private static final int SCHEDULING_DELAY = 2000;
46 private static volatile List<ScheduledFuture> scheduledFutureList = new ArrayList<>();
48 private final TaskScheduler taskScheduler;
49 private final ScheduledTasks scheduledTask;
52 public SchedulerConfig(TaskScheduler taskScheduler, ScheduledTasks scheduledTask) {
53 this.taskScheduler = taskScheduler;
54 this.scheduledTask = scheduledTask;
58 * Function which have to stop tasks execution.
60 * @return response entity about status of cancellation operation
62 @ApiOperation(value = "Get response on stopping task execution")
63 public synchronized Mono<ResponseEntity<String>> getResponseFromCancellationOfTasks() {
64 scheduledFutureList.forEach(x -> x.cancel(false));
65 scheduledFutureList.clear();
66 return Mono.defer(() ->
67 Mono.just(new ResponseEntity<>("Datafile Service has already been stopped!", HttpStatus.CREATED))
72 * Function for starting scheduling Datafile workflow.
74 * @return status of operation execution: true - started, false - not started
77 @ApiOperation(value = "Start task if possible")
78 public synchronized boolean tryToStartTask() {
79 if (scheduledFutureList.isEmpty()) {
80 scheduledFutureList.add(taskScheduler
81 .scheduleWithFixedDelay(scheduledTask::scheduleMainDatafileEventTask, SCHEDULING_DELAY));