467d43f9d4211cce38adf8aa0298426e142e0720
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.context.impl.distribution;
22
23 import java.util.Timer;
24 import java.util.TimerTask;
25
26 import org.onap.policy.apex.context.ContextException;
27 import org.onap.policy.apex.context.Distributor;
28 import org.onap.policy.apex.context.parameters.PersistorParameters;
29 import org.onap.policy.apex.model.basicmodel.service.ParameterService;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
32
33 /**
34  * This class is used to periodically flush a context distributor.
35  *
36  * @author eeilfn
37  */
38 public class DistributorFlushTimerTask extends TimerTask {
39     private static final XLogger LOGGER = XLoggerFactory.getXLogger(DistributorFlushTimerTask.class);
40
41     // The timer for flushing
42     private Timer timer = null;
43
44     // The context distributor to flush
45     private final Distributor contextDistributor;
46
47     // Timing information
48     private long period = 0;
49     private long flushCount = 0;
50
51     /**
52      * Constructor, save a reference to the event stream handler.
53      *
54      * @param contextDistributor the distributor that this timer task is flushing
55      * @throws ContextException On flush setup errors
56      */
57     public DistributorFlushTimerTask(final Distributor contextDistributor) throws ContextException {
58         // Save the context distributor and period
59         this.contextDistributor = contextDistributor;
60
61         // Set the period for persistence flushing
62         final PersistorParameters persistorParameters = ParameterService.getParameters(PersistorParameters.class);
63         period = persistorParameters.getFlushPeriod();
64
65         // Set up the timer
66         timer = new Timer(DistributorFlushTimerTask.class.getSimpleName(), true);
67         timer.schedule(this, period, period);
68
69         LOGGER.debug("context distributor " + contextDistributor.getKey().getID() + " flushing set up with interval: "
70                 + period + "ms");
71     }
72
73     /**
74      * Flush the context distributor.
75      */
76     @Override
77     public void run() {
78         // Increment the flush counter
79         flushCount++;
80
81         LOGGER.debug("context distributor " + contextDistributor.getKey().getID() + " flushing: period=" + period
82                 + ": count=" + flushCount);
83         try {
84             contextDistributor.flush();
85             LOGGER.debug("context distributor " + contextDistributor.getKey().getID() + " flushed: period=" + period
86                     + ": count=" + flushCount);
87         } catch (final ContextException e) {
88             LOGGER.error("flush error on context distributor " + contextDistributor.getKey().getID() + ": period="
89                     + period + ": count=" + flushCount, e);
90         }
91     }
92
93     /**
94      * Cancel the timer.
95      *
96      * @return true, if cancel
97      */
98     @Override
99     public boolean cancel() {
100         // Cancel the timer
101         if (timer != null) {
102             timer.cancel();
103         }
104         return true;
105     }
106
107     /*
108      * (non-Javadoc)
109      *
110      * @see java.lang.Object#toString()
111      */
112     @Override
113     public String toString() {
114         return "ContextDistributorFlushTimerTask [period=" + period + ", flushCount=" + flushCount + "]";
115     }
116 }