2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2023 Deutsche Telekom 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=========================================================
20 package org.onap.dcaegen2.services.prh.tasks.commit;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.concurrent.ScheduledFuture;
26 import javax.annotation.PreDestroy;
27 import org.onap.dcaegen2.services.prh.configuration.PrhProperties;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.slf4j.Marker;
31 import org.slf4j.MarkerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.boot.context.event.ApplicationStartedEvent;
34 import org.springframework.context.annotation.Configuration;
35 import org.springframework.context.annotation.Profile;
36 import org.springframework.context.event.EventListener;
37 import org.springframework.scheduling.TaskScheduler;
38 import org.springframework.scheduling.annotation.EnableScheduling;
41 * @author <a href="mailto:pravin.kokane@t-systems.com">Pravin Kokane</a> on 3/13/23
44 @Profile("autoCommitDisabled")
47 public class ScheduledTasksRunnerWithCommit {
48 private static final Logger LOGGER = LoggerFactory.getLogger(ScheduledTasksRunnerWithCommit.class);
49 private static final Marker ENTRY = MarkerFactory.getMarker("ENTRY");
50 private static List<ScheduledFuture> scheduledPrhTaskFutureList = new ArrayList<>();
52 private final TaskScheduler taskScheduler;
53 private final PrhProperties prhProperties;
56 private ScheduledTasksWithCommit scheduledTasksWithCommit;
58 public ScheduledTasksRunnerWithCommit(TaskScheduler taskScheduler, ScheduledTasksWithCommit scheduledTasksWithCommit,
59 PrhProperties prhProperties) {
60 this.taskScheduler = taskScheduler;
61 this.scheduledTasksWithCommit = scheduledTasksWithCommit;
62 this.prhProperties = prhProperties;
66 public void onApplicationStartedEvent(ApplicationStartedEvent applicationStartedEvent) {
67 tryToStartTaskWithCommit();
71 * Function which have to stop tasks execution.
74 public synchronized void cancelTasks() {
75 scheduledPrhTaskFutureList.forEach(x -> x.cancel(false));
76 scheduledPrhTaskFutureList.clear();
80 * Function for starting scheduling PRH workflow.
82 * @return status of operation execution: true - started, false - not started
85 public synchronized boolean tryToStartTaskWithCommit() {
86 LOGGER.info(ENTRY, "Start scheduling PRH workflow with Commit Tasks Runner");
87 if (scheduledPrhTaskFutureList.isEmpty()) {
88 Collections.synchronizedList(scheduledPrhTaskFutureList);
89 scheduledPrhTaskFutureList.add(taskScheduler
90 .scheduleWithFixedDelay(scheduledTasksWithCommit::scheduleKafkaPrhEventTask,
91 prhProperties.getWorkflowSchedulingInterval()));