1d0a192f2fbe991efcff586e929dfbab57bfbc28
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6  * in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.configuration;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.concurrent.ScheduledFuture;
22
23 import javax.annotation.PostConstruct;
24
25 import org.onap.dcaegen2.collectors.datafile.tasks.ScheduledTasks;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.context.annotation.Configuration;
28 import org.springframework.http.HttpStatus;
29 import org.springframework.http.ResponseEntity;
30 import org.springframework.scheduling.TaskScheduler;
31 import org.springframework.scheduling.annotation.EnableScheduling;
32
33 import io.swagger.annotations.ApiOperation;
34 import reactor.core.publisher.Mono;
35
36 /**
37  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/13/18
38  */
39 @Configuration
40 @EnableScheduling
41 public class SchedulerConfig extends DatafileAppConfig {
42
43     private static final int SCHEDULING_DELAY = 2000;
44     private static volatile List<ScheduledFuture> scheduledFutureList = new ArrayList<ScheduledFuture>();
45
46     private final TaskScheduler taskScheduler;
47     private final ScheduledTasks scheduledTask;
48
49     @Autowired
50     public SchedulerConfig(TaskScheduler taskScheduler, ScheduledTasks scheduledTask) {
51         this.taskScheduler = taskScheduler;
52         this.scheduledTask = scheduledTask;
53     }
54
55     /**
56      * Function which have to stop tasks execution.
57      *
58      * @return response entity about status of cancellation operation
59      */
60     @ApiOperation(value = "Get response on stopping task execution")
61     public synchronized Mono<ResponseEntity<String>> getResponseFromCancellationOfTasks() {
62         scheduledFutureList.forEach(x -> x.cancel(false));
63         scheduledFutureList.clear();
64         return Mono.defer(() -> Mono
65                 .just(new ResponseEntity<>("Datafile Service has already been stopped!", HttpStatus.CREATED)));
66     }
67
68     /**
69      * Function for starting scheduling Datafile workflow.
70      *
71      * @return status of operation execution: true - started, false - not started
72      */
73     @PostConstruct
74     @ApiOperation(value = "Start task if possible")
75     public synchronized boolean tryToStartTask() {
76         if (scheduledFutureList.isEmpty()) {
77             scheduledFutureList.add(taskScheduler.scheduleWithFixedDelay(scheduledTask::scheduleMainDatafileEventTask,
78                     SCHEDULING_DELAY));
79             return true;
80         } else {
81             return false;
82         }
83
84     }
85 }