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