823fe73215fcbae78faeb986450617bbc357e6af
[dcaegen2/collectors/datafile.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dcaegen2.collectors.datafile.configuration;
22
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;
28
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;
37
38 /**
39  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/13/18
40  */
41 @Configuration
42 @EnableScheduling
43 public class SchedulerConfig extends DatafileAppConfig {
44
45     private static final int SCHEDULING_DELAY = 2000;
46     private static volatile List<ScheduledFuture> scheduledFutureList = new ArrayList<>();
47
48     private final TaskScheduler taskScheduler;
49     private final ScheduledTasks scheduledTask;
50
51     @Autowired
52     public SchedulerConfig(TaskScheduler taskScheduler, ScheduledTasks scheduledTask) {
53         this.taskScheduler = taskScheduler;
54         this.scheduledTask = scheduledTask;
55     }
56
57     /**
58      * Function which have to stop tasks execution.
59      *
60      * @return response entity about status of cancellation operation
61      */
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))
68         );
69     }
70
71     /**
72      * Function for starting scheduling Datafile workflow.
73      *
74      * @return status of operation execution: true - started, false - not started
75      */
76     @PostConstruct
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));
82             return true;
83         } else {
84             return false;
85         }
86
87     }
88 }