2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.context.impl.distribution;
23 import java.util.Timer;
24 import java.util.TimerTask;
25 import org.onap.policy.apex.context.ContextException;
26 import org.onap.policy.apex.context.Distributor;
27 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
28 import org.onap.policy.apex.context.parameters.PersistorParameters;
29 import org.onap.policy.common.parameters.ParameterService;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
34 * This class is used to periodically flush a context distributor.
38 public class DistributorFlushTimerTask extends TimerTask {
39 private static final XLogger LOGGER = XLoggerFactory.getXLogger(DistributorFlushTimerTask.class);
41 // The timer for flushing
42 private Timer timer = null;
44 // The context distributor to flush
45 private final Distributor contextDistributor;
48 private long flushPeriod = 0;
49 private long flushCount = 0;
52 * Constructor, save a reference to the event stream handler.
54 * @param contextDistributor the distributor that this timer task is flushing
55 * @throws ContextException On flush setup errors
57 public DistributorFlushTimerTask(final Distributor contextDistributor) throws ContextException {
58 // Save the context distributor and period
59 this.contextDistributor = contextDistributor;
61 // Set the period for persistence flushing
62 final PersistorParameters persistorParameters = ParameterService
63 .get(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
64 flushPeriod = persistorParameters.getFlushPeriod();
67 timer = new Timer(DistributorFlushTimerTask.class.getSimpleName(), true);
68 timer.schedule(this, flushPeriod, flushPeriod);
70 LOGGER.debug("context distributor {} flushing set up with interval: {} ms", contextDistributor.getKey().getId(),
75 * Flush the context distributor.
79 // Increment the flush counter
82 LOGGER.debug("context distributor {} flushing: period={}: count={}", contextDistributor.getKey().getId(),
83 flushPeriod, flushCount);
85 contextDistributor.flush();
86 LOGGER.debug("context distributor {} flushed: period={}: count={}", contextDistributor.getKey().getId(),
87 flushPeriod, flushCount);
88 } catch (final ContextException e) {
89 LOGGER.error("flush error on context distributor {}: period={}: count={}",
90 contextDistributor.getKey().getId(), flushPeriod, flushCount, e);
97 * @return true, if cancel
100 public boolean cancel() {
112 public String toString() {
113 return "ContextDistributorFlushTimerTask [period=" + flushPeriod + ", flushCount=" + flushCount + "]";