2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.context.impl.distribution;
24 import java.util.Timer;
25 import java.util.TimerTask;
26 import lombok.ToString;
27 import org.onap.policy.apex.context.ContextException;
28 import org.onap.policy.apex.context.Distributor;
29 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
30 import org.onap.policy.apex.context.parameters.PersistorParameters;
31 import org.onap.policy.common.parameters.ParameterService;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
36 * This class is used to periodically flush a context distributor.
41 public class DistributorFlushTimerTask extends TimerTask {
42 private static final XLogger LOGGER = XLoggerFactory.getXLogger(DistributorFlushTimerTask.class);
44 // The timer for flushing
46 private Timer timer = null;
48 // The context distributor to flush
50 private final Distributor contextDistributor;
53 private long flushPeriod = 0;
54 private long flushCount = 0;
57 * Constructor, save a reference to the event stream handler.
59 * @param contextDistributor the distributor that this timer task is flushing
60 * @throws ContextException On flush setup errors
62 public DistributorFlushTimerTask(final Distributor contextDistributor) throws ContextException {
63 // Save the context distributor and period
64 this.contextDistributor = contextDistributor;
66 // Set the period for persistence flushing
67 final PersistorParameters persistorParameters = ParameterService
68 .get(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
69 flushPeriod = persistorParameters.getFlushPeriod();
72 timer = new Timer(DistributorFlushTimerTask.class.getSimpleName(), true);
73 timer.schedule(this, flushPeriod, flushPeriod);
75 LOGGER.debug("context distributor {} flushing set up with interval: {} ms", contextDistributor.getKey().getId(),
80 * Flush the context distributor.
84 // Increment the flush counter
87 LOGGER.debug("context distributor {} flushing: period={}: count={}", contextDistributor.getKey().getId(),
88 flushPeriod, flushCount);
90 contextDistributor.flush();
91 LOGGER.debug("context distributor {} flushed: period={}: count={}", contextDistributor.getKey().getId(),
92 flushPeriod, flushCount);
93 } catch (final ContextException e) {
94 LOGGER.error("flush error on context distributor {}: period={}: count={}",
95 contextDistributor.getKey().getId(), flushPeriod, flushCount, e);
102 * @return true, if cancel
105 public boolean cancel() {