2 * ===============================LICENSE_START======================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============================LICENSE_END===========================================
\r
21 package org.openecomp.dcae.apod.analytics.cdap.tca.worker;
\r
23 import co.cask.cdap.api.worker.AbstractWorker;
\r
24 import com.google.common.base.Preconditions;
\r
25 import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants;
\r
26 import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;
\r
27 import org.quartz.Scheduler;
\r
28 import org.quartz.SchedulerException;
\r
29 import org.slf4j.Logger;
\r
30 import org.slf4j.LoggerFactory;
\r
32 import java.util.concurrent.atomic.AtomicBoolean;
\r
34 import static java.lang.String.format;
\r
37 * Base logic for DMaaP Workers which uses scheduler to poll DMaaP MR topics at frequent intervals
\r
39 * @author Rajiv Singla . Creation Date: 12/19/2016.
\r
41 public abstract class BaseTCADMaaPMRWorker extends AbstractWorker {
\r
43 private static final Logger LOG = LoggerFactory.getLogger(BaseTCADMaaPMRWorker.class);
\r
48 protected Scheduler scheduler;
\r
50 * Determines if scheduler is shutdown
\r
52 protected AtomicBoolean isSchedulerShutdown;
\r
58 Preconditions.checkNotNull(scheduler, "Scheduler must not be null");
\r
59 String schedulerName = "";
\r
63 schedulerName = scheduler.getSchedulerName();
\r
65 isSchedulerShutdown.getAndSet(false);
\r
67 } catch (SchedulerException e) {
\r
68 final String errorMessage =
\r
69 format("Error while starting TCA DMaaP MR scheduler name: %s, error: %s", schedulerName, e);
\r
70 throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);
\r
73 LOG.info("Successfully started DMaaP MR Scheduler: {}", schedulerName);
\r
75 // indefinite loop which wakes up and confirms scheduler is indeed running
\r
76 while (!isSchedulerShutdown.get()) {
\r
79 Thread.sleep(AnalyticsConstants.TCA_DEFAULT_WORKER_SHUTDOWN_CHECK_INTERVAL_MS);
\r
81 } catch (InterruptedException e) {
\r
83 final String errorMessage =
\r
84 format("Error while checking TCA DMaaP MR Scheduler worker status name: %s, error: %s",
\r
86 throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);
\r
90 LOG.info("Finished execution of TCA DMaaP MR worker thread: {}", schedulerName);
\r
95 public void stop() {
\r
97 Preconditions.checkNotNull(scheduler, "Scheduler must not be null");
\r
98 String schedulerName = "";
\r
102 schedulerName = scheduler.getSchedulerName();
\r
103 LOG.info("Shutting TCA DMaaP MR Scheduler: {}", schedulerName);
\r
104 scheduler.shutdown();
\r
105 isSchedulerShutdown.getAndSet(true);
\r
107 } catch (SchedulerException e) {
\r
109 final String errorMessage =
\r
110 format("Error while shutting down TCA DMaaP MR Scheduler: name: %s, error: %s", schedulerName, e);
\r
111 throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);
\r